From 3d345a0ca78dce1e3f7e3cbbf373ebb4d324fe9d Mon Sep 17 00:00:00 2001 From: JVBorges Date: Mon, 26 Sep 2022 14:29:22 -0300 Subject: [PATCH 1/3] fix(snapshot): priotize parser used in the project --- packages/jest-snapshot/src/InlineSnapshots.ts | 9 +++-- .../src/__tests__/InlineSnapshots.test.ts | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 2324ae7372fc..b5cdfa2452c0 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -267,14 +267,15 @@ const runPrettier = ( ? prettier.resolveConfig.sync(sourceFilePath, {editorconfig: true}) : null; - // Detect the parser for the test file. + // Prioritize parser found in the project config. + // If not found detect the parser for the test file. // For older versions of Prettier, fallback to a simple parser detection. // @ts-expect-error - `inferredParser` is `string` const inferredParser: PrettierParserName | null | undefined = - prettier.getFileInfo + (config && typeof config.parser === 'string' && config.parser) || + (prettier.getFileInfo ? prettier.getFileInfo.sync(sourceFilePath).inferredParser - : (config && typeof config.parser === 'string' && config.parser) || - simpleDetectParser(sourceFilePath); + : simpleDetectParser(sourceFilePath)); if (!inferredParser) { throw new Error( diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index 1d8940556379..4eb285e417cb 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -629,3 +629,42 @@ test('saveInlineSnapshots() does not indent empty lines', () => { ' `));\n', ); }); + +test('saveInlineSnapshots() prioritize parser from project/editor configuration', () => { + const filename = path.join(dir, 'my.test.js'); + fs.writeFileSync( + filename, + 'const foo = {\n' + + ' "1": "Some value",\n' + + '};\n' + + 'test("something", () => {\n' + + ' expect("a").toMatchInlineSnapshot();\n' + + '});\n', + ); + + (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({ + parser: 'flow', + }); + + const prettierSpy = jest.spyOn(prettier.getFileInfo, 'sync'); + + saveInlineSnapshots( + [ + { + frame: {column: 15, file: filename, line: 5} as Frame, + snapshot: 'a', + }, + ], + 'prettier', + ); + + expect(prettierSpy).not.toBeCalled(); + expect(fs.readFileSync(filename, 'utf-8')).toBe( + 'const foo = {\n' + + ' "1": "Some value",\n' + + '};\n' + + 'test("something", () => {\n' + + ' expect("a").toMatchInlineSnapshot(`a`);\n' + + '});\n', + ); +}); From 6733d186989c3b239390ef5a7f9a82c2f279c1b3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Sep 2022 09:04:35 +0200 Subject: [PATCH 2/3] tweak --- packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index 07b0676facc6..b3cee8cc1f5b 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -702,7 +702,7 @@ test('saveInlineSnapshots() prioritize parser from project/editor configuration' '});\n', ); - (prettier.resolveConfig.sync as jest.Mock).mockReturnValue({ + jest.mocked(prettier.resolveConfig.sync).mockReturnValue({ parser: 'flow', }); @@ -715,6 +715,7 @@ test('saveInlineSnapshots() prioritize parser from project/editor configuration' snapshot: 'a', }, ], + dir, 'prettier', ); From dd5b573ced50afe891e83f97b81d9f454104af3b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Sep 2022 09:05:36 +0200 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f45933d4ecb..8ab58462f3c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - `[jest-haste-map]` Remove `__proto__` usage ([#13256](https://github.com/facebook/jest/pull/13256)) - `[jest-mock]` Improve `spyOn` typings to handle optional properties ([#13247](https://github.com/facebook/jest/pull/13247)) - `[jest-snapshot]` Throw useful error when an array is passed as property matchers ([#13263](https://github.com/facebook/jest/pull/13263)) +- `[jest-snapshot]` Prioritize parser used in the project ([#13323](https://github.com/facebook/jest/pull/13323)) ### Chore & Maintenance