From 59cf7eae4b29f5a013614eed638531684779c428 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 1 Mar 2023 13:26:51 -0300 Subject: [PATCH 1/5] add inline snapshots support to cts and mts files --- packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index e9a601591bfc..cc187a1be498 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]?tsx?$/.test(sourceFilePath)) { plugins.push([ require.resolve('@babel/plugin-syntax-typescript'), {isTSX: sourceFilePath.endsWith('x')}, From 933c446067e33290345225c9eec8ad362b6da22d Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 1 Mar 2023 13:47:04 -0300 Subject: [PATCH 2/5] Update InlineSnapshots.test.ts --- .../src/__tests__/InlineSnapshots.test.ts | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index c4eca34fcf28..8b234a1c844b 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'); From 4bd51806ee16cde694aaf7e670052d1e60bc06ab Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 1 Mar 2023 14:00:19 -0300 Subject: [PATCH 3/5] Update InlineSnapshots.ts --- packages/jest-snapshot/src/InlineSnapshots.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/InlineSnapshots.ts b/packages/jest-snapshot/src/InlineSnapshots.ts index cc187a1be498..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 (/\.[cm]?tsx?$/.test(sourceFilePath)) { + if (/\.([cm]?ts|tsx)$/.test(sourceFilePath)) { plugins.push([ require.resolve('@babel/plugin-syntax-typescript'), {isTSX: sourceFilePath.endsWith('x')}, From 940c59efd4817386a7811493f0a189f8ec531eaf Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 1 Mar 2023 14:05:31 -0300 Subject: [PATCH 4/5] Update InlineSnapshots.test.ts --- packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts index 8b234a1c844b..f40d40f5c74e 100644 --- a/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts +++ b/packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts @@ -167,7 +167,7 @@ const a: [Foo, Foo] = [{ foo: 'one' }, { foo: 'two' }]; expect(a).toMatchInlineSnapshot(\`[{ foo: 'one' }, { foo: 'two' }]\`); `.trim()}\n`, ); - } + }, ); test('saveInlineSnapshots() can handle tsx without prettier', () => { From cdbb46aa199fea870e4edc8ce2d6fbd225aa242b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 2 Mar 2023 19:27:34 +0100 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eff6af9f3dac..8a2b26548fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,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