Installations
npm install snapshot-diff
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=14
Score
71
Supply Chain
51.5
Quality
74.1
Maintenance
100
Vulnerability
97.3
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
thymikee
Download Statistics
Total Downloads
18,212,257
Last Day
10,500
Last Week
41,672
Last Month
218,939
Last Year
3,461,044
GitHub Statistics
604 Stars
225 Commits
22 Forks
5 Watching
11 Branches
21 Contributors
Bundle Size
1.56 MB
Minified
407.90 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.10.0
Package Id
snapshot-diff@0.10.0
Unpacked Size
15.90 kB
Size
6.15 kB
File Count
23
Total Downloads
Cumulative downloads
Total Downloads
18,212,257
Last day
-11.1%
10,500
Compared to previous day
Last week
-28.4%
41,672
Compared to previous week
Last month
-14.3%
218,939
Compared to previous month
Last year
-20.7%
3,461,044
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
snapshot-diff
Diffing snapshot utility for Jest. Takes two values, and return their difference as a string, ready to be snapshotted with toMatchSnapshot()
.
Especially helpful when testing the difference between different React component states.
Installation
1yarn add --dev snapshot-diff
Usage
With default jest matcher
1const snapshotDiff = require('snapshot-diff'); 2 3test('snapshot difference between 2 strings', () => { 4 expect(snapshotDiff(a, b)).toMatchSnapshot(); 5}); 6 7const React = require('react'); 8const Component = require('./Component'); 9 10test('snapshot difference between 2 React components state', () => { 11 expect( 12 snapshotDiff(<Component test="say" />, <Component test="my name" />) 13 ).toMatchSnapshot(); 14});
With custom matcher
1const { toMatchDiffSnapshot } = require('snapshot-diff'); 2 3expect.extend({ toMatchDiffSnapshot }); 4 5test('snapshot difference between 2 strings', () => { 6 expect(a).toMatchDiffSnapshot(b); 7}); 8 9const React = require('react'); 10const Component = require('./Component'); 11 12test('snapshot difference between 2 React components state', () => { 13 expect(<Component test="say" />).toMatchDiffSnapshot( 14 <Component test="my name" /> 15 ); 16});
... alternatively import it once, for instance in your tests setup file:
1require('snapshot-diff/extend-expect');
Produced snapshot:
1exports[`snapshot difference between 2 strings 1`] = ` 2"- First value 3+ Second value 4 5 6- abcx 7+ abcy 8 " 9`; 10 11exports[`snapshot difference between 2 React components state 1`] = ` 12"- <Component test=\\"say\\" /> 13+ <Component test=\\"my name\\" /> 14 15@@ -27,11 +27,11 @@ 16 <span /> 17 <span /> 18 <span /> 19 <span /> 20 <span> 21- say 22+ my name 23 </span> 24 <span /> 25 <span /> 26 <span /> 27 <span />" 28`;
Custom serializers
By default, snapshot-diff
uses a built in React serializer based on react-test-renderer
. The
serializers used can be set by calling
setSerializers
with an array of serializers to use. The order of serializers in this array may be important to you as
serializers are tested in order until a match is found.
setSerializers
can be used to add new serializers for unsupported data types, or to set a different serializer
for React components. If you want to keep the default React serializer in place, don't forget to add the default
serializers to your list of serializers!
Adding a new custom serializer
1const snapshotDiff = require('snapshot-diff');
2const myCustomSerializer = require('./my-custom-serializer');
3
4snapshotDiff.setSerializers([
5 ...snapshotDiff.defaultSerializers, // use default React serializer - add this if you want to serialise React components!
6 myCustomSerializer
7]);
Serializing React components with a different serializer
You can replace the default React serializer by omitting it from the serializer list. The following uses enzymes to-json serializer instead:
1const snapshotDiff = require('snapshot-diff'); 2const enzymeToJson = require('enzyme-to-json/serializer'); 3const myCustomSerializer = require('./my-custom-serializer'); 4 5snapshotDiff.setSerializers([ 6 enzymeToJson, // using enzymes serializer instead 7 myCustomSerializer 8]);
Snapshot serializer
By default Jest adds extra quotes around strings so it makes diff snapshots of objects too noisy.
To fix this – snapshot-diff
comes with custom serializer, which you can add directly in your tests or in setupFiles
script:
1const snapshotDiff = require('snapshot-diff'); 2 3expect.addSnapshotSerializer(snapshotDiff.getSnapshotDiffSerializer()); 4 5test('snapshot difference between 2 objects', () => { 6 expect(snapshotDiff({ foo: 'bar' }, { foo: 'baz' })).toMatchSnapshot(); 7});
...or add it globally to your jest config:
1// jest.config.js 2module.exports = { 3 snapshotSerializers: [ 4 require.resolve('snapshot-diff/serializer.js'), 5 ], 6};
API
1type Options = {
2 expand?: boolean,
3 colors?: boolean,
4 contextLines?: number
5};
6
7// default export
8snapshotDiff(valueA: any, valueB: any, options?: Options) => string
9// custom matcher
10expect(valueA: any).toMatchDiffSnapshot(valueB: any, options?: Options, testName?: string) => void
Options
expand: boolean
(default:false
) – expand the diff, so the whole information is preservedcolors: boolean
(default:false
) – preserve color information from Jest diffcontextLines: number
(default: 5) - number of context lines to be shown at the beginning and at the end of a snapshotstablePatchmarks: boolean
(default:false
) - prevent line number patch marks from appearing in diffs. This can be helpful when diffs are breaking only because of the patch marks. Changes@@ -1,1 +1,2 @@
to@@ --- --- @@
.aAnnotation: string
(default:'First Value'
) - the annotation indicating from which serialization the-
lines arebAnnotation: string
(default:'Second Value'
) - the annotation indicating from which serialization the+
lines are
Project is MIT-licensed. Pull Requests welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/jest-community/snapshot-diff/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/jest-community/snapshot-diff/nodejs.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.5
/10
Last Scanned on 2025-01-27
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 MoreOther packages similar to snapshot-diff
@frsource/cypress-plugin-visual-regression-diff
Perform visual regression test with a nice GUI as help. 💅 Only for Cypress!
enzyme-snapshot-diff
snapshot-diff for enzyme
diffable-html
Opinionated HTML formatter focused towards making HTML diffs readable.
snapshot-diff-serializer
A serializer that lets you generate snapshot diffs between two values