Skip to content

Commit 586ac6e

Browse files
committed
Apply Simen's fix
Source: #11561 (comment)
1 parent bf75889 commit 586ac6e

File tree

7 files changed

+45
-19
lines changed

7 files changed

+45
-19
lines changed

e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
3333
expect(updateSnapshotRun.json.testResults[0].message).toMatchInlineSnapshot(`
3434
" ● Test suite failed to run
3535
36-
SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (5:26):
36+
SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (12:26):
3737
38-
3 |
39-
4 | test('<div>x</div>', () => {
40-
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
41-
| ^
42-
6 | <div>
43-
7 | y
44-
8 | </div>
38+
10 |
39+
11 | test('<div>x</div>', () => {
40+
> 12 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
41+
| ^
42+
13 | <div>
43+
14 | y
44+
15 | </div>
4545
4646
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
4747
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
@@ -66,15 +66,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
6666
+ x
6767
</div>
6868
69-
3 |
70-
4 | test('<div>x</div>', () => {
71-
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
72-
| ^
73-
6 | <div>
74-
7 | y
75-
8 | </div>
69+
10 |
70+
11 | test('<div>x</div>', () => {
71+
> 12 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
72+
| ^
73+
13 | <div>
74+
14 | y
75+
15 | </div>
7676
77-
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
77+
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:12:50)
7878
"
7979
`);
8080
});

e2e/to-match-inline-snapshot-with-jsx/MismatchingSnapshot.original.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
18
import React from 'react';
29
import renderer from 'react-test-renderer';
310

e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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+
18
import React from 'react';
29
import renderer from 'react-test-renderer';
310

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export const initialize = async ({
114114
const snapshotState = new SnapshotState(snapshotPath, {
115115
expand: globalConfig.expand,
116116
prettierPath: config.prettierPath,
117+
rootDir: config.rootDir,
117118
snapshotFormat: config.snapshotFormat,
118119
updateSnapshot: globalConfig.updateSnapshot,
119120
});

packages/jest-jasmine2/src/setup_jest_globals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ export default async function setupJestGlobals({
104104

105105
patchJasmine();
106106
const {expand, updateSnapshot} = globalConfig;
107-
const {prettierPath, snapshotFormat} = config;
107+
const {prettierPath, rootDir, snapshotFormat} = config;
108108
const snapshotResolver = await buildSnapshotResolver(config, localRequire);
109109
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
110110
const snapshotState = new SnapshotState(snapshotPath, {
111111
expand,
112112
prettierPath,
113+
rootDir,
113114
snapshotFormat,
114115
updateSnapshot,
115116
});

packages/jest-snapshot/src/InlineSnapshots.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type InlineSnapshot = {
4646

4747
export function saveInlineSnapshots(
4848
snapshots: Array<InlineSnapshot>,
49+
rootDir: string,
4950
prettierPath: string,
5051
): void {
5152
let prettier: Prettier | null = null;
@@ -64,6 +65,7 @@ export function saveInlineSnapshots(
6465
saveSnapshotsForFile(
6566
snapshotsByFile[sourceFilePath],
6667
sourceFilePath,
68+
rootDir,
6769
prettier && semver.gte(prettier.version, '1.5.0') ? prettier : undefined,
6870
);
6971
}
@@ -72,6 +74,7 @@ export function saveInlineSnapshots(
7274
const saveSnapshotsForFile = (
7375
snapshots: Array<InlineSnapshot>,
7476
sourceFilePath: string,
77+
rootDir: string,
7578
prettier?: Prettier,
7679
) => {
7780
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');
@@ -96,7 +99,7 @@ const saveSnapshotsForFile = (
9699
filename: sourceFilePath,
97100
plugins,
98101
presets,
99-
root: path.dirname(sourceFilePath),
102+
root: rootDir,
100103
});
101104
if (!ast) {
102105
throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`);

packages/jest-snapshot/src/State.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type SnapshotStateOptions = {
2727
prettierPath: string;
2828
expand?: boolean;
2929
snapshotFormat: PrettyFormatOptions;
30+
rootDir: string;
3031
};
3132

3233
export type SnapshotMatchOptions = {
@@ -64,6 +65,7 @@ export default class SnapshotState {
6465
private _uncheckedKeys: Set<string>;
6566
private _prettierPath: string;
6667
private _snapshotFormat: PrettyFormatOptions;
68+
private _rootDir: string;
6769

6870
added: number;
6971
expand: boolean;
@@ -92,6 +94,7 @@ export default class SnapshotState {
9294
this._updateSnapshot = options.updateSnapshot;
9395
this.updated = 0;
9496
this._snapshotFormat = options.snapshotFormat;
97+
this._rootDir = options.rootDir;
9598
}
9699

97100
markSnapshotsAsCheckedForTest(testName: string): void {
@@ -154,7 +157,11 @@ export default class SnapshotState {
154157
saveSnapshotFile(this._snapshotData, this._snapshotPath);
155158
}
156159
if (hasInlineSnapshots) {
157-
saveInlineSnapshots(this._inlineSnapshots, this._prettierPath);
160+
saveInlineSnapshots(
161+
this._inlineSnapshots,
162+
this._rootDir,
163+
this._prettierPath,
164+
);
158165
}
159166
status.saved = true;
160167
} else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) {

0 commit comments

Comments
 (0)