Easily perform recursive comparisons in your Chai assertions
This Chai plugin extends Chai's assertion capabilities to allow for seamless recursive comparisons of objects and arrays.
It enables you to write concise and expressive tests for nested structures, ensuring the integrity of your data at every level.
- π Recursive equality: Assert that two objects or arrays are recursively equal, taking into account nested values and their types.
- π¦ Recursive inclusion: Verify that a nested value is present within an object or array, even if it's located deep within the structure.
- π§ Customizable matchers: Use matchers from Chai's rich library to define specific conditions for nested values, such as type checks, string patterns, or numerical ranges.
- βΉοΈ Informative error messages: Receive clear and detailed error messages when assertions fail, pinpointing the exact path of the discrepancy within the nested structure.
Give it a try to enhance your testing experience with Chai.
npm install -D chai-recursive-match
Note: No need to install types for TypeScript separately β they are included.
import { use } from 'chai';
import { chaiRecursive } from 'chai-recursive-match';
use(chaiRecursive);
An object or an array is checked against a pattern.
See types.ts
for pattern's type definition.
expect({ foo: { bar: 'baz' } }).to.recursive.equal({
foo: to => to.recursive.equal({ bar: to => to.be.a('string') }),
});
expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.recursive.equal([
{ foo: to => to.recursive.equal({ bar: to.be.a('string') }) },
{ foo: to => to.recursive.equal({ bar: to.match(/^foo/) }) },
]);
expect({
num1: 1,
num2: 2,
arr1: [1, 2, 3],
arr2: [{ id: 1 }, { id: 2 }],
str1: 'hello 1',
str2: 'hello 2',
obj1: { key: 'a', value: 'A' },
obj2: { key: 'b', value: 'B' },
method1() {},
}).to.recursive.equal({
num1: 1,
num2: to => to.be.gt(1),
arr1: [1, 2, 3],
arr2: to => to.deep.contain({ id: 2 }),
str1: 'hello 1',
str2: to => to.match(/^hello/),
obj1: { key: 'a', value: 'A' },
obj2: to => to.recursive.equal({ key: 'b', value: to => to.be.a('string') }),
});
expect({ foo: { bar: 'baz' } }).to.not.recursive.equal({
foo: to => to.recursive.equal({ bar: to => to.be.a('number') }),
});
This is similar to recursive.include
, but the value is expected to fully match the pattern:
expect([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
]).to.recursive.equal({ id: 1, name: to => to.contain('A') });
This is similar to recursive.have()
with several members to be compared:
expect([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 2, name: 'Carol' },
]).to.recursive.have.members([
{ id: 1, name: to => to.contain('A') },
{ id: 3, name: to => to.contain('C') },
]);
expect({ foo: { bar: 'baz' }, num: 123 }).to.recursive.include({
num: to => to.be.gt(100),
});
expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.recursive.include({
foo: to => to.recursive.equal({ bar: to.match(/^foo/) }),
});
expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.not.recursive.include({
foo: to => to.recursive.equal({ bar: to.match(/^baz/) }),
});
expect([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 2, name: 'Carol' },
]).to.recursive.include.members([
{
name: to => to.contain('A')
},
{
name: to => to.contain('C')
}
]);
- π§ Show diff in error message
- π§ Support
chai.assert
interface - π§ Support more array methods (e.g.
to.recursive.have.ordered.members
)