-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inline Snapshots fail to update if test uses JSX #11561
Comments
@SimenB Reproduction:
I was hoping this is just an issue with duplicate modules but it still reproduces after I ran This issue is blocking facebook/react#21575 |
The issue is that Passing through Regression introduced in #7792, fwiw |
This diff seems sensible to me, but not 100% sure. diff --git i/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts w/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts
index cd2781a1af..bdbe8035d4 100644
--- i/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts
+++ w/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts
@@ -156,6 +156,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 i/packages/jest-jasmine2/src/setup_jest_globals.ts w/packages/jest-jasmine2/src/setup_jest_globals.ts
index 1a8cec06c9..add4a72f4c 100644
--- i/packages/jest-jasmine2/src/setup_jest_globals.ts
+++ w/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 i/packages/jest-snapshot/src/InlineSnapshots.ts w/packages/jest-snapshot/src/InlineSnapshots.ts
index 4f1310528f..1bc45aeac5 100644
--- i/packages/jest-snapshot/src/InlineSnapshots.ts
+++ w/packages/jest-snapshot/src/InlineSnapshots.ts
@@ -46,6 +46,7 @@ export type InlineSnapshot = {
export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
+ 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<InlineSnapshot>,
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 i/packages/jest-snapshot/src/State.ts w/packages/jest-snapshot/src/State.ts
index fc96a3b47a..ef424b8bfe 100644
--- i/packages/jest-snapshot/src/State.ts
+++ w/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<string>;
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)) { |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
When updating inline snapshots, if the test file includes any JSX, it will fail with a
Support for the experimental syntax 'jsx' isn't currently enabled
SyntaxError.To Reproduce
Steps to reproduce the behavior:
-u
flagIf the test is run without the
-u
flag, the test fails due to a snapshot mismatch. If the snapshot is manually fixed to be correct, the test passes without errors. If the JSX is in a separate file and imported, the update works fine.Expected behavior
The inline snapshot is updated
Link to repl or repo (highly encouraged)
https://replit.com/@illandril/EasyCarelessInteger
envinfo
The text was updated successfully, but these errors were encountered: