Gathering detailed insights and metrics for chai-jest-snapshot
Gathering detailed insights and metrics for chai-jest-snapshot
Gathering detailed insights and metrics for chai-jest-snapshot
Gathering detailed insights and metrics for chai-jest-snapshot
Chai assertion that provides Jest's snapshot testing
npm install chai-jest-snapshot
Typescript
Module System
Node Version
NPM Version
97.8
Supply Chain
95.2
Quality
75.3
Maintenance
100
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
7,896,681
Last Day
1,095
Last Week
30,375
Last Month
137,120
Last Year
1,052,823
MIT License
103 Stars
64 Commits
17 Forks
4 Watchers
2 Branches
9 Contributors
Updated on Jul 24, 2024
Minified
Minified + Gzipped
Latest Version
2.0.0
Package Id
chai-jest-snapshot@2.0.0
Size
5.11 kB
NPM Version
5.3.0
Node Version
8.5.0
Published on
Nov 07, 2017
Cumulative downloads
Total Downloads
Last Day
-35.9%
1,095
Compared to previous day
Last Week
-16.4%
30,375
Compared to previous week
Last Month
-4.9%
137,120
Compared to previous month
Last Year
-25.5%
1,052,823
Compared to previous year
2
1
Chai assertion for jest-snapshot. See Jest 14.0: React Snapshot Testing for background knowledge about snapshot testing.
On the command line:
$ npm install --save-dev chai-jest-snapshot
There are four different ways to use chai-jest-snapshot.
If you are using mocha as your test runner, it is recommended to use chai-jest-snapshot in "mocha configuration mode".
Note: do not use an arrow function for the beforeEach
as these will not receive the correct this
value provided by mocha.
In your test setup file:
1import chai from "chai"; 2import chaiJestSnapshot from "chai-jest-snapshot"; 3 4chai.use(chaiJestSnapshot); 5 6before(function() { 7 chaiJestSnapshot.resetSnapshotRegistry(); 8}); 9 10beforeEach(function() { 11 chaiJestSnapshot.configureUsingMochaContext(this); 12});
In your spec file(s) (as an example):
1import React from "react"; 2import renderer from "react-test-renderer"; 3import { expect } from "chai"; 4import Link from "./Link"; 5 6describe("Link", function() { 7 it("renders correctly", () => { 8 const tree = renderer.create( 9 <Link page="http://www.facebook.com">Facebook</Link> 10 ).toJSON(); 11 expect(tree).to.matchSnapshot(); 12 }); 13});
This will automatically write snapshots to a file with the same name as your test file, with .snap
added to the end.
This will also choose snapshot names based on the test name, adding a number to the end based on the number of times matchSnapshot
was called in each test (similar to jest).
In this mode, to update a single snapshot, you can pass true
as an argument to matchSnapshot
:
1expect(tree).to.matchSnapshot(true);
If you want to update all snapshots without adding true
to each one, set the environment variable CHAI_JEST_SNAPSHOT_UPDATE_ALL
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test 4# fish 5$ env CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test
This behaves similarly to running jest -u
.
If you want tests to fail when a snapshot is missing (instead of writing a new one), set the environment variable CI
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CI=true npm test 4# fish 5$ env CI=true npm test
This behaves similarly to running jest --ci
.
If you are using Jest, but prefer Chai assertions, you don’t have anything to configure except loading the plugin itself: the matchSnapshot
Chai assertion will automatically delegate to Jest’s built-in snapshot capability, so all usual options, settings, CLI flags, method arguments, etc. will work out of the box.
1import chai from "chai"; 2import chaiJestSnapshot from "chai-jest-snapshot"; 3 4chai.use(chaiJestSnapshot);
If you are using neither mocha nor Jest as your test runner, it is recommended to use chai-jest-snapshot in "framework-agnostic configuration mode".
In your test setup file:
1import chai from "chai"; 2import chaiJestSnapshot from "chai-jest-snapshot"; 3 4chai.use(chaiJestSnapshot); 5 6before(function() { 7 // In order for watch mode to work correctly, the snapshot registry needs to 8 // be reset at the beginning of each suite run. In mocha, `before` callbacks 9 // are called before the whole suite runs, but in other test runners you may 10 // need to run this somewhere else; for example, in jasmine, you'd put it in a 11 // `beforeAll` instead of `before`. 12 chaiJestSnapshot.resetSnapshotRegistry(); 13});
In your spec file(s) (as an example):
1import React from "react"; 2import renderer from "react-test-renderer"; 3import { expect } from "chai"; 4import Link from "./Link"; 5 6describe("Link", function() { 7 beforeEach(function() { 8 chaiJestSnapshot.setFilename(__filename + ".snap"); 9 }); 10 11 it("renders correctly", () => { 12 // There may be a way to automate this in your test runner; for example, 13 // getting the test name in the beforeEach callback, or using a custom 14 // reporter to hook into the test lifecycle. 15 chaiJestSnapshot.setTestName("Link renders correctly"); 16 17 const tree = renderer.create( 18 <Link page="http://www.facebook.com">Facebook</Link> 19 ).toJSON(); 20 expect(tree).to.matchSnapshot(); 21 }); 22});
This will write snapshots to the file name you specify, (in this example, a file with the same name and location as the spec file, but with .snap
added to the end).
This will use whatever snapshot name you specify as a template, adding a number to the end based on the number of times matchSnapshot
was called using the same file name and snapshot name (similar to what jest does).
In this mode, to update a single snapshot, you can pass true
as an argument to matchSnapshot
:
1expect(tree).to.matchSnapshot(true);
If you want to update all snapshots without adding true
to each one, set the environment variable CHAI_JEST_SNAPSHOT_UPDATE_ALL
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test 4# fish 5$ env CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test
This behaves similarly to running jest -u
.
If you want tests to fail when a snapshot is missing (instead of writing a new one), set the environment variable CI
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CI=true npm test 4# fish 5$ env CI=true npm test
This behaves similarly to running jest --ci
.
If Mocha Configuration Mode or Framework-agnostic Configuration Mode do not satisfy your needs, you can use "manual mode".
In your test setup file:
1import chai from "chai"; 2import chaiJestSnapshot from "chai-jest-snapshot"; 3 4chai.use(chaiJestSnapshot);
In your spec file(s) (as an example):
1import React from "react"; 2import renderer from "react-test-renderer"; 3import { expect } from "chai"; 4import Link from "./Link"; 5 6describe("Link", function() { 7 it("renders correctly", () => { 8 const tree = renderer.create( 9 <Link page="http://www.facebook.com">Facebook</Link> 10 ).toJSON(); 11 12 let snapshotFilename = __filename + ".snap"; 13 let snapshotName = "Link renders correctly"; 14 expect(tree).to.matchSnapshot(snapshotFilename, snapshotName); 15 }); 16});
This will write snapshots to the file name you specify, (in this example, a file with the same name and location as the spec file, but with .snap
added to the end).
This will use whatever snapshot name you specify as the snapshot name. NOTE: unlike other modes, this mode does not add a number to the end of the snapshot name.
In this mode, to update a single snapshot, you can pass true
as an extra, third argument to matchSnapshot
:
1expect(tree).to.matchSnapshot(snapshotFilename, snapshotName, true);
If you want to update all snapshots without adding true
to each one, set the environment variable CHAI_JEST_SNAPSHOT_UPDATE_ALL
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test 4# fish 5$ env CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test
This behaves similarly to running jest -u
.
If you want tests to fail when a snapshot is missing (instead of writing a new one), set the environment variable CI
to "true":
1# assuming `npm test` runs your tests: 2# sh/bash/zsh 3$ CI=true npm test 4# fish 5$ env CI=true npm test
This behaves similarly to running jest --ci
.
__filename
or __dirname
in your snapshot file names, and compile your tests using babel, you will probably want to use babel-plugin-transform-dirname-filename to ensure your snapshots end up in your source directory instead of the directory where your tests were built (ie dist
or build
).$ npm install
$ npm test
Pull Requests and Issues welcome
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/18 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
56 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More