ada*_*ily 15 integration-testing automated-tests jestjs
我想断言jest数组包含具有某些属性的对象,例如:
[
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },
{ id: 3 } // should throw assertion error
]
Run Code Online (Sandbox Code Playgroud)
在chai我chai-things会这样做should.all.have,它是非常自我描述的:
result.should.all.have.property('id');
result.should.all.have.property('name');
Run Code Online (Sandbox Code Playgroud)
有类似的方法可以实现这一点吗jest?
Arn*_*del 13
您可以toHaveProperty从 Jest 中使用。
这是文档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')
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11013 次 |
| 最近记录: |