Skip to content

Commit

Permalink
Apply Simen's fix
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 27, 2022
1 parent bf75889 commit 4fd47bd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Expand Up @@ -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,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/src/setup_jest_globals.ts
Expand Up @@ -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,
});
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -46,6 +46,7 @@ export type InlineSnapshot = {

export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
rootDir: string,
prettierPath: string,
): void {
let prettier: Prettier | null = null;
Expand All @@ -64,6 +65,7 @@ export function saveInlineSnapshots(
saveSnapshotsForFile(
snapshotsByFile[sourceFilePath],
sourceFilePath,
rootDir,
prettier && semver.gte(prettier.version, '1.5.0') ? prettier : undefined,
);
}
Expand All @@ -72,6 +74,7 @@ export function saveInlineSnapshots(
const saveSnapshotsForFile = (
snapshots: Array<InlineSnapshot>,
sourceFilePath: string,
rootDir: string,
prettier?: Prettier,
) => {
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');
Expand All @@ -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}`);
Expand Down
9 changes: 8 additions & 1 deletion packages/jest-snapshot/src/State.ts
Expand Up @@ -27,6 +27,7 @@ export type SnapshotStateOptions = {
prettierPath: string;
expand?: boolean;
snapshotFormat: PrettyFormatOptions;
rootDir: string;
};

export type SnapshotMatchOptions = {
Expand Down Expand Up @@ -64,6 +65,7 @@ export default class SnapshotState {
private _uncheckedKeys: Set<string>;
private _prettierPath: string;
private _snapshotFormat: PrettyFormatOptions;
private _rootDir: string;

added: number;
expand: boolean;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 4fd47bd

Please sign in to comment.