Skip to content

Commit 0145af1

Browse files
authored
feat: add testName to toMatchDiffSnapshot (#64)
1 parent 876bdf4 commit 0145af1

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

README.md

+21-25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Diffing snapshot utility for Jest. Takes two values, and return their difference
77
Especially helpful when testing the difference between different React component states.
88

99
## Installation
10+
1011
```bash
1112
yarn add --dev snapshot-diff
1213
```
@@ -18,7 +19,7 @@ yarn add --dev snapshot-diff
1819
```js
1920
const 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

2829
test('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

4341
expect.extend({ toMatchDiffSnapshot });
4442

45-
test('snapshot difference between 2 strings', () => {
43+
test('snapshot difference between 2 strings', () => {
4644
expect(a).toMatchDiffSnapshot(b);
4745
});
4846

4947
const React = require('react');
5048
const 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-
6357
Produced 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.
9993
To fix this – `snapshot-diff` comes with custom serializer, which you can add directly in your tests or in `setupFiles` script:
10094

10195
```js
10296
const snapshotDiff = require('snapshot-diff');
10397

10498
expect.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
131127
snapshotDiff(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

__tests__/__snapshots__/toMatchDiffSnapshot.test.js.snap

+18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ exports[`proxies "expand" option(s) 1`] = `
4848
"
4949
`;
5050

51+
exports[`works with custom name: slim 1`] = `
52+
"Snapshot Diff:
53+
- First value
54+
+ Second value
55+
56+
@@ -5,9 +5,10 @@
57+
some
58+
some
59+
some
60+
some
61+
not
62+
+ so
63+
very
64+
long
65+
script
66+
"
67+
`;
68+
5169
exports[`works with default options 1`] = `
5270
"Snapshot Diff:
5371
- First value

__tests__/toMatchDiffSnapshot.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ test('works with default options', () => {
4747
});
4848
}
4949
);
50+
51+
test('works with custom name', () => {
52+
// $FlowFixMe
53+
expect(a).toMatchDiffSnapshot(b, {}, 'slim');
54+
});

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare namespace jest {
1414
* Compare the difference between the actual in the `expect()`
1515
* vs the object inside `valueB` with some extra options.
1616
*/
17-
toMatchDiffSnapshot(valueB: any, options?: DiffOptions): R
17+
toMatchDiffSnapshot(valueB: any, options?: DiffOptions, testName?: string): R
1818
}
1919
}
2020

@@ -30,7 +30,7 @@ declare module "snapshot-diff" {
3030
*/
3131
toMatchDiffSnapshot: jest.CustomMatcher
3232
/**
33-
* By default Jest adds extra quotes around strings so it makes diff
33+
* By default Jest adds extra quotes around strings so it makes diff
3434
* snapshots of objects too noisy. To fix this – snapshot-diff comes
3535
* with custom serializer.
3636
*/

src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ function diffReactComponents(valueA: any, valueB: any, options: Options) {
8888
});
8989
}
9090

91-
function toMatchDiffSnapshot(valueA: any, valueB: any, options?: Options) {
91+
function toMatchDiffSnapshot(
92+
valueA: any,
93+
valueB: any,
94+
options?: Options,
95+
testName?: string
96+
) {
9297
const difference = snapshotDiff(valueA, valueB, options);
9398

94-
return snapshot.toMatchSnapshot.call(this, difference);
99+
return snapshot.toMatchSnapshot.call(this, difference, testName);
95100
}
96101

97102
function getSnapshotDiffSerializer() {

0 commit comments

Comments
 (0)