Skip to content

Commit 5d38d77

Browse files
feat(breaking): Output jest mock function names in snapshot diff (#122)
* Output jest mock function names in snapshot diff Use same serializers as jest-snapshot when serializing react components rendered by react-test-renderer. Fixes #106 * Add test to show output for React fragments With the React serializer updated to use `prettyFormat`, this now supports diffing React fragments when comparing a single element to multiple elements returned. Adding test to confirm output. Fixes #111 * Use jest-snapshot serializers for diff annotations Makes use of the same serializers used for printing, in the react serializer diff options which set up the A and B diff annotations.
1 parent ca9c069 commit 5d38d77

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

__tests__/__snapshots__/snapshotDiff.test.js.snap

+33-15
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ exports[`can use contextLines on diff 1`] = `
5050

5151
exports[`can use contextLines with React components 1`] = `
5252
"Snapshot Diff:
53-
- <Component test=\\"say\\" />
54-
+ <Component test=\\"my name\\" />
53+
- <Component namedJestMock={[MockFunction test-mock-name]} test=\\"say\\" unnamedFunction={[Function unnamedFunction]} unnamedJestMock={[MockFunction]} />
54+
+ <Component namedJestMock={[MockFunction test-mock-name]} test=\\"my name\\" unnamedFunction={[Function unnamedFunction]} unnamedJestMock={[MockFunction]} />
5555
56-
@@ -6,1 +6,1 @@
56+
@@ -10,1 +10,1 @@
5757
- say
5858
+ my name
59-
@@ -9,1 +9,1 @@
59+
@@ -13,1 +13,1 @@
6060
- say
6161
+ my name
62-
@@ -32,1 +32,1 @@
62+
@@ -36,1 +36,1 @@
6363
- say
6464
+ my name"
6565
`;
@@ -115,15 +115,15 @@ exports[`collapses diffs and strips ansi by default 1`] = `
115115

116116
exports[`detects React components 1`] = `
117117
"Snapshot Diff:
118-
- <Component test=\\"say\\" />
119-
+ <Component test=\\"my name\\" />
120-
121-
@@ -1,14 +1,14 @@
122-
<div>
123-
<span />
124-
<span />
125-
<span />
126-
<span>
118+
- <Component namedJestMock={[MockFunction test-mock-name]} test=\\"say\\" unnamedFunction={[Function unnamedFunction]} unnamedJestMock={[MockFunction]} />
119+
+ <Component namedJestMock={[MockFunction test-mock-name]} test=\\"my name\\" unnamedFunction={[Function unnamedFunction]} unnamedJestMock={[MockFunction]} />
120+
121+
@@ -5,14 +5,14 @@
122+
<span
123+
onBlur={[Function unnamedFunction]}
124+
onClick={[MockFunction]}
125+
onFocus={[MockFunction test-mock-name]}
126+
>
127127
- say
128128
+ my name
129129
</span>
@@ -135,7 +135,7 @@ exports[`detects React components 1`] = `
135135
<span />
136136
<span />
137137
<span />
138-
@@ -27,11 +27,11 @@
138+
@@ -31,11 +31,11 @@
139139
<span />
140140
<span />
141141
<span />
@@ -165,3 +165,21 @@ exports[`failed optional deps throws with sensible message on missing react-test
165165
"Failed to load optional module \\"react-test-renderer\\". If you need to compare React elements, please add \\"react-test-renderer\\" to your project's dependencies.
166166
Cannot find module 'non-existent-module-for-testing' from 'snapshotDiff.test.js'"
167167
`;
168+
169+
exports[`shows diff when comparing React fragments of varying length 1`] = `
170+
"Snapshot Diff:
171+
- <FragmentComponent />
172+
+ <FragmentComponent withSecond={true} />
173+
174+
- <div>
175+
- First
176+
- </div>
177+
+ Array [
178+
+ <div>
179+
+ First
180+
+ </div>,
181+
+ <div>
182+
+ Second
183+
+ </div>,
184+
+ ]"
185+
`;

__tests__/snapshotDiff.test.js

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @flow
22

3+
/* eslint-disable react/no-multi-comp */
4+
35
const React = require('react');
46
const snapshotDiff = require('../src/index');
57

@@ -87,7 +89,13 @@ class Component extends React.Component<Props> {
8789
<span />
8890
<span />
8991
<span />
90-
<span>{this.props.test}</span>
92+
<span
93+
onBlur={this.props.unnamedFunction}
94+
onClick={this.props.unnamedJestMock}
95+
onFocus={this.props.namedJestMock}
96+
>
97+
{this.props.test}
98+
</span>
9199
<span>{this.props.test}</span>
92100
<span />
93101
<span />
@@ -159,6 +167,23 @@ class Component extends React.Component<Props> {
159167
}
160168
}
161169

170+
class FragmentComponent extends React.Component<Props> {
171+
render() {
172+
return (
173+
<>
174+
<div>First</div>
175+
{this.props.withSecond ? <div>Second</div> : null}
176+
</>
177+
);
178+
}
179+
}
180+
181+
const props = {
182+
unnamedFunction: () => {},
183+
unnamedJestMock: jest.fn(),
184+
namedJestMock: jest.fn().mockName('test-mock-name'),
185+
};
186+
162187
test('collapses diffs and strips ansi by default', () => {
163188
expect(snapshotDiff(a, b)).toMatchSnapshot();
164189
});
@@ -187,15 +212,28 @@ test('diffs short strings', () => {
187212

188213
test('detects React components', () => {
189214
expect(
190-
snapshotDiff(<Component test="say" />, <Component test="my name" />)
215+
snapshotDiff(
216+
<Component {...props} test="say" />,
217+
<Component {...props} test="my name" />
218+
)
191219
).toMatchSnapshot();
192220
});
193221

194222
test('can use contextLines with React components', () => {
195223
expect(
196-
snapshotDiff(<Component test="say" />, <Component test="my name" />, {
197-
contextLines: 0,
198-
})
224+
snapshotDiff(
225+
<Component {...props} test="say" />,
226+
<Component {...props} test="my name" />,
227+
{
228+
contextLines: 0,
229+
}
230+
)
231+
).toMatchSnapshot();
232+
});
233+
234+
test('shows diff when comparing React fragments of varying length', () => {
235+
expect(
236+
snapshotDiff(<FragmentComponent />, <FragmentComponent withSecond />)
199237
).toMatchSnapshot();
200238
});
201239

src/react-serializer.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
'use strict';
44

55
const prettyFormat = require('pretty-format');
6+
const snapshot = require('jest-snapshot');
7+
8+
const serializers = snapshot.getSerializers();
69

7-
const { ReactElement } = prettyFormat.plugins;
810
const reactElement = Symbol.for('react.element');
911

1012
function getReactComponentSerializer() {
@@ -22,7 +24,8 @@ function getReactComponentSerializer() {
2224
}
2325
throw error;
2426
}
25-
return value => renderer.create(value).toJSON();
27+
return value =>
28+
prettyFormat(renderer.create(value), { plugins: serializers });
2629
}
2730

2831
const reactSerializer = {
@@ -32,7 +35,7 @@ const reactSerializer = {
3235
return reactComponentSerializer(value);
3336
},
3437
diffOptions: (valueA: any, valueB: any) => {
35-
const prettyFormatOptions = { plugins: [ReactElement], min: true };
38+
const prettyFormatOptions = { plugins: serializers, min: true };
3639
return {
3740
aAnnotation: prettyFormat(valueA, prettyFormatOptions),
3841
bAnnotation: prettyFormat(valueB, prettyFormatOptions),

0 commit comments

Comments
 (0)