Skip to content

Commit d9f5e68

Browse files
JoostKcpojer
andauthored
fix: use parsed source text for Prettier formatting (#15764)
Co-authored-by: Christoph Nakazawa <[email protected]>
1 parent a787c53 commit d9f5e68

File tree

5 files changed

+112
-8
lines changed

5 files changed

+112
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
## main
22

3+
34
### Features
45

56
- `[jest-runtime]` Reduce redundant ReferenceError messages
67
- `[jest-core]` Include test modules that failed to load when --onlyFailures is active
78

89
### Fixes
910

11+
- `[jest-snapshot]` Fix mangled inline snapshot updates when used with Prettier 3 and CRLF line endings
1012
- `[jest-runtime]` Importing from `@jest/globals` in more than one file no longer breaks relative paths ([#15772](https://github.com/jestjs/jest/issues/15772))
1113

1214
# Chore
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 * as fs from 'graceful-fs';
10+
import {cleanup, runYarnInstall, writeFiles} from '../Utils';
11+
import runJest from '../runJest';
12+
13+
const DIR = path.resolve(__dirname, '../to-match-inline-snapshot-crlf');
14+
const TESTS_DIR = path.resolve(DIR, '__tests__');
15+
const JEST_CONFIG_PATH = path.resolve(DIR, 'jest.config.js');
16+
const PRETTIER_RC_PATH = path.resolve(DIR, '.prettierrc');
17+
18+
const readFile = (filename: string) =>
19+
fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8');
20+
21+
beforeAll(() => {
22+
runYarnInstall(DIR);
23+
});
24+
beforeEach(() => {
25+
cleanup(TESTS_DIR);
26+
cleanup(JEST_CONFIG_PATH);
27+
cleanup(PRETTIER_RC_PATH);
28+
});
29+
afterAll(() => {
30+
cleanup(TESTS_DIR);
31+
cleanup(JEST_CONFIG_PATH);
32+
cleanup(PRETTIER_RC_PATH);
33+
});
34+
35+
test('prettier with crlf newlines', () => {
36+
writeFiles(DIR, {
37+
'jest.config.js': `
38+
module.exports = {prettierPath: require.resolve('prettier')};
39+
`,
40+
});
41+
writeFiles(DIR, {
42+
'.prettierrc': '{"endOfLine": "crlf"}',
43+
});
44+
writeFiles(TESTS_DIR, {
45+
'test.ts': `test('snapshots', () => {
46+
expect({test: 1}).toMatchInlineSnapshot();
47+
48+
expect({test: 2}).toMatchInlineSnapshot();
49+
});
50+
`.replaceAll('\n', '\r\n'),
51+
});
52+
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
53+
expect(stderr).toContain('Snapshots: 2 written, 2 total');
54+
expect(exitCode).toBe(0);
55+
56+
const fileAfter = readFile('test.ts');
57+
expect(fileAfter).toBe(
58+
`test("snapshots", () => {
59+
expect({ test: 1 }).toMatchInlineSnapshot(\`
60+
{
61+
"test": 1,
62+
}
63+
\`);
64+
65+
expect({ test: 2 }).toMatchInlineSnapshot(\`
66+
{
67+
"test": 2,
68+
}
69+
\`);
70+
});
71+
`.replaceAll('\n', '\r\n'),
72+
);
73+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"prettier": "^3.0.0"
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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: 8
6+
cacheKey: 10
7+
8+
"prettier@npm:^3.0.0":
9+
version: 3.5.3
10+
resolution: "prettier@npm:3.5.3"
11+
bin:
12+
prettier: bin/prettier.cjs
13+
checksum: 10/7050c08f674d9e49fbd9a4c008291d0715471f64e94cc5e4b01729affce221dfc6875c8de7e66b728c64abc9352eefb7eaae071b5f79d30081be207b53774b78
14+
languageName: node
15+
linkType: hard
16+
17+
"root-workspace-0b6124@workspace:.":
18+
version: 0.0.0-use.local
19+
resolution: "root-workspace-0b6124@workspace:."
20+
dependencies:
21+
prettier: "npm:^3.0.0"
22+
languageName: unknown
23+
linkType: soft

packages/jest-snapshot/src/worker.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ runAsWorker(
4949
parser: inferredParser,
5050
});
5151

52-
// @ts-expect-error private API
53-
const {ast} = await prettier.__debug.parse(sourceFileWithSnapshots, {
54-
...config,
55-
filepath,
56-
originalText: sourceFileWithSnapshots,
57-
parser: inferredParser,
58-
});
52+
const {ast, text: parsedSourceFileWithSnapshots} =
53+
// @ts-expect-error private API
54+
await prettier.__debug.parse(sourceFileWithSnapshots, {
55+
...config,
56+
filepath,
57+
originalText: sourceFileWithSnapshots,
58+
parser: inferredParser,
59+
});
5960
processPrettierAst(ast, config, snapshotMatcherNames, true);
6061
// Snapshots have now been inserted. Run prettier to make sure that the code is
6162
// formatted, except snapshot indentation. Snapshots cannot be formatted until
@@ -66,7 +67,7 @@ runAsWorker(
6667
const formatAST = await prettier.__debug.formatAST(ast, {
6768
...config,
6869
filepath,
69-
originalText: sourceFileWithSnapshots,
70+
originalText: parsedSourceFileWithSnapshots,
7071
parser: inferredParser,
7172
});
7273
return formatAST.formatted;

0 commit comments

Comments
 (0)