xercool
xercool

Reputation: 4609

Chai check object contains only keys

I'm trying to find out some chai replacement for only keyword that checks object contains ONLY listed keys.

There are my expectations:

chai.expect({ x: 1, z: 1 }).to.contains.only.keys("x", "y") fails

chai.expect({ }).to.contains.only.keys("x", "y") passes

chai.expect({ x: 1 }).to.contains.only.keys("x", "y") passes

Upvotes: 6

Views: 10521

Answers (2)

Daniel
Daniel

Reputation: 777

Using lodash to filter the object will get you what you want:

expect(_.omit({ x: 1, z: 1 }, ['x','y']), 'invalid properties').to.be.empty; /* fails */
expect(_.omit({ x: 1 }, ['x','y']), 'invalid properties').to.be.empty; /* passes */
expect(_.omit({ }, ['x','y']), 'invalid properties').to.be.empty; /* passes */

Upvotes: 0

Troopers
Troopers

Reputation: 5452

You should use to.have.all.keys

expect({ x: 1 }).to.have.all.keys('x');

Upvotes: 7

Related Questions