1.4.1 âĸ Published 12 months ago
chai-recursive-match v1.4.1
chai-recursive-match
Easily perform recursive comparisons in your Chai assertions
Key features:
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.
Install
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.
Usage
import { use } from 'chai';
import { chaiRecursive } from 'chai-recursive-match';
use(chaiRecursive);
API
Recursively equality
An object or an array is checked against a pattern.
See types.ts
for pattern's type definition.
A simple example:
expect({ foo: { bar: 'baz' } }).to.recursive.equal({
foo: to => to.recursive.equal({ bar: to => to.be.a('string') }),
});
An array example:
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/) }) },
]);
A complete example:
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') }),
});
Check if an array has a member
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') });
Check if an array has members
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') },
]);
With negation:
expect({ foo: { bar: 'baz' } }).to.not.recursive.equal({
foo: to => to.recursive.equal({ bar: to => to.be.a('number') }),
});
Recursively inclusion
An object example:
expect({ foo: { bar: 'baz' }, num: 123 }).to.recursive.include({
num: to => to.be.gt(100),
});
An array example:
expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.recursive.include({
foo: to => to.recursive.equal({ bar: to.match(/^foo/) }),
});
Check if an array includes members
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') }]);
With negation:
expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.not.recursive.include({
foo: to => to.recursive.equal({ bar: to.match(/^baz/) }),
});
TBD
- đ§ Show diff in error message
- đ§ Support
chai.assert
interface - đ§ Support more array methods (e.g.
to.recursive.have.ordered.members
)