Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

jestjs - 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about toHaveProperty ?

const obj = { userName: 'Joe', password: 'pwd' };

it('obj should not have password property', () => {
 expect(obj).not.toHaveProperty('password');
});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...