Skip to content

Commit 9492a56

Browse files
authored
fix(jest-snapshot): pass snapshotFormat through when diffing failing snapshots (#13181)
1 parent b06f4e5 commit 9492a56

File tree

9 files changed

+90
-33
lines changed

9 files changed

+90
-33
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-snapshot]` Pass `snapshotFormat` through when diffing snapshots ([#13181](https://github.com/facebook/jest/pull/13181))
8+
79
### Chore & Maintenance
810

911
### Performance

e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap

+32
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@ exports[`basic support: snapshot updated 1`] = `
3939
"
4040
`;
4141

42+
exports[`diff with prototype is correct 1`] = `
43+
"FAIL __tests__/with-prototype-diff.test.js
44+
✕ diff with prototype is correct
45+
46+
● diff with prototype is correct
47+
48+
expect(received).toMatchInlineSnapshot(snapshot)
49+
50+
Snapshot name: \`diff with prototype is correct 1\`
51+
52+
- Snapshot - 1
53+
+ Received + 1
54+
55+
- Object {
56+
+ {
57+
"hello": "world",
58+
}
59+
60+
1 | test('diff with prototype is correct', () => {
61+
> 2 | expect({ hello: 'world' }).toMatchInlineSnapshot(\`
62+
| ^
63+
3 | Object {
64+
4 | "hello": "world",
65+
5 | }
66+
67+
at Object.toMatchInlineSnapshot (__tests__/with-prototype-diff.test.js:2:30)
68+
69+
› 1 snapshot failed.
70+
Snapshot Summary
71+
› 1 snapshot failed from 1 test suite. Inspect your code changes or re-run jest with \`-u\` to update them."
72+
`;
73+
4274
exports[`do not indent empty lines: initial write 1`] = `
4375
"test('inline snapshots', () =>
4476
expect(\`hello

e2e/__tests__/failureDetailsProperty.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test('that the failureDetails property is set', () => {
9494
"p1": "hello",
9595
"p2": "world",
9696
}",
97-
"expected": "Object {
97+
"expected": "{
9898
"p1": "hello",
9999
"p2": "sunshine",
100100
}",
@@ -219,7 +219,7 @@ test('that the failureDetails property is set', () => {
219219
"p1": "hello",
220220
"p2": "world",
221221
}",
222-
"expected": "Object {
222+
"expected": "{
223223
"p1": "hello",
224224
"p2": "sunshine",
225225
}",

e2e/__tests__/toMatchInlineSnapshot.test.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import * as path from 'path';
99
import * as fs from 'graceful-fs';
10-
import {cleanup, makeTemplate, writeFiles} from '../Utils';
10+
import {cleanup, extractSummary, makeTemplate, writeFiles} from '../Utils';
1111
import runJest from '../runJest';
1212

1313
const DIR = path.resolve(__dirname, '../to-match-inline-snapshot');
@@ -396,3 +396,21 @@ test('indentation is correct in the presences of existing snapshots, when the fi
396396
expect(exitCode).toBe(0);
397397
expect(fileAfter).toMatchSnapshot('existing snapshot');
398398
});
399+
400+
test('diff with prototype is correct', () => {
401+
const filename = 'with-prototype-diff.test.js';
402+
const test = `
403+
test('diff with prototype is correct', () => {
404+
expect({ hello: 'world' }).toMatchInlineSnapshot(\`
405+
Object {
406+
"hello": "world",
407+
}
408+
\`);
409+
});
410+
`;
411+
412+
writeFiles(TESTS_DIR, {[filename]: test});
413+
const {stderr, exitCode} = runJest(DIR, ['--run-in-band', filename]);
414+
expect(extractSummary(stderr).rest).toMatchSnapshot();
415+
expect(exitCode).toBe(1);
416+
});

e2e/failureDetails-property/__tests__/tests.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('my test', () => {
2020
p1: 'hello',
2121
p2: 'world',
2222
}).toMatchInlineSnapshot(`
23-
Object {
23+
{
2424
"p1": "hello",
2525
"p2": "sunshine",
2626
}

packages/jest-snapshot/src/State.ts

+27-27
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import * as fs from 'graceful-fs';
99
import type {Config} from '@jest/types';
1010
import {getStackTraceLines, getTopFrame} from 'jest-message-util';
11-
import type {OptionsReceived as PrettyFormatOptions} from 'pretty-format';
1211
import {InlineSnapshot, saveInlineSnapshots} from './InlineSnapshots';
13-
import type {SnapshotData} from './types';
12+
import type {SnapshotData, SnapshotFormat} from './types';
1413
import {
1514
addExtraLineBreaks,
1615
getSnapshotData,
@@ -23,28 +22,28 @@ import {
2322
} from './utils';
2423

2524
export type SnapshotStateOptions = {
26-
updateSnapshot: Config.SnapshotUpdateState;
27-
prettierPath?: string | null;
28-
expand?: boolean;
29-
snapshotFormat: PrettyFormatOptions;
30-
rootDir: string;
25+
readonly updateSnapshot: Config.SnapshotUpdateState;
26+
readonly prettierPath?: string | null;
27+
readonly expand?: boolean;
28+
readonly snapshotFormat: SnapshotFormat;
29+
readonly rootDir: string;
3130
};
3231

3332
export type SnapshotMatchOptions = {
34-
testName: string;
35-
received: unknown;
36-
key?: string;
37-
inlineSnapshot?: string;
38-
isInline: boolean;
39-
error?: Error;
33+
readonly testName: string;
34+
readonly received: unknown;
35+
readonly key?: string;
36+
readonly inlineSnapshot?: string;
37+
readonly isInline: boolean;
38+
readonly error?: Error;
4039
};
4140

4241
type SnapshotReturnOptions = {
43-
actual: string;
44-
count: number;
45-
expected?: string;
46-
key: string;
47-
pass: boolean;
42+
readonly actual: string;
43+
readonly count: number;
44+
readonly expected?: string;
45+
readonly key: string;
46+
readonly pass: boolean;
4847
};
4948

5049
type SaveStatus = {
@@ -57,15 +56,16 @@ export default class SnapshotState {
5756
private _dirty: boolean;
5857
// @ts-expect-error - seemingly unused?
5958
private _index: number;
60-
private _updateSnapshot: Config.SnapshotUpdateState;
59+
private readonly _updateSnapshot: Config.SnapshotUpdateState;
6160
private _snapshotData: SnapshotData;
62-
private _initialData: SnapshotData;
63-
private _snapshotPath: string;
61+
private readonly _initialData: SnapshotData;
62+
private readonly _snapshotPath: string;
6463
private _inlineSnapshots: Array<InlineSnapshot>;
65-
private _uncheckedKeys: Set<string>;
66-
private _prettierPath: string | null;
67-
private _snapshotFormat: PrettyFormatOptions;
68-
private _rootDir: string;
64+
private readonly _uncheckedKeys: Set<string>;
65+
private readonly _prettierPath: string | null;
66+
private readonly _rootDir: string;
67+
68+
readonly snapshotFormat: SnapshotFormat;
6969

7070
added: number;
7171
expand: boolean;
@@ -93,7 +93,7 @@ export default class SnapshotState {
9393
this.unmatched = 0;
9494
this._updateSnapshot = options.updateSnapshot;
9595
this.updated = 0;
96-
this._snapshotFormat = options.snapshotFormat;
96+
this.snapshotFormat = options.snapshotFormat;
9797
this._rootDir = options.rootDir;
9898
}
9999

@@ -213,7 +213,7 @@ export default class SnapshotState {
213213
}
214214

215215
const receivedSerialized = addExtraLineBreaks(
216-
serialize(received, undefined, this._snapshotFormat),
216+
serialize(received, undefined, this.snapshotFormat),
217217
);
218218
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
219219
const pass = expected === receivedSerialized;

packages/jest-snapshot/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ const _toMatchSnapshot = (config: MatchSnapshotConfig) => {
394394
actual,
395395
received,
396396
snapshotState.expand,
397+
snapshotState.snapshotFormat,
397398
)}`;
398399

399400
// Passing the actual and expected objects so that a custom reporter

packages/jest-snapshot/src/printSnapshot.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
bForeground3,
4141
} from './colors';
4242
import {dedentLines} from './dedentLines';
43-
import type {MatchSnapshotConfig} from './types';
43+
import type {MatchSnapshotConfig, SnapshotFormat} from './types';
4444
import {deserializeString, minify, serialize} from './utils';
4545

4646
type Chalk = chalk.Chalk;
@@ -235,6 +235,7 @@ export const printSnapshotAndReceived = (
235235
b: string, // received serialized but without extra line breaks
236236
received: unknown,
237237
expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
238+
snapshotFormat: SnapshotFormat,
238239
): string => {
239240
const aAnnotation = 'Snapshot';
240241
const bAnnotation = 'Received';
@@ -303,7 +304,7 @@ export const printSnapshotAndReceived = (
303304

304305
// Fall through to fix a regression for custom serializers
305306
// like jest-snapshot-serializer-raw that ignore the indent option.
306-
const b0 = serialize(received, 0);
307+
const b0 = serialize(received, 0, snapshotFormat);
307308
if (b0 !== b) {
308309
const aLines0 = dedentLines(aLines2);
309310

packages/jest-snapshot/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import type {MatcherContext} from 'expect';
9+
import type {PrettyFormatOptions} from 'pretty-format';
910
import type SnapshotState from './State';
1011

1112
export interface Context extends MatcherContext {
@@ -63,3 +64,5 @@ export interface SnapshotMatchers<R extends void | Promise<void>, T> {
6364
*/
6465
toThrowErrorMatchingInlineSnapshot(snapshot?: string): R;
6566
}
67+
68+
export type SnapshotFormat = Omit<PrettyFormatOptions, 'compareKeys'>;

0 commit comments

Comments
 (0)