1.4.1 â€ĸ Published 11 months ago

chai-recursive-match v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

chai-recursive-match

npm package Build Status Issues Semantic Release

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)
1.4.1

11 months ago

1.4.0

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

1.2.0

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago