Skip to content

Commit 2092927

Browse files
authored
fix: Error if prettier@3 is used for inline snapshots (#14367)
1 parent e7d991c commit 2092927

File tree

6 files changed

+175
-2
lines changed

6 files changed

+175
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[jest-circus]` Fix snapshot matchers in concurrent tests when nr of tests exceeds `maxConcurrency` ([#14335](https://github.com/jestjs/jest/pull/14335))
88
- `[@jest/core]` When running global setup and teardown, do not try to change the `message` property of the thrown error object when the `message` property is unwritable ([#14113](https://github.com/jestjs/jest/pull/14113))
99
- `[jest-snapshot]` Move `@types/prettier` from `dependencies` to `devDependencies` ([#14328](https://github.com/jestjs/jest/pull/14328))
10+
- `[jest-snapshot]` Throw an explicit error if Prettier v3 is used ([#14367](https://github.com/jestjs/jest/pull/14367))
1011
- `[jest-reporters]` Add "skipped" and "todo" symbols to Github Actions Reporter ([#14309](https://github.com/jestjs/jest/pull/14309))
1112

1213
### Chore & Maintenance

docs/Configuration.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,41 @@ Default: `'prettier'`
11441144

11451145
Sets the path to the [`prettier`](https://prettier.io/) node module used to update inline snapshots.
11461146

1147+
<details>
1148+
<summary>Prettier version 3 is not supported!</summary>
1149+
1150+
You can either pass `prettierPath: null` in your config to disable using prettier if you don't need it, or use v2 of Prettier solely for Jest.
1151+
1152+
```json title="package.json"
1153+
{
1154+
"devDependencies": {
1155+
"prettier-2": "npm:prettier@^2"
1156+
}
1157+
}
1158+
```
1159+
1160+
```js tab
1161+
/** @type {import('jest').Config} */
1162+
const config = {
1163+
prettierPath: require.resolve('prettier-2'),
1164+
};
1165+
1166+
module.exports = config;
1167+
```
1168+
1169+
```ts tab
1170+
import type {Config} from 'jest';
1171+
1172+
const config: Config = {
1173+
prettierPath: require.resolve('prettier-2'),
1174+
};
1175+
1176+
export default config;
1177+
```
1178+
1179+
We hope to support Prettier v3 seamlessly out of the box in a future version of Jest. See [this](https://github.com/jestjs/jest/issues/14305) tracking issue.
1180+
</details>
1181+
11471182
### `projects` \[array&lt;string | ProjectConfig&gt;]
11481183

11491184
Default: `undefined`
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import {cleanup, runYarnInstall, writeFiles} from '../Utils';
10+
import runJest from '../runJest';
11+
12+
const DIR = path.resolve(
13+
__dirname,
14+
'../to-match-inline-snapshot-with-prettier-3',
15+
);
16+
const TESTS_DIR = path.resolve(DIR, '__tests__');
17+
const JEST_CONFIG_PATH = path.resolve(DIR, 'jest.config.js');
18+
19+
beforeAll(() => {
20+
runYarnInstall(DIR);
21+
});
22+
beforeEach(() => {
23+
cleanup(TESTS_DIR);
24+
cleanup(JEST_CONFIG_PATH);
25+
});
26+
afterAll(() => {
27+
cleanup(TESTS_DIR);
28+
cleanup(JEST_CONFIG_PATH);
29+
});
30+
31+
test('throws correct error', () => {
32+
writeFiles(DIR, {
33+
'jest.config.js': `
34+
module.exports = {prettierPath: require.resolve('prettier')};
35+
`,
36+
});
37+
writeFiles(TESTS_DIR, {
38+
'test.js': `
39+
test('snapshots', () => {
40+
expect(3).toMatchInlineSnapshot();
41+
});
42+
`,
43+
});
44+
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
45+
expect(stderr).toContain(
46+
'Jest: Inline Snapshots are not supported when using Prettier 3.0.0 or above.',
47+
);
48+
expect(exitCode).toBe(1);
49+
});
50+
51+
test('supports passing `null` as `prettierPath`', () => {
52+
writeFiles(DIR, {
53+
'jest.config.js': `
54+
module.exports = {prettierPath: null};
55+
`,
56+
});
57+
writeFiles(TESTS_DIR, {
58+
'test.js': `
59+
test('snapshots', () => {
60+
expect(3).toMatchInlineSnapshot();
61+
});
62+
`,
63+
});
64+
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
65+
expect(stderr).toContain('Snapshots: 1 written, 1 total');
66+
expect(exitCode).toBe(0);
67+
});
68+
69+
test('supports passing `prettier-2` as `prettierPath`', () => {
70+
writeFiles(DIR, {
71+
'jest.config.js': `
72+
module.exports = {prettierPath: require.resolve('prettier-2')};
73+
`,
74+
});
75+
writeFiles(TESTS_DIR, {
76+
'test.js': `
77+
test('snapshots', () => {
78+
expect(3).toMatchInlineSnapshot();
79+
});
80+
`,
81+
});
82+
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
83+
expect(stderr).toContain('Snapshots: 1 written, 1 total');
84+
expect(exitCode).toBe(0);
85+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"devDependencies": {
3+
"prettier": "^3.0.0",
4+
"prettier-2": "npm:prettier@^2"
5+
}
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 6
6+
cacheKey: 8
7+
8+
"prettier-2@npm:prettier@^2":
9+
version: 2.8.8
10+
resolution: "prettier@npm:2.8.8"
11+
bin:
12+
prettier: bin-prettier.js
13+
checksum: b49e409431bf129dd89238d64299ba80717b57ff5a6d1c1a8b1a28b590d998a34e083fa13573bc732bb8d2305becb4c9a4407f8486c81fa7d55100eb08263cf8
14+
languageName: node
15+
linkType: hard
16+
17+
"prettier@npm:^3.0.0":
18+
version: 3.0.0
19+
resolution: "prettier@npm:3.0.0"
20+
bin:
21+
prettier: bin/prettier.cjs
22+
checksum: 6a832876a1552dc58330d2467874e5a0b46b9ccbfc5d3531eb69d15684743e7f83dc9fbd202db6270446deba9c82b79d24383d09924c462b457136a759425e33
23+
languageName: node
24+
linkType: hard
25+
26+
"root-workspace-0b6124@workspace:.":
27+
version: 0.0.0-use.local
28+
resolution: "root-workspace-0b6124@workspace:."
29+
dependencies:
30+
prettier: ^3.0.0
31+
prettier-2: "npm:prettier@^2"
32+
languageName: unknown
33+
linkType: soft

packages/jest-snapshot/src/InlineSnapshots.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as path from 'path';
9+
import {types} from 'util';
910
import type {ParseResult, PluginItem} from '@babel/core';
1011
import type {
1112
Expression,
@@ -60,8 +61,20 @@ export function saveInlineSnapshots(
6061
try {
6162
// @ts-expect-error requireOutside Babel transform
6263
prettier = requireOutside(prettierPath) as Prettier;
63-
} catch {
64-
// Continue even if prettier is not installed.
64+
65+
if (semver.gte(prettier.version, '3.0.0')) {
66+
throw new Error(
67+
'Jest: Inline Snapshots are not supported when using Prettier 3.0.0 or above.\nSee https://jestjs.io/docs/configuration/#prettierpath-string for alternatives.',
68+
);
69+
}
70+
} catch (error) {
71+
if (!types.isNativeError(error)) {
72+
throw error;
73+
}
74+
75+
if ((error as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
76+
throw error;
77+
}
6578
}
6679
}
6780

0 commit comments

Comments
 (0)