
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
chai-subset
Advanced tools
"containSubset" object properties matcher for Chai assertion library
npm install --save-dev chai-subset
common.js
var chai = require('chai');
var chaiSubset = require('chai-subset');
chai.use(chaiSubset);
in your spec.js
var obj = {
a: 'b',
c: 'd',
e: {
foo: 'bar',
baz: {
qux: 'quux'
}
}
};
expect(obj).to.containSubset({
a: 'b',
e: {
baz: {
qux: 'quux'
}
}
});
// or using a compare function
expect(obj).containSubset({
a: (expectedValue) => expectedValue,
c: (expectedValue) => expectedValue === 'd'
})
// or with 'not'
expect(obj).to.not.containSubset({
g: 'whatever'
});
Also works good with arrays and should interface
var list = [{a: 'a', b: 'b'}, {v: 'f', d: {z: 'g'}}];
list.should.containSubset([{a:'a'}]); //Assertion error is not thrown
list.should.containSubset([{a:'a', b: 'b'}]); //Assertion error is not thrown
list.should.containSubset([{a:'a', b: 'bd'}]);
/*throws
AssertionError: expected
[
{
"a": "a",
"b": "b"
},
{
"v": "f",
"d": {
"z": "g"
}
}
]
to contain subset
[ { a: 'a', b: 'bd' } ]
*/
and with assert interface
assert.containSubset({a: 1, b: 2}, {a: 1});
chai-things is a Chai plugin that provides additional assertions for arrays. It allows you to assert that all or some items in an array meet certain conditions. Compared to chai-subset, chai-things is more focused on array-specific assertions rather than general object subset matching.
chai-as-promised is a Chai plugin that extends Chai with assertions about promises. It allows you to assert that a promise will be fulfilled or rejected with a specific value. While it doesn't provide subset matching, it complements chai-subset by adding promise-specific assertions.
chai-json-schema is a Chai plugin that allows you to validate JSON objects against a JSON schema. It provides a way to assert that an object conforms to a specific schema, which can be more powerful than subset matching when you need to validate complex structures. Compared to chai-subset, chai-json-schema offers more comprehensive validation capabilities.
FAQs
Object properties matcher for Chai
We found that chai-subset demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.