@@ -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,15 +89,15 @@ 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```
@@ -130,16 +124,16 @@ type Options = {
130124// default export
131125snapshotDiff (valueA: any, valueB: any, options?: Options) => string
132126// custom matcher
133- expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options) => void
127+ expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options, testName ?: string ) => void
134128```
135129
136130### 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
142131
132+ - ` expand: boolean ` (default: ` false ` ) – expand the diff, so the whole information is preserved
133+ - ` colors: boolean ` (default: ` false ` ) – preserve color information from Jest diff
134+ - ` contextLines: number ` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
135+ - ` aAnnotation: string ` (default: ` 'First Value' ` ) - the annotation indicating from which serialization the ` - ` lines are
136+ - ` bAnnotation: string ` (default: ` 'Second Value' ` ) - the annotation indicating from which serialization the ` + ` lines are
143137
144138---
145139
0 commit comments