Skip to content

Commit 6b18aed

Browse files
authored
fix(jest-snapshot): always run prettier format a second time (#11560)
1 parent fb0e09c commit 6b18aed

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Fixes
88

9+
- `[jest-snapshot]` Correctly indent inline snapshots ([#11560](https://github.com/facebook/jest/pull/11560))
10+
911
### Chore & Maintenance
1012

1113
### Performance

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ test('handles property matchers', () => {
118118
119119
`;
120120
121+
exports[`indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier: existing snapshot 1`] = `
122+
it('is true', () => {
123+
expect(true).toMatchInlineSnapshot(\`true\`);
124+
expect([1, 2, 3]).toMatchInlineSnapshot(\`
125+
Array [
126+
1,
127+
2,
128+
3,
129+
]
130+
\`);
131+
});
132+
133+
`;
134+
121135
exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = `
122136
test('existing snapshot', () => {
123137
expect({hello: 'world'}).toMatchInlineSnapshot(\`

e2e/__tests__/toMatchInlineSnapshot.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,20 @@ test('indentation is correct in the presences of existing snapshots', () => {
382382
expect(exitCode).toBe(0);
383383
expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot');
384384
});
385+
386+
test('indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier', () => {
387+
const filename = 'existing-snapshot.test.js';
388+
const test = `
389+
it('is true', () => {
390+
expect(true).toMatchInlineSnapshot(\`true\`);
391+
expect([1, 2, 3]).toMatchInlineSnapshot();
392+
});\\n
393+
`;
394+
395+
writeFiles(TESTS_DIR, {[filename]: test});
396+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
397+
const fileAfter = readFile(filename);
398+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
399+
expect(exitCode).toBe(0);
400+
expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot');
401+
});

packages/jest-snapshot/src/InlineSnapshots.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,19 @@ const runPrettier = (
285285
// Snapshots have now been inserted. Run prettier to make sure that the code is
286286
// formatted, except snapshot indentation. Snapshots cannot be formatted until
287287
// after the initial format because we don't know where the call expression
288-
// will be placed (specifically its indentation).
289-
let newSourceFile = prettier.format(sourceFileWithSnapshots, {
290-
...config,
291-
filepath: sourceFilePath,
292-
});
293-
294-
if (newSourceFile !== sourceFileWithSnapshots) {
295-
// prettier moved things around, run it again to fix snapshot indentations.
296-
newSourceFile = prettier.format(newSourceFile, {
288+
// will be placed (specifically its indentation), so we have to do two
289+
// prettier.format calls back-to-back.
290+
return prettier.format(
291+
prettier.format(sourceFileWithSnapshots, {
292+
...config,
293+
filepath: sourceFilePath,
294+
}),
295+
{
297296
...config,
298297
filepath: sourceFilePath,
299298
parser: createFormattingParser(snapshotMatcherNames, inferredParser),
300-
});
301-
}
302-
303-
return newSourceFile;
299+
},
300+
);
304301
};
305302

306303
// This parser formats snapshots to the correct indentation.

0 commit comments

Comments
 (0)