From e2ca268eb2796984df8b973a214f3f911f14d995 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Tue, 11 May 2021 18:32:42 -0600 Subject: [PATCH 1/4] feat: use fallback if prettier not found --- packages/jest-snapshot/src/InlineSnapshots.ts | 13 +++++--- .../src/__tests__/InlineSnapshots.test.ts | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 8652344b49e3..4597f58000bd 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -49,10 +49,15 @@ export function saveInlineSnapshots( snapshots: Array, prettierPath: Config.Path, ): void { - const prettier = prettierPath - ? // @ts-expect-error requireOutside Babel transform - (requireOutside(prettierPath) as Prettier) - : undefined; + let prettier; + if (prettierPath) { + try { + // @ts-expect-error requireOutside Babel transform + prettier = requireOutside(prettierPath) as Prettier; + } catch (_) { + // Continue even if prettier is not installed. + } + } const snapshotsByFile = groupSnapshotsByFile(snapshots); diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index e033cf6aee1e..792c2f14ed19 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -73,6 +73,36 @@ expect(a).toMatchInlineSnapshot(\`[1, 2]\`); ); }); +test('saveInlineSnapshots() with bad prettier path leaves formatting outside of snapshots alone', () => { + const filename = path.join(dir, 'my.test.js'); + fs.writeFileSync( + filename, + ` +const a = [1, 2]; +expect(a).toMatchInlineSnapshot(\`an out-of-date and also multi-line +snapshot\`); +expect(a).toMatchInlineSnapshot(); +expect(a).toMatchInlineSnapshot(\`[1, 2]\`); +`.trim() + '\n', + ); + + saveInlineSnapshots( + [2, 4, 5].map(line => ({ + frame: {column: 11, file: filename, line} as Frame, + snapshot: `[1, 2]`, + })), + 'bad-prettier', + ); + + expect(fs.readFileSync(filename, 'utf8')).toBe( + `const a = [1, 2]; +expect(a).toMatchInlineSnapshot(\`[1, 2]\`); +expect(a).toMatchInlineSnapshot(\`[1, 2]\`); +expect(a).toMatchInlineSnapshot(\`[1, 2]\`); +`, + ); +}); + test('saveInlineSnapshots() can handle typescript without prettier', () => { const filename = path.join(dir, 'my.test.ts'); fs.writeFileSync( From 4edbfc601b3ab650f006926c0d17b4f91e8b9813 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 14 May 2021 10:30:54 -0700 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e38d4a8e26a..b7e6dfac42c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ - `[jest-runtime]` Detect reexports from CJS as named exports in ESM ([#10988](https://github.com/facebook/jest/pull/10988)) - `[jest-runtime]` Support for async code transformations ([#11191](https://github.com/facebook/jest/pull/11191) & [#11220](https://github.com/facebook/jest/pull/11220)) - `[jest-reporters]` Add static filepath property to all reporters ([#11015](https://github.com/facebook/jest/pull/11015)) -- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792)) +- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792) & [#11192](https://github.com/facebook/jest/pull/11192)) - `[jest-snapshot]` [**BREAKING**] Run transforms over `snapshotResolver` ([#8751](https://github.com/facebook/jest/pull/8829)) - `[jest-transform]` Pass config options defined in Jest's config to transformer's `process` and `getCacheKey` functions ([#10926](https://github.com/facebook/jest/pull/10926)) - `[jest-transform]` Add support for transformers written in ESM ([#11163](https://github.com/facebook/jest/pull/11163)) From 09e9efb9ce517e1e13335bccca55951bfccec41c Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 14 May 2021 10:32:00 -0700 Subject: [PATCH 3/4] no catch args --- packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 4597f58000bd..e09b0010b388 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -54,7 +54,7 @@ export function saveInlineSnapshots( try { // @ts-expect-error requireOutside Babel transform prettier = requireOutside(prettierPath) as Prettier; - } catch (_) { + } catch { // Continue even if prettier is not installed. } } From de3e3b5ca39036e19f8e4705a6ba54de41370259 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 20 May 2021 18:16:48 +0200 Subject: [PATCH 4/4] Update packages/jest-snapshot/src/InlineSnapshots.ts --- packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index e09b0010b388..99c46b376925 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -49,7 +49,7 @@ export function saveInlineSnapshots( snapshots: Array, prettierPath: Config.Path, ): void { - let prettier; + let prettier: Prettier | null = null; if (prettierPath) { try { // @ts-expect-error requireOutside Babel transform