From d0d66d45de338b8ee08a3df986c8d14127d60c24 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 11 Jun 2021 09:42:16 +1200 Subject: [PATCH 1/3] fix(jest-snapshot): always run prettier format a second time --- packages/jest-snapshot/src/InlineSnapshots.ts | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 99c46b376925..e50485a5f29f 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -285,22 +285,19 @@ const runPrettier = ( // Snapshots have now been inserted. Run prettier to make sure that the code is // formatted, except snapshot indentation. Snapshots cannot be formatted until // after the initial format because we don't know where the call expression - // will be placed (specifically its indentation). - let newSourceFile = prettier.format(sourceFileWithSnapshots, { - ...config, - filepath: sourceFilePath, - }); - - if (newSourceFile !== sourceFileWithSnapshots) { - // prettier moved things around, run it again to fix snapshot indentations. - newSourceFile = prettier.format(newSourceFile, { + // will be placed (specifically its indentation), so we have to do two + // prettier.format calls back-to-back. + return prettier.format( + prettier.format(sourceFileWithSnapshots, { + ...config, + filepath: sourceFilePath, + }), + { ...config, filepath: sourceFilePath, parser: createFormattingParser(snapshotMatcherNames, inferredParser), - }); - } - - return newSourceFile; + }, + ); }; // This parser formats snapshots to the correct indentation. From cc34849a44c75ab285680fb17da2d1dec2b45bf2 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 11 Jun 2021 17:54:25 +1200 Subject: [PATCH 2/3] test(jest-snapshot): add new case for inline snapshot behaviour --- .../toMatchInlineSnapshot.test.ts.snap | 14 ++++++++++++++ e2e/__tests__/toMatchInlineSnapshot.test.ts | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap index a64fb6a82fde..4fbfd82ce35a 100644 --- a/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap +++ b/e2e/__tests__/__snapshots__/toMatchInlineSnapshot.test.ts.snap @@ -118,6 +118,20 @@ test('handles property matchers', () => { `; +exports[`indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier: existing snapshot 1`] = ` +it('is true', () => { + expect(true).toMatchInlineSnapshot(\`true\`); + expect([1, 2, 3]).toMatchInlineSnapshot(\` + Array [ + 1, + 2, + 3, + ] + \`); +}); + +`; + exports[`indentation is correct in the presences of existing snapshots: existing snapshot 1`] = ` test('existing snapshot', () => { expect({hello: 'world'}).toMatchInlineSnapshot(\` diff --git a/e2e/__tests__/toMatchInlineSnapshot.test.ts b/e2e/__tests__/toMatchInlineSnapshot.test.ts index 228662fe6e57..4ac3db884836 100644 --- a/e2e/__tests__/toMatchInlineSnapshot.test.ts +++ b/e2e/__tests__/toMatchInlineSnapshot.test.ts @@ -382,3 +382,20 @@ test('indentation is correct in the presences of existing snapshots', () => { expect(exitCode).toBe(0); expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot'); }); + +test('indentation is correct in the presences of existing snapshots, when the file is correctly formatted by prettier', () => { + const filename = 'existing-snapshot.test.js'; + const test = ` + it('is true', () => { + expect(true).toMatchInlineSnapshot(\`true\`); + expect([1, 2, 3]).toMatchInlineSnapshot(); + });\\n + `; + + writeFiles(TESTS_DIR, {[filename]: test}); + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); + const fileAfter = readFile(filename); + expect(stderr).toMatch('1 snapshot written from 1 test suite.'); + expect(exitCode).toBe(0); + expect(wrap(fileAfter)).toMatchSnapshot('existing snapshot'); +}); From e65f526284dddfe00c1324473663c9518c29d52a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 29 Sep 2021 12:48:10 +0200 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 424b5d74f462..b0bbbb000029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Fixes +- `[jest-snapshot]` Correctly indent inline snapshots ([#11560](https://github.com/facebook/jest/pull/11560)) + ### Chore & Maintenance ### Performance