@@ -7,6 +7,7 @@ Diffing snapshot utility for Jest. Takes two values, and return their difference
7
7
Especially helpful when testing the difference between different React component states.
8
8
9
9
## Installation
10
+
10
11
``` bash
11
12
yarn add --dev snapshot-diff
12
13
```
@@ -18,7 +19,7 @@ yarn add --dev snapshot-diff
18
19
``` js
19
20
const snapshotDiff = require (' snapshot-diff' );
20
21
21
- test (' snapshot difference between 2 strings' , () => {
22
+ test (' snapshot difference between 2 strings' , () => {
22
23
expect (snapshotDiff (a, b)).toMatchSnapshot ();
23
24
});
24
25
@@ -27,10 +28,7 @@ const Component = require('./Component');
27
28
28
29
test (' snapshot difference between 2 React components state' , () => {
29
30
expect (
30
- snapshotDiff (
31
- < Component test= " say" / > ,
32
- < Component test= " my name" / >
33
- )
31
+ snapshotDiff (< Component test= " say" / > , < Component test= " my name" / > )
34
32
).toMatchSnapshot ();
35
33
});
36
34
```
@@ -42,24 +40,20 @@ const { toMatchDiffSnapshot } = require('snapshot-diff');
42
40
43
41
expect .extend ({ toMatchDiffSnapshot });
44
42
45
- test (' snapshot difference between 2 strings' , () => {
43
+ test (' snapshot difference between 2 strings' , () => {
46
44
expect (a).toMatchDiffSnapshot (b);
47
45
});
48
46
49
47
const React = require (' react' );
50
48
const Component = require (' ./Component' );
51
49
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 (
56
52
< Component test= " my name" / >
57
- );
53
+ );
58
54
});
59
55
```
60
56
61
-
62
-
63
57
Produced snapshot:
64
58
65
59
``` diff
@@ -95,26 +89,28 @@ exports[`snapshot difference between 2 React components state 1`] = `
95
89
96
90
## Snapshot serializer
97
91
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.
99
93
To fix this – ` snapshot-diff ` comes with custom serializer, which you can add directly in your tests or in ` setupFiles ` script:
100
94
101
95
``` js
102
96
const snapshotDiff = require (' snapshot-diff' );
103
97
104
98
expect .addSnapshotSerializer (snapshotDiff .getSnapshotDiffSerializer ());
105
99
106
- test (' snapshot difference between 2 objects' , () => {
100
+ test (' snapshot difference between 2 objects' , () => {
107
101
expect (snapshotDiff ({ foo: ' bar' }, { foo: ' baz' })).toMatchSnapshot ();
108
102
});
109
103
```
110
104
111
105
...or add it globally to your jest config:
112
106
113
107
``` 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
+ }
118
114
}
119
115
```
120
116
@@ -130,16 +126,16 @@ type Options = {
130
126
// default export
131
127
snapshotDiff (valueA: any, valueB: any, options?: Options) => string
132
128
// custom matcher
133
- expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options) => void
129
+ expect (valueA: any).toMatchDiffSnapshot (valueB: any, options?: Options, testName ?: string ) => void
134
130
```
135
131
136
132
### 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
142
133
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
143
139
144
140
---
145
141
0 commit comments