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-better-shallow-deep-equal

v1.1.1

Published

Chai shallowDeepEqual replacement that wraps Unexpected

Downloads

6,303

Readme

(Better) Chai Shallow Deep Equal plugin

This module provides a drop-in replacement shallowDeepEqual assertion for chai that uses strict semantics and an intuitive output diff.

NPM version Build Status Coverage Status

Under the hood the library wraps the Unexpected library, specifically making use of the structural "to satisfy" assertion.

Use

Once installed the plugin can be simply imported and used as a plugin:

const chai = require("chai");
const expect = chai.expect;

chai.use(require("chai-better-shallow-deep-equal"));

An additional .shallowDeepEqual() assertion is then available for use and on error an informative diff will be printed:

expect({ foo: true, bar: 0 }).to.shallowDeepEqual({ foo: true, bar: 1 });
expected { foo: true, bar: 0 } to satisfy { foo: true, bar: 1 }

{
  foo: true,
  bar: 0 // should equal 1
}

The assertion works with all three chai APIs: expect, should and assert.

Support for ES6 types

The plugin has support for structurally comparing both Map and Set objects:

expect(
  new Map([
    ["foo", 1],
    ["bar", false]
  ])
).to.shallowDeepEqual(
  new Map([
    ["foo", 1],
    ["bar", true]
  ])
);
expected new Map[ ['foo', 1], ['bar', false] ])
to satisfy new Map[ ['foo', 1], ['bar', true] ])

new Map[
  ['foo', 1,]
  ['bar',
    false // should equal true
  ]
])
expect(new Set(["foo", "baz"])).to.shallowDeepEqual(
  new Set(["foo", "bar"])
);
expected new Set([ 'foo', 'baz' ]) to satisfy new Set([ 'foo', 'bar' ])

new Set([
  'foo',
  'baz' // should be removed
  // missing 'bar'
])

Customisation

Adding types

Sometimes it can be beneficial to identify certain types within the test suite - perhaps to customise their display or to treat them otherwise differently. This can be achieved by using the addType() API:

const chaiBetterShallowDeepEqual = require("chai-better-shallow-deep-equal");

chaiBetterShallowDeepEqual.addType({
  name: "CustomDate",
  base: "date",
  identify: obj => obj && obj._isCustomDate
});

In the example above, we are trying to single out certain objects that occur within a hypthetical test suite that use custom dates by checking whether they have an "isCustomDate" property.

Given our definition of the identify() method above, when the plugin encounters such objects it will think of them as CustomDate and be aware that they extend the behavior of the builtin date type.

This API accepts the same options as the Unexpected addType() method. Please consult the link for more detailed description.

Custom Matching

With the availablity of custom types are in the picture, one common desire is to allow customising the way those identified types are matched.

By default only alike types are compared, but suppose that within our tests we want to allow comparing any CustomDate object against a, ISO time string.

Let's stick with the exmaple from our earlier hypothetical - we can define allowing the comparison using the addMatch() API:

chaiBetterShallowDeepEqual.addMatch({
  leftType: "CustomDate",
  rightType: "string",
  handler: (lhs, rhs) => [lhs.toISOString(), rhs]
});

What we've defined here is when we see a CustomDate being compared to a string, to instead first convert it to an ISO string and then do the comparison. In the test suite, the effect is to allow expecations to be defined in a way that is much more easily read:

const fooDate = new Date(1583947016326);

expect({ fooDate }).to.shallowDeepEqual({
  fooDate: "2020-03-11T17:16:56.326Z"
});
expected { fooDate: new Date('2020-03-11T17:16:56.326Z') }
to satisfy { fooDate: '2020-03-11T17:16:56.326Z' }

{
  fooDate: new Date('2020-03-11T17:16:56.326Z') // should equal '2020-03-11T17:16:56.326Z'
}