@@ -7,6 +7,7 @@ Diffing snapshot utility for Jest. Takes two values, and return their difference
77Especially helpful when testing the difference between different React component states.
88
99## Installation
10+
1011``` bash
1112yarn add --dev snapshot-diff
1213```
@@ -18,7 +19,7 @@ yarn add --dev snapshot-diff
1819``` js
1920const snapshotDiff = require (' snapshot-diff' );
2021
21- test (' snapshot difference between 2 strings' , () => {
22+ test (' snapshot difference between 2 strings' , () => {
2223 expect (snapshotDiff (a, b)).toMatchSnapshot ();
2324});
2425
@@ -27,10 +28,7 @@ const Component = require('./Component');
2728
2829test (' snapshot difference between 2 React components state' , () => {
2930 expect (
30- snapshotDiff (
31- < Component test= " say" / > ,
32- < Component test= " my name" / >
33- )
31+ snapshotDiff (< Component test= " say" / > , < Component test= " my name" / > )
3432 ).toMatchSnapshot ();
3533});
3634```
@@ -42,24 +40,20 @@ const { toMatchDiffSnapshot } = require('snapshot-diff');
4240
4341expect .extend ({ toMatchDiffSnapshot });
4442
45- test (' snapshot difference between 2 strings' , () => {
43+ test (' snapshot difference between 2 strings' , () => {
4644 expect (a).toMatchDiffSnapshot (b);
4745});
4846
4947const React = require (' react' );
5048const Component = require (' ./Component' );
5149
52- test (' snapshot difference between 2 React components state' , () => {
53- expect (
54- < Component test= " say" / >
55- ).toMatchDiffSnapshot (
50+ test (' snapshot difference between 2 React components state' , () => {
51+ expect (< Component test= " say" / > ).toMatchDiffSnapshot (
5652 < Component test= " my name" / >
57- );
53+ );
5854});
5955```
6056
61-
62-
6357Produced snapshot:
6458
6559``` diff
@@ -95,26 +89,28 @@ exports[`snapshot difference between 2 React components state 1`] = `
9589
9690## Snapshot serializer
9791
98- By default Jest adds extra quotes around strings so it makes diff snapshots of objects too noisy.
92+ By default Jest adds extra quotes around strings so it makes diff snapshots of objects too noisy.
9993To fix this – ` snapshot-diff ` comes with custom serializer, which you can add directly in your tests or in ` setupFiles ` script:
10094
10195``` js
10296const snapshotDiff = require (' snapshot-diff' );
10397
10498expect .addSnapshotSerializer (snapshotDiff .getSnapshotDiffSerializer ());
10599
106- test (' snapshot difference between 2 objects' , () => {
100+ test (' snapshot difference between 2 objects' , () => {
107101 expect (snapshotDiff ({ foo: ' bar' }, { foo: ' baz' })).toMatchSnapshot ();
108102});
109103```
110104
111105...or add it globally to your jest config:
112106
113107``` json
114- "jest" : {
115- "snapshotSerializers" : [
116- " <rootDir>/node_modules/snapshot-diff/serializer.js"
117- ]
108+ {
109+ "jest" : {
110+ "snapshotSerializers" : [
111+ " <rootDir>/node_modules/snapshot-diff/serializer.js"
112+ ]
113+ }
118114}
119115```
120116
@@ -130,16 +126,16 @@ type Options = {
130126// default export
131127snapshotDiff (valueA: any, valueB: any, options?: Options) => string
132128// custom matcher
133- expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options) => void
129+ expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options, testName ?: string ) => void
134130```
135131
136132### Options
137- * ` expand: boolean ` (default: ` false ` ) – expand the diff, so the whole information is preserved
138- * ` colors: boolean ` (default: ` false ` ) – preserve color information from Jest diff
139- * ` contextLines: number ` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
140- * ` aAnnotation: string ` (default: ` 'First Value' ` ) - the annotation indicating from which serialization the ` - ` lines are
141- * ` bAnnotation: string ` (default: ` 'Second Value' ` ) - the annotation indicating from which serialization the ` + ` lines are
142133
134+ - ` expand: boolean ` (default: ` false ` ) – expand the diff, so the whole information is preserved
135+ - ` colors: boolean ` (default: ` false ` ) – preserve color information from Jest diff
136+ - ` contextLines: number ` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
137+ - ` aAnnotation: string ` (default: ` 'First Value' ` ) - the annotation indicating from which serialization the ` - ` lines are
138+ - ` bAnnotation: string ` (default: ` 'Second Value' ` ) - the annotation indicating from which serialization the ` + ` lines are
143139
144140---
145141
0 commit comments