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

Support Prettier 3 #14566

Merged
merged 9 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/index.cjs')};
Copy link
Member

Choose a reason for hiding this comment

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

why not just prettier?

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 @fisker 's suggestion, perhaps to avoid unexpected use of Prettier 2?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I mean both index.cjs and index.mjs both should be tested, since user can pass prettier path.

Copy link
Member

Choose a reason for hiding this comment

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

We use require to load it, so ESM will fail

Copy link
Contributor

@fisker fisker Sep 25, 2023

Choose a reason for hiding this comment

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

The debug apis was actually missing in commonjs version at first, I added in prettier/prettier#13731

Copy link
Contributor

Choose a reason for hiding this comment

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

We use require to load it, so ESM will fail

Al right, didn't know.

SimenB marked this conversation as resolved.
Show resolved Hide resolved
`,
});
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