Skip to content

Commit

Permalink
feat: use fallback if prettier not found (#11400)
Browse files Browse the repository at this point in the history
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
  • Loading branch information
connorjclark and SimenB authored May 20, 2021
1 parent 150dbd8 commit 50451df
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 9 additions & 4 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
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);

Expand Down
30 changes: 30 additions & 0 deletions packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 50451df

Please sign in to comment.