node.js - Using Jest to test node.js code, how do you test for the absence of a property
问题描述
What matcher would I use in Jest to test for the absence of a property on an object.
My MUT returns an object, say {userName: 'Joe'}
.
How do I test that the object does NOT contain a property named password
?
Such that this result would fail the test: {userName: 'Joe', password: 'pwd'}
.
I've thought of .toBeUndefined()
, but if the password property is there, and just contains an undefined
value, the test would yield a false pass - such a case should fail, because the password
property is there (even though it's value is undefined
).
解决方案
How about toHaveProperty
?
const obj = { userName: 'Joe', password: 'pwd' };
it('obj should not have password property', () => {
expect(obj).not.toHaveProperty('password');
});
推荐阅读
- python-3.x - Python 3 discord bot 字符串到字节的转换
- php - 更改 WooCommerece 单品页面布局
- c# - 队列项结果返回给调用 api 调用的成员
- javascript - 从引导模式中的“是”按钮调用 JS 警报
- resharper - 如何通过 resharper 禁用评论中的错字
- amazon-web-services - 使用 SDK 将 SQS 触发器添加到 Lambda
- kotlin - 如何在 Kotlin CoRoutine 中编写 rx concatArrayEager 等效项?
- r - 在R中提取文本字符串
- spring - ehcache RMI 复制:java.rmi.NoSuchObjectException:表中没有这样的对象
- ruby-on-rails - Bootstrap 3下拉菜单在生产模式rails 5.2中不起作用