diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 6e00f54eed5f..f348e9006932 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -114,6 +114,7 @@ export const initialize = async ({ const snapshotState = new SnapshotState(snapshotPath, { expand: globalConfig.expand, prettierPath: config.prettierPath, + rootDir: config.rootDir, snapshotFormat: config.snapshotFormat, updateSnapshot: globalConfig.updateSnapshot, }); diff --git a/packages/jest-jasmine2/src/setup_jest_globals.ts b/packages/jest-jasmine2/src/setup_jest_globals.ts index 1a8cec06c940..add4a72f4c7a 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -104,12 +104,13 @@ export default async function setupJestGlobals({ patchJasmine(); const {expand, updateSnapshot} = globalConfig; - const {prettierPath, snapshotFormat} = config; + const {prettierPath, rootDir, snapshotFormat} = config; const snapshotResolver = await buildSnapshotResolver(config, localRequire); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, prettierPath, + rootDir, snapshotFormat, updateSnapshot, }); diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 4f1310528f47..1bc45aeac5dd 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -46,6 +46,7 @@ export type InlineSnapshot = { export function saveInlineSnapshots( snapshots: Array, + rootDir: string, prettierPath: string, ): void { let prettier: Prettier | null = null; @@ -64,6 +65,7 @@ export function saveInlineSnapshots( saveSnapshotsForFile( snapshotsByFile[sourceFilePath], sourceFilePath, + rootDir, prettier && semver.gte(prettier.version, '1.5.0') ? prettier : undefined, ); } @@ -72,6 +74,7 @@ export function saveInlineSnapshots( const saveSnapshotsForFile = ( snapshots: Array, sourceFilePath: string, + rootDir: string, prettier?: Prettier, ) => { const sourceFile = fs.readFileSync(sourceFilePath, 'utf8'); @@ -96,7 +99,7 @@ const saveSnapshotsForFile = ( filename: sourceFilePath, plugins, presets, - root: path.dirname(sourceFilePath), + root: rootDir, }); if (!ast) { throw new Error(`jest-snapshot: Failed to parse ${sourceFilePath}`); diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index fc96a3b47add..ef424b8bfe48 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -27,6 +27,7 @@ export type SnapshotStateOptions = { prettierPath: string; expand?: boolean; snapshotFormat: PrettyFormatOptions; + rootDir: string; }; export type SnapshotMatchOptions = { @@ -64,6 +65,7 @@ export default class SnapshotState { private _uncheckedKeys: Set; private _prettierPath: string; private _snapshotFormat: PrettyFormatOptions; + private _rootDir: string; added: number; expand: boolean; @@ -92,6 +94,7 @@ export default class SnapshotState { this._updateSnapshot = options.updateSnapshot; this.updated = 0; this._snapshotFormat = options.snapshotFormat; + this._rootDir = options.rootDir; } markSnapshotsAsCheckedForTest(testName: string): void { @@ -154,7 +157,11 @@ export default class SnapshotState { saveSnapshotFile(this._snapshotData, this._snapshotPath); } if (hasInlineSnapshots) { - saveInlineSnapshots(this._inlineSnapshots, this._prettierPath); + saveInlineSnapshots( + this._inlineSnapshots, + this._rootDir, + this._prettierPath, + ); } status.saved = true; } else if (!hasExternalSnapshots && fs.existsSync(this._snapshotPath)) {