Skip to content

Commit b36d6ed

Browse files
fix(snapshot-utils): Handle Windows line endings (#15800)
1 parent 4d5f41d commit b36d6ed

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
## main
22

3+
### Fixes
4+
5+
- `[jest-snapshot-utils]` Fix deprecated goo.gl snapshot warning not handling Windows end-of-line sequences ([#15800](https://github.com/jestjs/jest/pull/15800))
6+
37
## 30.1.0
48

59
## Features
610

7-
- `[jest-leak-detector]` Configurable GC aggressiveness regarding to V8 heap snapshot generation ([#15793](https://github.com/jestjs/jest/pull/15793/))
11+
- `[jest-leak-detector]` Configurable GC aggressiveness regarding to V8 heap snapshot generation ([#15793](https://github.com/jestjs/jest/pull/15793/))
812
- `[jest-runtime]` Reduce redundant ReferenceError messages
913
- `[jest-core]` Include test modules that failed to load when --onlyFailures is active
1014

1115
### Fixes
1216

13-
- `[jest-snapshot-utils] Fix deprecated goo.gl snapshot guide link not getting replaced with fully canonical URL ([#15787](https://github.com/jestjs/jest/pull/15787))
17+
- `[jest-snapshot-utils]` Fix deprecated goo.gl snapshot guide link not getting replaced with fully canonical URL ([#15787](https://github.com/jestjs/jest/pull/15787))
1418
- `[jest-circus]` Fix `it.concurrent` not working with `describe.skip` ([#15765](https://github.com/jestjs/jest/pull/15765))
1519
- `[jest-snapshot]` Fix mangled inline snapshot updates when used with Prettier 3 and CRLF line endings
1620
- `[jest-runtime]` Importing from `@jest/globals` in more than one file no longer breaks relative paths ([#15772](https://github.com/jestjs/jest/issues/15772))

packages/jest-snapshot-utils/src/__tests__/utils.test.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,33 @@ test('getSnapshotData() throws for older snapshot version', () => {
135135
);
136136
});
137137

138-
test('getSnapshotData() throws for newer snapshot version', () => {
139-
const filename = path.join(__dirname, 'old-snapshot.snap');
140-
jest
141-
.mocked(fs.readFileSync)
142-
.mockReturnValue(
143-
`// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}\n\n` +
144-
'exports[`myKey`] = `<div>\n</div>`;\n',
138+
test.each([
139+
['Linux', '\n'],
140+
['Windows', '\r\n'],
141+
])(
142+
'getSnapshotData() throws for newer snapshot version with %s line endings',
143+
(_: string, fileEol: string) => {
144+
const filename = path.join(__dirname, 'old-snapshot.snap');
145+
jest
146+
.mocked(fs.readFileSync)
147+
.mockReturnValue(
148+
`// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}${fileEol}${fileEol}` +
149+
`exports[\`myKey\`] = \`<div>${fileEol}</div>\`;${fileEol}`,
150+
);
151+
const update = 'none';
152+
153+
expect(() => getSnapshotData(filename, update)).toThrow(
154+
`${chalk.red(
155+
`${chalk.red.bold('Outdated Jest version')}: The version of this ` +
156+
'snapshot file indicates that this project is meant to be used ' +
157+
'with a newer version of Jest. ' +
158+
'The snapshot file version ensures that all developers on a project ' +
159+
'are using the same version of Jest. ' +
160+
'Please update your version of Jest and re-run the tests.',
161+
)}\n\nExpected: v${SNAPSHOT_VERSION}\nReceived: v2`,
145162
);
146-
const update = 'none';
147-
148-
expect(() => getSnapshotData(filename, update)).toThrow(
149-
`${chalk.red(
150-
`${chalk.red.bold('Outdated Jest version')}: The version of this ` +
151-
'snapshot file indicates that this project is meant to be used ' +
152-
'with a newer version of Jest. ' +
153-
'The snapshot file version ensures that all developers on a project ' +
154-
'are using the same version of Jest. ' +
155-
'Please update your version of Jest and re-run the tests.',
156-
)}\n\nExpected: v${SNAPSHOT_VERSION}\nReceived: v2`,
157-
);
158-
});
163+
},
164+
);
159165

160166
test('getSnapshotData() throws for deprecated snapshot guide link', () => {
161167
const deprecatedGuideLink = 'https://goo.gl/fbAQLP';
@@ -170,7 +176,9 @@ test('getSnapshotData() throws for deprecated snapshot guide link', () => {
170176

171177
expect(() => getSnapshotData(filename, update)).toThrow(
172178
`${chalk.red(
173-
`${chalk.red.bold('Outdated guide link')}: The snapshot guide link is outdated.` +
179+
`${chalk.red.bold(
180+
'Outdated guide link',
181+
)}: The snapshot guide link is outdated.` +
174182
'Please update all snapshots while upgrading of Jest',
175183
)}\n\nExpected: ${SNAPSHOT_GUIDE_LINK}\n` +
176184
`Received: ${deprecatedGuideLink}`,

packages/jest-snapshot-utils/src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {Config} from '@jest/types';
1313
import type {SnapshotData} from './types';
1414

1515
export const SNAPSHOT_VERSION = '1';
16-
const SNAPSHOT_HEADER_REGEXP = /^\/\/ Jest Snapshot v(.+), (.+)\n/;
16+
const SNAPSHOT_HEADER_REGEXP = /^\/\/ Jest Snapshot v(.+), (.+)\r?\n/;
1717
export const SNAPSHOT_GUIDE_LINK = 'https://jestjs.io/docs/snapshot-testing';
1818
export const SNAPSHOT_VERSION_WARNING = chalk.yellow(
1919
`${chalk.bold('Warning')}: Before you upgrade snapshots, ` +
@@ -77,7 +77,9 @@ const validateSnapshotHeader = (snapshotContents: string) => {
7777
return new Error(
7878
// eslint-disable-next-line prefer-template
7979
chalk.red(
80-
`${chalk.red.bold('Outdated guide link')}: The snapshot guide link is outdated.` +
80+
`${chalk.red.bold(
81+
'Outdated guide link',
82+
)}: The snapshot guide link is outdated.` +
8183
'Please update all snapshots while upgrading of Jest',
8284
) +
8385
'\n\n' +

0 commit comments

Comments
 (0)