Shawn de Wet
Shawn de Wet

Reputation: 5976

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

Upvotes: 0

Views: 287

Answers (1)

Dario
Dario

Reputation: 6280

How about toHaveProperty ?

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

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

Upvotes: 2

Related Questions