Gathering detailed insights and metrics for snapshot-diff
Gathering detailed insights and metrics for snapshot-diff
Gathering detailed insights and metrics for snapshot-diff
Gathering detailed insights and metrics for snapshot-diff
npm install snapshot-diff
Typescript
Module System
Min. Node Version
JavaScript (100%)
Total Downloads
20,229,143
Last Day
10,475
Last Week
106,553
Last Month
529,087
Last Year
3,657,554
MIT License
606 Stars
225 Commits
23 Forks
4 Watchers
12 Branches
19 Contributors
Updated on Aug 11, 2025
Latest Version
0.10.0
Package Id
snapshot-diff@0.10.0
Unpacked Size
15.90 kB
Size
6.15 kB
File Count
23
Cumulative downloads
Total Downloads
Last Day
-50.8%
10,475
Compared to previous day
Last Week
-8.9%
106,553
Compared to previous week
Last Month
13.2%
529,087
Compared to previous month
Last Year
-2.6%
3,657,554
Compared to previous year
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.
1yarn add --dev snapshot-diff
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});
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`;
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!
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]);
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]);
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};
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
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 areProject is MIT-licensed. Pull Requests welcome!
No vulnerabilities found.
@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