Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
## main


### Features

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

### Fixes

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

# Chore
Expand Down
73 changes: 73 additions & 0 deletions e2e/__tests__/toMatchInlineSnapshotCrlf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import * as fs from 'graceful-fs';
import {cleanup, runYarnInstall, writeFiles} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../to-match-inline-snapshot-crlf');
const TESTS_DIR = path.resolve(DIR, '__tests__');
const JEST_CONFIG_PATH = path.resolve(DIR, 'jest.config.js');
const PRETTIER_RC_PATH = path.resolve(DIR, '.prettierrc');

const readFile = (filename: string) =>
fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8');

beforeAll(() => {
runYarnInstall(DIR);
});
beforeEach(() => {
cleanup(TESTS_DIR);
cleanup(JEST_CONFIG_PATH);
cleanup(PRETTIER_RC_PATH);
});
afterAll(() => {
cleanup(TESTS_DIR);
cleanup(JEST_CONFIG_PATH);
cleanup(PRETTIER_RC_PATH);
});

test('prettier with crlf newlines', () => {
writeFiles(DIR, {
'jest.config.js': `
module.exports = {prettierPath: require.resolve('prettier')};
`,
});
writeFiles(DIR, {
'.prettierrc': '{"endOfLine": "crlf"}',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this file cannot exist in the to-match-inline-snapshot-crlf test directory, as it would cause Jest's own usage of Prettier to format everything in the directory to have CRLF line endings, which then get normalized to LF by git and cause Prettier checks to fail, as CRLF is expected. An alternative could be to set up a dedicated .gitattributes file in the folder to instruct Git to use CRLF for this directory wholesale, but I'd rather not check in any CRLF into Git at all.

});
writeFiles(TESTS_DIR, {
'test.ts': `test('snapshots', () => {
expect({test: 1}).toMatchInlineSnapshot();

expect({test: 2}).toMatchInlineSnapshot();
});
`.replaceAll('\n', '\r\n'),
});
const {stderr, exitCode} = runJest(DIR, ['--ci=false']);
expect(stderr).toContain('Snapshots: 2 written, 2 total');
expect(exitCode).toBe(0);

const fileAfter = readFile('test.ts');
expect(fileAfter).toBe(
`test("snapshots", () => {
expect({ test: 1 }).toMatchInlineSnapshot(\`
{
"test": 1,
}
\`);

expect({ test: 2 }).toMatchInlineSnapshot(\`
{
"test": 2,
}
\`);
});
`.replaceAll('\n', '\r\n'),
);
});
5 changes: 5 additions & 0 deletions e2e/to-match-inline-snapshot-crlf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"prettier": "^3.0.0"
}
}
23 changes: 23 additions & 0 deletions e2e/to-match-inline-snapshot-crlf/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10

"prettier@npm:^3.0.0":
version: 3.5.3
resolution: "prettier@npm:3.5.3"
bin:
prettier: bin/prettier.cjs
checksum: 10/7050c08f674d9e49fbd9a4c008291d0715471f64e94cc5e4b01729affce221dfc6875c8de7e66b728c64abc9352eefb7eaae071b5f79d30081be207b53774b78
languageName: node
linkType: hard

"root-workspace-0b6124@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
prettier: "npm:^3.0.0"
languageName: unknown
linkType: soft
17 changes: 9 additions & 8 deletions packages/jest-snapshot/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ runAsWorker(
parser: inferredParser,
});

// @ts-expect-error private API
const {ast} = await prettier.__debug.parse(sourceFileWithSnapshots, {
...config,
filepath,
originalText: sourceFileWithSnapshots,
parser: inferredParser,
});
const {ast, text: parsedSourceFileWithSnapshots} =
// @ts-expect-error private API
await prettier.__debug.parse(sourceFileWithSnapshots, {
...config,
filepath,
originalText: sourceFileWithSnapshots,
parser: inferredParser,
});
processPrettierAst(ast, config, snapshotMatcherNames, true);
// Snapshots have now been inserted. Run prettier to make sure that the code is
// formatted, except snapshot indentation. Snapshots cannot be formatted until
Expand All @@ -66,7 +67,7 @@ runAsWorker(
const formatAST = await prettier.__debug.formatAST(ast, {
...config,
filepath,
originalText: sourceFileWithSnapshots,
originalText: parsedSourceFileWithSnapshots,
parser: inferredParser,
});
return formatAST.formatted;
Expand Down
Loading