npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-recursive-match

v1.4.1

Published

A chai plugin that adds recursive deep equality assertions

Downloads

117

Readme

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)