Skip to content

Commit 4fd47bd

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

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

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)