Skip to content

Commit 9ac8ca7

Browse files
petternordholmscotthovestadt
authored andcommitted
Prevent drifting inline snapshots #8424 (#8492)
* Prevent drifting inline snapshots #8424 Fixes #8424 by avoiding indenting inline snapshot if second line of inline snapshot already has been indented. * Added pull request to changelog * Fixed review comment Also added a short comment to the code describing its purpose.
1 parent 07c554e commit 9ac8ca7

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
- `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429)
1212
- `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422))
13+
- `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492))
1314

1415
### Chore & Maintenance
1516

packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,56 @@ test('saveInlineSnapshots() indents multi-line snapshots with spaces', () => {
236236
);
237237
});
238238

239+
test('saveInlineSnapshots() does not re-indent already indented snapshots', () => {
240+
const filename = path.join(__dirname, 'my.test.js');
241+
(fs.readFileSync as jest.Mock).mockImplementation(
242+
() =>
243+
"it('is a test', () => {\n" +
244+
" expect({a: 'a'}).toMatchInlineSnapshot();\n" +
245+
'});\n' +
246+
"it('is a another test', () => {\n" +
247+
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
248+
' Object {\n' +
249+
" b: 'b'\n" +
250+
' }\n' +
251+
' `);\n' +
252+
'});\n',
253+
);
254+
(prettier.resolveConfig.sync as jest.Mock).mockReturnValue({
255+
bracketSpacing: false,
256+
singleQuote: true,
257+
});
258+
259+
saveInlineSnapshots(
260+
[
261+
{
262+
frame: {column: 20, file: filename, line: 2} as Frame,
263+
snapshot: `\nObject {\n a: 'a'\n}\n`,
264+
},
265+
],
266+
prettier,
267+
babelTraverse,
268+
);
269+
270+
expect(fs.writeFileSync).toHaveBeenCalledWith(
271+
filename,
272+
"it('is a test', () => {\n" +
273+
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
274+
' Object {\n' +
275+
" a: 'a'\n" +
276+
' }\n' +
277+
' `);\n' +
278+
'});\n' +
279+
"it('is a another test', () => {\n" +
280+
" expect({a: 'a'}).toMatchInlineSnapshot(`\n" +
281+
' Object {\n' +
282+
" b: 'b'\n" +
283+
' }\n' +
284+
' `);\n' +
285+
'});\n',
286+
);
287+
});
288+
239289
test('saveInlineSnapshots() indents multi-line snapshots with tabs', () => {
240290
const filename = path.join(__dirname, 'my.test.js');
241291
(fs.readFileSync as jest.Mock).mockImplementation(

packages/jest-snapshot/src/inline_snapshots.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file);
120120

121121
const indent = (snapshot: string, numIndents: number, indentation: string) => {
122122
const lines = snapshot.split('\n');
123+
// Prevent re-identation of inline snapshots.
124+
if (
125+
lines.length >= 2 &&
126+
lines[1].startsWith(indentation.repeat(numIndents + 1))
127+
) {
128+
return snapshot;
129+
}
130+
123131
return lines
124132
.map((line, index) => {
125133
if (index === 0) {

0 commit comments

Comments
 (0)