Skip to content

Commit 1b1d343

Browse files
avocadowastakenthymikee
authored andcommitted
Add custom serializer for cleaner snapshots (#12)
1 parent f4c65f7 commit 1b1d343

8 files changed

+138
-17
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ yarn add --dev snapshot-diff
1111

1212
## Usage
1313

14-
##### With default jest matcher
14+
#### With default jest matcher
1515

1616
```js
1717
const snapshotDiff = require('snapshot-diff');
@@ -33,7 +33,7 @@ test('snapshot difference between 2 React components state', () => {
3333
});
3434
```
3535

36-
##### With custom matcher
36+
#### With custom matcher
3737

3838
```js
3939
const { toMatchDiffSnapshot } = require('snapshot-diff');
@@ -91,12 +91,38 @@ exports[`snapshot difference between 2 React components state 1`] = `
9191
`;
9292
```
9393

94+
## Snapshot serializer
95+
96+
By default Jest adds extra quotes around strings so it makes diff snapshots of objects too noisy.
97+
To fix this – `snapshot-diff` comes with custom serializer, which you can add directly in your tests or in `setupFiles` script:
98+
99+
```js
100+
const snapshotDiff = require('snapshot-diff');
101+
102+
expect.addSnapshotSerializer(snapshotDiff.getSnapshotDiffSerializer());
103+
104+
test('snapshot difference between 2 objects', () => {
105+
expect(snapshotDiff({ foo: 'bar' }, { foo: 'baz' })).toMatchSnapshot();
106+
});
107+
```
108+
109+
...or add it globally to your jest config:
110+
111+
```json
112+
"jest": {
113+
"snapshotSerializers": [
114+
"<rootDir>/node_modules/snapshot-diff/serializer.js"
115+
]
116+
}
117+
```
118+
94119
## API
95120

96121
```js
97122
type Options = {
98123
expand?: boolean,
99-
colors?: boolean
124+
colors?: boolean,
125+
contextLines?: number
100126
};
101127

102128
// default export
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`serialize array diff output 1`] = `
4+
Snapshot Diff:
5+
- First value
6+
+ Second value
7+
8+
Array [
9+
"foo",
10+
- "bar",
11+
"baz",
12+
+ "quoz",
13+
]
14+
`;
15+
16+
exports[`serialize object diff output 1`] = `
17+
Snapshot Diff:
18+
- First value
19+
+ Second value
20+
21+
Object {
22+
- "foo": "bar",
23+
+ "foo": "baz",
24+
}
25+
`;
26+
27+
exports[`serialize text diff output 1`] = `
28+
Snapshot Diff:
29+
- First value
30+
+ Second value
31+
32+
foo
33+
-bar
34+
baz
35+
+quoz
36+
`;

__tests__/__snapshots__/snapshotDiff.test.js.snap

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`can colorize diff 1`] = `
4-
"- First value
4+
"Snapshot Diff:
5+
- First value
56
+ Second value
67
78
@@ -5,8 +5,9 @@
@@ -17,7 +18,8 @@ exports[`can colorize diff 1`] = `
1718
`;
1819

1920
exports[`can expand diff 1`] = `
20-
"- First value
21+
"Snapshot Diff:
22+
- First value
2123
+ Second value
2224
2325
@@ -36,15 +38,17 @@ exports[`can expand diff 1`] = `
3638
`;
3739

3840
exports[`can use contextLines on diff 1`] = `
39-
"- First value
41+
"Snapshot Diff:
42+
- First value
4043
+ Second value
4144
4245
@@ -10,0 +10,1 @@
4346
+ so"
4447
`;
4548

4649
exports[`can use contextLines with React components 1`] = `
47-
"- <Component test=\\"say\\" />
50+
"Snapshot Diff:
51+
- <Component test=\\"say\\" />
4852
+ <Component test=\\"my name\\" />
4953
5054
@@ -6,1 +6,1 @@
@@ -59,7 +63,8 @@ exports[`can use contextLines with React components 1`] = `
5963
`;
6064

6165
exports[`collapses diffs and strips ansi by default 1`] = `
62-
"- First value
66+
"Snapshot Diff:
67+
- First value
6368
+ Second value
6469
6570
@@ -5,8 +5,9 @@
@@ -75,7 +80,8 @@ exports[`collapses diffs and strips ansi by default 1`] = `
7580
`;
7681

7782
exports[`detects React components 1`] = `
78-
"- <Component test=\\"say\\" />
83+
"Snapshot Diff:
84+
- <Component test=\\"say\\" />
7985
+ <Component test=\\"my name\\" />
8086
8187
@@ -1,14 +1,14 @@
@@ -111,7 +117,8 @@ exports[`detects React components 1`] = `
111117
`;
112118
113119
exports[`diffs short strings 1`] = `
114-
"- First value
120+
"Snapshot Diff:
121+
- First value
115122
+ Second value
116123
117124

__tests__/__snapshots__/toMatchDiffSnapshot.test.js.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`proxies "colors" option(s) 1`] = `
4-
"- First value
4+
"Snapshot Diff:
5+
- First value
56
+ Second value
67
78
@@ -5,8 +5,9 @@
@@ -17,15 +18,17 @@ exports[`proxies "colors" option(s) 1`] = `
1718
`;
1819

1920
exports[`proxies "contextLines" option(s) 1`] = `
20-
"- First value
21+
"Snapshot Diff:
22+
- First value
2123
+ Second value
2224
2325
@@ -10,0 +10,1 @@
2426
+ so"
2527
`;
2628

2729
exports[`proxies "expand" option(s) 1`] = `
28-
"- First value
30+
"Snapshot Diff:
31+
- First value
2932
+ Second value
3033
3134
@@ -44,7 +47,8 @@ exports[`proxies "expand" option(s) 1`] = `
4447
`;
4548

4649
exports[`works with default options 1`] = `
47-
"- First value
50+
"Snapshot Diff:
51+
- First value
4852
+ Second value
4953
5054
@@ -5,8 +5,9 @@
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @flow
2+
3+
const snapshotDiff = require('../src/index');
4+
5+
expect.addSnapshotSerializer(snapshotDiff.getSnapshotDiffSerializer());
6+
7+
test('serialize text diff output', () => {
8+
expect(
9+
snapshotDiff(
10+
['foo', 'bar', 'baz'].join('\n'),
11+
['foo', 'baz', 'quoz'].join('\n')
12+
)
13+
).toMatchSnapshot();
14+
});
15+
16+
test('serialize array diff output', () => {
17+
expect(
18+
snapshotDiff(['foo', 'bar', 'baz'], ['foo', 'baz', 'quoz'])
19+
).toMatchSnapshot();
20+
});
21+
22+
test('serialize object diff output', () => {
23+
expect(snapshotDiff({ foo: 'bar' }, { foo: 'baz' })).toMatchSnapshot();
24+
});

__tests__/index.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ test('public api', () => {
44
expect(index).toBeInstanceOf(Function);
55
expect(index.snapshotDiff).toBe(index);
66
expect(index.toMatchDiffSnapshot).toBeInstanceOf(Function);
7+
expect(index.getSnapshotDiffSerializer).toBeInstanceOf(Function);
78

8-
const { snapshotDiff, toMatchDiffSnapshot } = require('../src/index');
9+
const {
10+
snapshotDiff,
11+
toMatchDiffSnapshot,
12+
getSnapshotDiffSerializer,
13+
} = require('../src/index');
914

1015
expect(snapshotDiff).toBe(index);
1116
expect(toMatchDiffSnapshot).toBe(index.toMatchDiffSnapshot);
17+
expect(getSnapshotDiffSerializer).toBe(index.getSnapshotDiffSerializer);
1218
});

serializer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var snapshotDiff = require('./build');
2+
3+
module.exports = snapshotDiff.getSnapshotDiffSerializer();

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const defaultOptions = {
2121
contextLines: -1, // Forces to use default from Jest
2222
};
2323

24+
const SNAPSHOT_TITLE = 'Snapshot Diff:\n';
25+
2426
const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
2527
let difference;
2628
const mergedOptions = Object.assign({}, defaultOptions, options);
@@ -33,10 +35,11 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
3335

3436
if (!mergedOptions.colors) {
3537
const stripAnsi = require('strip-ansi');
36-
return stripAnsi(difference);
38+
39+
difference = stripAnsi(difference);
3740
}
3841

39-
return difference;
42+
return SNAPSHOT_TITLE + difference;
4043
};
4144

4245
const isReactComponent = (value: any) =>
@@ -71,6 +74,18 @@ function toMatchDiffSnapshot(valueA: any, valueB: any, options?: Options) {
7174
return snapshot.toMatchSnapshot.call(this, difference);
7275
}
7376

77+
function getSnapshotDiffSerializer() {
78+
return {
79+
test(value: any) {
80+
return typeof value === 'string' && value.indexOf(SNAPSHOT_TITLE) === 0;
81+
},
82+
print(value: any) {
83+
return value;
84+
},
85+
};
86+
}
87+
7488
module.exports = snapshotDiff;
7589
module.exports.snapshotDiff = snapshotDiff;
7690
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;
91+
module.exports.getSnapshotDiffSerializer = getSnapshotDiffSerializer;

0 commit comments

Comments
 (0)