From 50451dfe4dc832bb5dfe31b909a73d87259bd095 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Thu, 20 May 2021 13:09:36 -0700 Subject: [PATCH] feat: use fallback if prettier not found (#11400) Co-authored-by: Simen Bekkhus --- CHANGELOG.md | 2 +- packages/jest-snapshot/src/InlineSnapshots.ts | 13 +++++--- .../src/__tests__/InlineSnapshots.test.ts | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe1a0477439e..70d616f2e945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,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)) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 8652344b49e3..99c46b376925 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: Prettier | null = null; + 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(