Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
chai-subset
Advanced tools
chai-subset is a plugin for the Chai assertion library that allows you to assert that a JavaScript object contains a subset of properties. This is particularly useful for testing partial matches in objects, arrays, and nested structures.
Object Subset Matching
This feature allows you to check if an object contains a subset of properties. In the example, the object `obj` is checked to see if it contains the properties `a` and `b` with the specified values.
const chai = require('chai');
const chaiSubset = require('chai-subset');
chai.use(chaiSubset);
const expect = chai.expect;
const obj = { a: 1, b: 2, c: 3 };
expect(obj).to.containSubset({ a: 1, b: 2 });
Array Subset Matching
This feature allows you to check if an array contains a subset of objects. In the example, the array `arr` is checked to see if it contains objects with the properties `a` and `b`.
const chai = require('chai');
const chaiSubset = require('chai-subset');
chai.use(chaiSubset);
const expect = chai.expect;
const arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
expect(arr).to.containSubset([{ a: 1 }, { b: 2 }]);
Nested Object Subset Matching
This feature allows you to check if a nested object contains a subset of properties. In the example, the nested object `nestedObj` is checked to see if it contains the nested properties `a.b.c` with the specified value.
const chai = require('chai');
const chaiSubset = require('chai-subset');
chai.use(chaiSubset);
const expect = chai.expect;
const nestedObj = { a: { b: { c: 3 } } };
expect(nestedObj).to.containSubset({ a: { b: { c: 3 } } });
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.
"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});
FAQs
Object properties matcher for Chai
The npm package chai-subset receives a total of 238,495 weekly downloads. As such, chai-subset popularity was classified as popular.
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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.