I have a unit test I am trying check the field but cannot find anything in the jest documentation to ignore the one value in this case, randomNumber.
const expected = [
{
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
},
]
The random number changes every test so I can't hard code the test to check that number. I would like to test to make sure the expected array has all of the fields, but ignore the values.
CodePudding user response:
const requiredKeys = ["model", "type", "randomNumber", "name"];
const actual = [
{
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
},
{
type: 'string',
randomNumber: 43,
name: 'string',
},
]
actual.forEach(x => {
const keys = Object.keys(x);
const hasAllKeys = requiredKeys.every(key => keys.includes(key));
expect(hasAllkeys).toBeTruthy();
});
CodePudding user response:
You can do something along the lines of this
const data = {
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
}
const dataExpected = {
model: 'string',
type: 'string',
name: 'string',
}
const {randomNumber, ...randomNumOmittedData} = data;
expect(randomNumOmittedData).toEqual(dataExpected);