Skip to content

Commit 23eead3

Browse files
committed
Implement toMatchInlineDiffSnapshot custom matcher (#330)
1 parent f042074 commit 23eead3

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ exports[`snapshot difference between 2 React components state 1`] = `
9393
`;
9494
```
9595

96+
### Inline Snapshots
97+
98+
You might want to use inline snapshots.
99+
100+
With a default matcher, you can use `toMatchInlineSnapshot` instead of
101+
`toMatchSnapshot`. With a custom matcher, you can use `toMatchInlineDiffSnapshot` instead of `toMatchDiffSnapshot`.
102+
96103
## Custom serializers
97104

98105
By default, `snapshot-diff` uses a built in React serializer based on `react-test-renderer`. The
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const snapshotDiff = require('../src/index');
2+
3+
const a = `
4+
some
5+
some
6+
some
7+
some
8+
some
9+
some
10+
some
11+
not
12+
very
13+
long
14+
script
15+
`;
16+
const b = `
17+
some
18+
some
19+
some
20+
some
21+
some
22+
some
23+
some
24+
not
25+
so
26+
very
27+
long
28+
script
29+
`;
30+
31+
beforeAll(() => {
32+
expect.extend({
33+
toMatchInlineDiffSnapshot: snapshotDiff.toMatchInlineDiffSnapshot,
34+
});
35+
});
36+
37+
test('works with default options', () => {
38+
expect(a).toMatchInlineDiffSnapshot(
39+
b,
40+
`
41+
"Snapshot Diff:
42+
- First value
43+
+ Second value
44+
45+
@@ -5,9 +5,10 @@
46+
some
47+
some
48+
some
49+
some
50+
not
51+
+ so
52+
very
53+
long
54+
script
55+
"
56+
`
57+
);
58+
});
59+
60+
test('works with non-default options', () => {
61+
expect(a).toMatchInlineDiffSnapshot(
62+
b,
63+
{ stablePatchmarks: true },
64+
`
65+
"Snapshot Diff:
66+
- First value
67+
+ Second value
68+
69+
@@ --- --- @@
70+
some
71+
some
72+
some
73+
some
74+
not
75+
+ so
76+
very
77+
long
78+
script
79+
"
80+
`
81+
);
82+
});

extend-expect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* global expect */
2-
const { toMatchDiffSnapshot } = require('./build/');
2+
const { toMatchDiffSnapshot, toMatchInlineDiffSnapshot } = require('./build/');
33

4-
expect.extend({ toMatchDiffSnapshot });
4+
expect.extend({ toMatchDiffSnapshot, toMatchInlineDiffSnapshot });

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ declare module 'snapshot-diff' {
4040
* make it available via `expect.extend({ toMatchDiffSnapshot })`.
4141
*/
4242
toMatchDiffSnapshot: jest.CustomMatcher;
43+
/**
44+
* Compare the changes from a to b with the diff inlined into tests
45+
*/
46+
toMatchInlineDiffSnapshot: jest.CustomMatcher;
4347
/**
4448
* By default Jest adds extra quotes around strings so it makes diff
4549
* snapshots of objects too noisy. To fix this – snapshot-diff comes

src/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ function toMatchDiffSnapshot(
9191
return snapshot.toMatchSnapshot.call(this, difference, testName || '');
9292
}
9393

94+
function toMatchInlineDiffSnapshot(
95+
valueA: any,
96+
valueB: any,
97+
optionsOrSnapshot?: Options | string,
98+
inlineSnapshot?: string
99+
) {
100+
let options = undefined;
101+
if (typeof optionsOrSnapshot === 'string') {
102+
inlineSnapshot = optionsOrSnapshot;
103+
} else {
104+
options = optionsOrSnapshot;
105+
}
106+
107+
const difference = snapshotDiff(valueA, valueB, options);
108+
109+
if (inlineSnapshot) {
110+
return snapshot.toMatchInlineSnapshot.call(this, difference, inlineSnapshot);
111+
} else {
112+
return snapshot.toMatchInlineSnapshot.call(this, difference)
113+
}
114+
}
115+
94116
function getSnapshotDiffSerializer() {
95117
return {
96118
test(value: any) {
@@ -109,6 +131,7 @@ function setSerializers(customSerializers) {
109131
module.exports = snapshotDiff;
110132
module.exports.snapshotDiff = snapshotDiff;
111133
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;
134+
module.exports.toMatchInlineDiffSnapshot = toMatchInlineDiffSnapshot;
112135
module.exports.getSnapshotDiffSerializer = getSnapshotDiffSerializer;
113136
module.exports.setSerializers = setSerializers;
114137
module.exports.defaultSerializers = defaultSerializers;

0 commit comments

Comments
 (0)