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-snapshot-matcher

v2.0.3

Published

A Snapshot testing library designed for Mocha and Chai users

Downloads

2,632

Readme

chai-snapshot-matcher is a node library developed to offer the jest snapshot testing features to mocha and chai users.

Why Snapshot Testing

Snapshot tests are a very useful tool whenever you want to make sure your API our UI does not change unexpectedly.

A typical snapshot test case for an API execute a http request, takes a snapshot to the requested response and then compares it to a reference snapshot file stored alongside the test. The test will fail if the two snapshots do not match: either the change is unexpected or the reference snapshot needs to be updated to the new version.

Installation

Using npm:

npm install chai-snapshot-matcher

Using yarn:

yarn add chai-snapshot-matcher

Usage

There are 2 ways to run chai-snapshot-matcher in your test script:

  • Import the chai-snapshot-matcher package on the tests scripts where the matchSnapshot or matchSpecificSnapshot will be used:
require('chai-snapshot-matcher');
  • Add the --require argument to your test script/command:
mocha --require chai-snapshot-matcher

.matchSnapshot(this [, config])

matchSnapshot is a customized chai matcher that creates a snapshot file on the first test run. On subsequent test runs, chai will compare the object that is being expected with the previous snapshot. If they match, the test will pass. If they don't match, the test will fail. If tests are failing due to a change decision implementation (and not a bug), snapshots needs to be updated (check how on Update Snapshots chapter).

How it works

  • matchSnapshot creates a new __snapshots__ folder at the same level of the test file, with a snapshot file inside it.
  • the snapshot file will use the test file name but the extension .test.js will be replaced by .snap (e.g. post-request.test.js will originate the post-request.snap).
  • all snapshots that belong to the same test file will be saved in the same snapshot file.
  • by default, the snapshot and test names will be the same, i. e. the joining of the it and all describe parent titles.

Example

Test file: (path: ./tests/myFirstSnapshotTest.test.js)

// myFirstSnapshotTest.test.js
describe('chai-snapshot-matcher', function () {
  describe('- matchSnapshot -', function () {
    it('check string', function () {
      const packageName = 'chai-snapshot-matcher';

      expect(name).to.matchSnapshot(this);
    });
  });
});

Snapshot file: (path: ./tests/__snapshots__/myFirstSnapshotTest.snap)

exports["chai-snapshot-matcher - matchSapshot - check string"] = "chai-snapshot-matcher";

Aditional Features

Extra functionalities are available through the config attribute (optional argument). The following table describes each field of the config attribute:

| Field | Type | Description | | :------------: | :----: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | hint | String | customize the snapshot name by including a hint at the end | | name | String | rename a snapshot by giving it a completely new name (it and describe titles will be ignored) | | folder | String | change the path where the snapshot file is saved. Instead of saving all the snapshots inside the __snapshots__ folder, one can organize the snapshots files inside nested folders. This could be useful for multi environment testing | | snapshotPath | String | set a completely new path (inside or outside the repositiory) to the snapshot files. The _snapshotPath_only works with the absolute path | | ignore | Array | ignore a certain set of fields from an object. This can be useful for checking an entire object and ignoring only the mutable (e.g. time-changing) fields |

NOTE:

- hint attribute will be ignored if the name attribute is used.

- folder attribute will be ignored if the snapshotPath attribute is used.

- Two snaps with the same name within the same snapshot file cannot exist. Therefore, if one wants to use the matchSnapshot twice inside a single it the hint, name, folder or snapshotPath argument will have to be used.

Example

Test file: (path: ./tests/mySecondSnapshotTest.test.js)

describe('chai-snapshot-matcher', function () {
  describe('- matchSnapshot (features) -', function () {
    it('new features examples', function () {
      const stringExample = 'Hello World';

      const objectExample = {
        id: '73as268f90sn.a4fju2',
        current_date: '2021-02-15T18:58Z',
      };

      // using hint argument
      expect(example).to.matchSnapshot(this, { hint: '(hint)' });
      // using name argument
      expect(example).to.matchSnapshot(this, { name: 'snapshot with a specific name' });
      // using folder argument
      expect(example).to.matchSnapshot(this, { folder: 'Examples' });
      // using snapshotPath argumentc
      expect(example).to.matchSnapshot(this, { snapshotPath: '/Users/my.user/Downloads/MySnapshots/' });
      // using ignore argument
      expect(example).to.matchSnapshot(this, { ignore: ['current_date'] });
    });
  });
});

Snapshot files:

  • path: ./tests/__snapshots__/mySecondSnapshotTest.snap
exports["chai-snapshot-matcher - matchSnapshot (features) - new features examples (hint)"] = "Hello World";

exports["snapshot with a specific name"] = "Hello World";

exports["chai-snapshot-matcher - matchSnapshot (features) - new features examples"] = { "id": "73as268f90sn.a4fju2"}
  • path: ./tests/__snapshots__/Examples/mySecondSnapshotTest.snap
exports["chai-snapshot-matcher - matchSnapshot (features) - new features examples"] = "Hello World";
  • path: /Users/my.user/Downloads/MySnapshots/mySecondSnapshotTest.snap
exports["chai-snapshot-matcher - matchSnapshot (features) - new features examples"] = "Hello World";

Update Snapshots

Add the --update argument when running mocha to update the snapshots of the tests that are failing:

mocha --update

NOTE:

- Tests or snapshots that were not executed will not be updated.

- If a test or snapshot name has been changed, a new snapshot will be created. When running the tests using the --update flag, only the new ones will be updated.

Recommendations

  • Arrow functions must not be used in tests, otherwise this test context will not be passed to the matchSnapshot and matchSpecificSnapshot.
  • The snapshot files should be committed along with the code, and reviewed as part of the code reviewing process.
  • When running tests on a Continuous Integration (CI) system --ci flag should be included. This will prevent the test to run in a CI environment when there are missing snapshot files.
  • Snapshots are a fantastic tool for identifying unexpected interface changes within the application – whether that interface is an API response, UI, logs, or error messages. Use it well!

Boilerplate project

Still not sure how to use the match-snapshot-matcher? Check this boilerplate project where you can find several examples.

License

MIT