Skip to content

Commit

Permalink
Support Prettier 3 (#14566)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Sep 25, 2023
1 parent a640c65 commit 769ee6e
Show file tree
Hide file tree
Showing 13 changed files with 665 additions and 373 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
- `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072))
- `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965))
- `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566))
- `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470))

### Fixes
Expand Down
4 changes: 3 additions & 1 deletion constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ gen_enforced_dependency(WorkspaceCwd, DependencyIdent, DependencyRange2, Depende
% @types/node in the root need to stay on ~14.14.45
'@types/node',
% upgrading the entire repository is a breaking change
'glob'
'glob',
% repository and snapshot
'prettier'
]).

% Enforces that a dependency doesn't appear in both `dependencies` and `devDependencies`
Expand Down
36 changes: 0 additions & 36 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1145,42 +1145,6 @@ Default: `'prettier'`

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

<details>
<summary>Prettier version 3 is not supported!</summary>

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.

```json title="package.json"
{
"devDependencies": {
"prettier-2": "npm:prettier@^2"
}
}
```

```js tab
/** @type {import('jest').Config} */
const config = {
prettierPath: require.resolve('prettier-2'),
};

module.exports = config;
```

```ts tab
import type {Config} from 'jest';

const config: Config = {
prettierPath: require.resolve('prettier-2'),
};

export default config;
```

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.

</details>

### `projects` \[array&lt;string | ProjectConfig&gt;]

Default: `undefined`
Expand Down
18 changes: 8 additions & 10 deletions e2e/__tests__/toMatchInlineSnapshotWithPretttier3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ afterAll(() => {
cleanup(JEST_CONFIG_PATH);
});

test('throws correct error', () => {
test('supports passing `null` as `prettierPath`', () => {
writeFiles(DIR, {
'jest.config.js': `
module.exports = {prettierPath: require.resolve('prettier')};
module.exports = {prettierPath: null};
`,
});
writeFiles(TESTS_DIR, {
Expand All @@ -42,16 +42,14 @@ test('throws correct error', () => {
`,
});
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
expect(stderr).toContain(
'Jest: Inline Snapshots are not supported when using Prettier 3.0.0 or above.',
);
expect(exitCode).toBe(1);
expect(stderr).toContain('Snapshots: 1 written, 1 total');
expect(exitCode).toBe(0);
});

test('supports passing `null` as `prettierPath`', () => {
test('supports passing `prettier-2` as `prettierPath`', () => {
writeFiles(DIR, {
'jest.config.js': `
module.exports = {prettierPath: null};
module.exports = {prettierPath: require.resolve('prettier-2')};
`,
});
writeFiles(TESTS_DIR, {
Expand All @@ -66,10 +64,10 @@ test('supports passing `null` as `prettierPath`', () => {
expect(exitCode).toBe(0);
});

test('supports passing `prettier-2` as `prettierPath`', () => {
test('supports passing `prettier` as `prettierPath`', () => {
writeFiles(DIR, {
'jest.config.js': `
module.exports = {prettierPath: require.resolve('prettier-2')};
module.exports = {prettierPath: require.resolve('prettier')};
`,
});
writeFiles(TESTS_DIR, {
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"jest-util": "workspace:*",
"natural-compare": "^1.4.0",
"pretty-format": "workspace:*",
"semver": "^7.5.3"
"semver": "^7.5.3",
"synckit": "^0.8.5"
},
"devDependencies": {
"@babel/preset-flow": "^7.7.2",
Expand All @@ -46,11 +47,12 @@
"@types/babel__core": "^7.1.14",
"@types/graceful-fs": "^4.1.3",
"@types/natural-compare": "^1.4.0",
"@types/prettier": "^2.1.5",
"@types/prettier-v2": "npm:@types/prettier@^2.1.5",
"@types/semver": "^7.1.0",
"ansi-regex": "^5.0.1",
"ansi-styles": "^5.0.0",
"prettier": "^2.1.1",
"prettier": "^3.0.3",
"prettier-v2": "npm:prettier@^2.1.5",
"tsd-lite": "^0.8.0"
},
"engines": {
Expand Down
Loading

0 comments on commit 769ee6e

Please sign in to comment.