From 586ac6e712548ee4bc8987bc6e66ac1093316d0f Mon Sep 17 00:00:00 2001
From: eps1lon <silbermann.sebastian@gmail.com>
Date: Wed, 27 Apr 2022 21:34:46 +0200
Subject: [PATCH] Apply Simen's fix

Source: https://github.com/facebook/jest/issues/11561#issuecomment-1087862880
---
 .../toMatchInlineSnapshotWithJSX.test.ts      | 32 +++++++++----------
 .../MismatchingSnapshot.original.js           |  7 ++++
 .../__tests__/MismatchingSnapshot.test.js     |  7 ++++
 .../jestAdapterInit.ts                        |  1 +
 .../jest-jasmine2/src/setup_jest_globals.ts   |  3 +-
 packages/jest-snapshot/src/InlineSnapshots.ts |  5 ++-
 packages/jest-snapshot/src/State.ts           |  9 +++++-
 7 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts b/e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts
index e8c82821b63b..adf288f69706 100644
--- a/e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts
+++ b/e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts
@@ -33,15 +33,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
   expect(updateSnapshotRun.json.testResults[0].message).toMatchInlineSnapshot(`
     "  ● Test suite failed to run
 
-        SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (5:26):
+        SyntaxError: /home/eps1lon/Development/forks/jest/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (12:26):
 
-          3 |
-          4 | test('<div>x</div>', () => {
-        > 5 |   expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
-            |                          ^
-          6 |     <div>
-          7 |       y
-          8 |     </div>
+          10 |
+          11 | test('<div>x</div>', () => {
+        > 12 |   expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
+             |                          ^
+          13 |     <div>
+          14 |       y
+          15 |     </div>
 
         Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
         If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
@@ -66,15 +66,15 @@ it('successfully runs the tests inside `to-match-inline-snapshot-with-jsx/`', ()
         +   x
           </div>
 
-          3 |
-          4 | test('<div>x</div>', () => {
-        > 5 |   expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
-            |                                                  ^
-          6 |     <div>
-          7 |       y
-          8 |     </div>
+          10 |
+          11 | test('<div>x</div>', () => {
+        > 12 |   expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
+             |                                                  ^
+          13 |     <div>
+          14 |       y
+          15 |     </div>
 
-          at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
+          at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:12:50)
     "
   `);
 });
diff --git a/e2e/to-match-inline-snapshot-with-jsx/MismatchingSnapshot.original.js b/e2e/to-match-inline-snapshot-with-jsx/MismatchingSnapshot.original.js
index 24b44b9139fa..09fdddae3d69 100644
--- a/e2e/to-match-inline-snapshot-with-jsx/MismatchingSnapshot.original.js
+++ b/e2e/to-match-inline-snapshot-with-jsx/MismatchingSnapshot.original.js
@@ -1,3 +1,10 @@
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
 import React from 'react';
 import renderer from 'react-test-renderer';
 
diff --git a/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js b/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js
index 24b44b9139fa..09fdddae3d69 100644
--- a/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js
+++ b/e2e/to-match-inline-snapshot-with-jsx/__tests__/MismatchingSnapshot.test.js
@@ -1,3 +1,10 @@
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
 import React from 'react';
 import renderer from 'react-test-renderer';
 
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<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 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<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)) {