From 3f9ca852d8d3d40cc3a662220d2e61aeae270ba2 Mon Sep 17 00:00:00 2001 From: Petter Nordholm Date: Thu, 23 May 2019 22:40:05 +0200 Subject: [PATCH 1/3] Prevent drifting inline snapshots #8424 Fixes #8424 by avoiding indenting inline snapshot if second line of inline snapshot already has been indented. --- .../src/__tests__/inline_snapshots.test.ts | 50 +++++++++++++++++++ .../jest-snapshot/src/inline_snapshots.ts | 7 +++ 2 files changed, 57 insertions(+) diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts index a6173b61cb86..68826b5aec2a 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.ts @@ -236,6 +236,56 @@ test('saveInlineSnapshots() indents multi-line snapshots with spaces', () => { ); }); +test('saveInlineSnapshots() does not re-indent already indented snapshots', () => { + const filename = path.join(__dirname, 'my.test.js'); + (fs.readFileSync as jest.Mock).mockImplementation( + () => + "it('is a test', () => {\n" + + " expect({a: 'a'}).toMatchInlineSnapshot();\n" + + '});\n' + + "it('is a another test', () => {\n" + + " expect({a: 'a'}).toMatchInlineSnapshot(`\n" + + ' Object {\n' + + " b: 'b'\n" + + ' }\n' + + ' `);\n' + + '});\n', + ); + (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({ + bracketSpacing: false, + singleQuote: true, + }); + + saveInlineSnapshots( + [ + { + frame: {column: 20, file: filename, line: 2} as Frame, + snapshot: `\nObject {\n a: 'a'\n}\n`, + }, + ], + prettier, + babelTraverse, + ); + + expect(fs.writeFileSync).toHaveBeenCalledWith( + filename, + "it('is a test', () => {\n" + + " expect({a: 'a'}).toMatchInlineSnapshot(`\n" + + ' Object {\n' + + " a: 'a'\n" + + ' }\n' + + ' `);\n' + + '});\n' + + "it('is a another test', () => {\n" + + " expect({a: 'a'}).toMatchInlineSnapshot(`\n" + + ' Object {\n' + + " b: 'b'\n" + + ' }\n' + + ' `);\n' + + '});\n', + ); +}); + test('saveInlineSnapshots() indents multi-line snapshots with tabs', () => { const filename = path.join(__dirname, 'my.test.js'); (fs.readFileSync as jest.Mock).mockImplementation( diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index ec2612ae2ce6..2f9a931dd317 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -120,6 +120,13 @@ const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file); const indent = (snapshot: string, numIndents: number, indentation: string) => { const lines = snapshot.split('\n'); + if ( + lines.length > 2 && + lines[1].startsWith(indentation.repeat(numIndents + 1)) + ) { + return snapshot; + } + return lines .map((line, index) => { if (index === 0) { From ef5a9e3cbe8790c2edc0848849dfbc20ff56307a Mon Sep 17 00:00:00 2001 From: Petter Nordholm Date: Fri, 24 May 2019 09:49:46 +0200 Subject: [PATCH 2/3] Added pull request to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea66aee969ad..dbfe4d39602f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429) - `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422)) +- `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492)) ### Chore & Maintenance From 9c92314f593bf2657582cdbe52418e2f06f1d98d Mon Sep 17 00:00:00 2001 From: Petter Nordholm Date: Sat, 25 May 2019 10:38:29 +0200 Subject: [PATCH 3/3] Fixed review comment Also added a short comment to the code describing its purpose. --- packages/jest-snapshot/src/inline_snapshots.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index 2f9a931dd317..8b3e38afcc28 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -120,8 +120,9 @@ const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file); const indent = (snapshot: string, numIndents: number, indentation: string) => { const lines = snapshot.split('\n'); + // Prevent re-identation of inline snapshots. if ( - lines.length > 2 && + lines.length >= 2 && lines[1].startsWith(indentation.repeat(numIndents + 1)) ) { return snapshot;