Skip to content

Commit 50451df

Browse files
connorjclarkSimenB
andauthored
feat: use fallback if prettier not found (#11400)
Co-authored-by: Simen Bekkhus <[email protected]>
1 parent 150dbd8 commit 50451df

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- `[jest-runtime]` Detect reexports from CJS as named exports in ESM ([#10988](https://github.com/facebook/jest/pull/10988))
3737
- `[jest-runtime]` Support for async code transformations ([#11191](https://github.com/facebook/jest/pull/11191) & [#11220](https://github.com/facebook/jest/pull/11220))
3838
- `[jest-reporters]` Add static filepath property to all reporters ([#11015](https://github.com/facebook/jest/pull/11015))
39-
- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
39+
- `[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))
4040
- `[jest-snapshot]` [**BREAKING**] Run transforms over `snapshotResolver` ([#8751](https://github.com/facebook/jest/pull/8829))
4141
- `[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))
4242
- `[jest-transform]` Add support for transformers written in ESM ([#11163](https://github.com/facebook/jest/pull/11163))

packages/jest-snapshot/src/InlineSnapshots.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ export function saveInlineSnapshots(
4949
snapshots: Array<InlineSnapshot>,
5050
prettierPath: Config.Path,
5151
): void {
52-
const prettier = prettierPath
53-
? // @ts-expect-error requireOutside Babel transform
54-
(requireOutside(prettierPath) as Prettier)
55-
: undefined;
52+
let prettier: Prettier | null = null;
53+
if (prettierPath) {
54+
try {
55+
// @ts-expect-error requireOutside Babel transform
56+
prettier = requireOutside(prettierPath) as Prettier;
57+
} catch {
58+
// Continue even if prettier is not installed.
59+
}
60+
}
5661

5762
const snapshotsByFile = groupSnapshotsByFile(snapshots);
5863

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
7373
);
7474
});
7575

76+
test('saveInlineSnapshots() with bad prettier path leaves formatting outside of snapshots alone', () => {
77+
const filename = path.join(dir, 'my.test.js');
78+
fs.writeFileSync(
79+
filename,
80+
`
81+
const a = [1, 2];
82+
expect(a).toMatchInlineSnapshot(\`an out-of-date and also multi-line
83+
snapshot\`);
84+
expect(a).toMatchInlineSnapshot();
85+
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
86+
`.trim() + '\n',
87+
);
88+
89+
saveInlineSnapshots(
90+
[2, 4, 5].map(line => ({
91+
frame: {column: 11, file: filename, line} as Frame,
92+
snapshot: `[1, 2]`,
93+
})),
94+
'bad-prettier',
95+
);
96+
97+
expect(fs.readFileSync(filename, 'utf8')).toBe(
98+
`const a = [1, 2];
99+
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
100+
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
101+
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
102+
`,
103+
);
104+
});
105+
76106
test('saveInlineSnapshots() can handle typescript without prettier', () => {
77107
const filename = path.join(dir, 'my.test.ts');
78108
fs.writeFileSync(

0 commit comments

Comments
 (0)