[英]jest unit test javascript
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.我有一个单元测试,我正在尝试检查该字段,但在开玩笑的文档中找不到任何东西来忽略这种情况下的一个值,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.
我想测试以确保预期的数组具有所有字段,但忽略这些值。
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();
});
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.