我想在 jest
中断言数组包含具有某些属性的对象,例如:
[
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },
{ id: 3 } // should throw assertion error
]
在 chai
和 chai-things
我会用 should.all.have
来做它是非常自我描述的:result.should.all.have.property('id');
result.should.all.have.property('name');
在 jest
中是否有类似的方法来实现此目的? ?
最佳答案
您可以使用 toHaveProperty
来自杰斯特。
这是文档 https://jestjs.io/docs/en/expect#tohavepropertykeypath-value
const elements = [
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },
{ id: 3 } // should throw assertion error
]
elements.forEach(element => {
expect(element).toHaveProperty('id')
expect(element).toHaveProperty('name')
});
关于jestjs - Jest - 如何断言数组中的所有项目都是对象并包含某些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62563807/