What is chai-subset?
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.
What are chai-subset's main functionalities?
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 } } });
Other packages similar to chai-subset
chai-things
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
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
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.
chai-subset
"containSubset" object properties matcher for Chai assertion library
Installation
npm install --save-dev chai-subset
Usage
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'
}
}
});
expect(obj).containSubset({
a: (expectedValue) => expectedValue,
c: (expectedValue) => expectedValue === 'd'
})
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'}]);
list.should.containSubset([{a:'a', b: 'b'}]);
list.should.containSubset([{a:'a', b: 'bd'}]);
and with assert
interface
assert.containSubset({a: 1, b: 2}, {a: 1});