Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use fallback if prettier not found #11400

Merged
merged 5 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
if (prettierPath) {
try {
// @ts-expect-error requireOutside Babel transform
prettier = requireOutside(prettierPath) as Prettier;
} catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about it, we should probably just swallow MODULE_NOT_FOUND and rethrow other errors. Meh 🤷

// 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', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is literally a copy/paste of the previous test case, but with a phony install path.

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