Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
Apply Simen's fix
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and SimenB committed Aug 19, 2022
1 parent 70df38c commit 6dc85d6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type InlineSnapshot = {

export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
rootDir: string,
prettierPath: string | null,
): 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 | undefined,
) => {
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type SnapshotStateOptions = {
prettierPath?: string | null;
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 | null;
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 6dc85d6

Please sign in to comment.