From 4156f8678b548ba64c0f3a78abc8579f65f81a91 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 2 Mar 2023 16:21:01 -0300 Subject: [PATCH] feat(jest-snapshot): add support to cts and mts typescript files to inline snapshots (#13975) --- CHANGELOG.md | 1 + packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- .../src/__tests__/InlineSnapshots.test.ts | 43 ++++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d64019f12e40..69bbea1761ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[jest-message-util]` Add support for [AggregateError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946) & [#13947](https://github.com/facebook/jest/pull/13947)) - `[jest-message-util]` Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in `test` and `it` ([#13935](https://github.com/facebook/jest/pull/13935) & [#13966](https://github.com/facebook/jest/pull/13966)) - `[jest-reporters]` Add `summaryThreshold` option to summary reporter to allow overriding the internal threshold that is used to print the summary of all failed tests when the number of test suites surpasses it ([#13895](https://github.com/facebook/jest/pull/13895)) +- `[jest-snapshot]` Add support to `cts` and `mts` TypeScript files to inline snapshots ([#13975](https://github.com/facebook/jest/pull/13975)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) ### Fixes diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index e9a601591bfc..7871cee4ecce 100644 --- a/packages/jest-snapshot/src/InlineSnapshots.ts +++ b/packages/jest-snapshot/src/InlineSnapshots.ts @@ -81,7 +81,7 @@ const saveSnapshotsForFile = ( // TypeScript projects may not have a babel config; make sure they can be parsed anyway. const presets = [require.resolve('babel-preset-current-node-syntax')]; const plugins: Array = []; - if (/\.tsx?$/.test(sourceFilePath)) { + if (/\.([cm]?ts|tsx)$/.test(sourceFilePath)) { plugins.push([ require.resolve('@babel/plugin-syntax-typescript'), {isTSX: sourceFilePath.endsWith('x')}, diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index c4eca34fcf28..f40d40f5c74e 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -132,40 +132,43 @@ expect(a).toMatchInlineSnapshot(\`[1, 2]\`); ); }); -test('saveInlineSnapshots() can handle typescript without prettier', () => { - const filename = path.join(dir, 'my.test.ts'); - fs.writeFileSync( - filename, - `${` +test.each([['ts'], ['cts'], ['mts']])( + 'saveInlineSnapshots() can handle typescript without prettier - %s extension', + extension => { + const filename = path.join(dir, `my.test.${extension}`); + fs.writeFileSync( + filename, + `${` interface Foo { foo: string } const a: [Foo, Foo] = [{ foo: 'one' }, { foo: 'two' }]; expect(a).toMatchInlineSnapshot(); `.trim()}\n`, - ); + ); - saveInlineSnapshots( - [ - { - frame: {column: 11, file: filename, line: 5} as Frame, - snapshot: "[{ foo: 'one' }, { foo: 'two' }]", - }, - ], - dir, - null, - ); + saveInlineSnapshots( + [ + { + frame: {column: 11, file: filename, line: 5} as Frame, + snapshot: "[{ foo: 'one' }, { foo: 'two' }]", + }, + ], + dir, + null, + ); - expect(fs.readFileSync(filename, 'utf8')).toBe( - `${` + expect(fs.readFileSync(filename, 'utf8')).toBe( + `${` interface Foo { foo: string } const a: [Foo, Foo] = [{ foo: 'one' }, { foo: 'two' }]; expect(a).toMatchInlineSnapshot(\`[{ foo: 'one' }, { foo: 'two' }]\`); `.trim()}\n`, - ); -}); + ); + }, +); test('saveInlineSnapshots() can handle tsx without prettier', () => { const filename = path.join(dir, 'my.test.tsx');