From 0c15eb647c1fdfb4fddfbd8a1dcf235696158306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Borges?= Date: Wed, 28 Sep 2022 04:18:14 -0300 Subject: [PATCH] fix: prioritize parser used in the project (#13323) --- CHANGELOG.md | 1 + packages/jest-snapshot/src/InlineSnapshots.ts | 9 +++-- .../src/__tests__/InlineSnapshots.test.ts | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) 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 diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index 53d644e63c48..1107d5fdca39 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -297,14 +297,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 74303a23a20e..b3cee8cc1f5b 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -689,3 +689,43 @@ test('saveInlineSnapshots() indents awaited snapshots with spaces', () => { '});\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', + ); + + jest.mocked(prettier.resolveConfig.sync).mockReturnValue({ + parser: 'flow', + }); + + const prettierSpy = jest.spyOn(prettier.getFileInfo, 'sync'); + + saveInlineSnapshots( + [ + { + frame: {column: 15, file: filename, line: 5} as Frame, + snapshot: 'a', + }, + ], + dir, + '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', + ); +});