From effae717369860e16cb0ccbf24027651493b9bf1 Mon Sep 17 00:00:00 2001 From: Ahn <27772165+ahnpnl@users.noreply.github.com> Date: Mon, 25 Jul 2022 19:03:00 +0200 Subject: [PATCH] fix: allow `.mts` to be processed (#3713) Fixes #3702 --- src/constants.ts | 8 ++-- src/legacy/ts-jest-transformer.spec.ts | 47 ++++++++++--------- .../create-jest-preset.spec.ts.snap | 1 + 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index b35e97f3b0..bbf6727984 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,10 +1,10 @@ export const LINE_FEED = '\n' export const DECLARATION_TYPE_EXT = '.d.ts' export const JS_JSX_EXTENSIONS = ['.js', '.jsx'] -export const TS_TSX_REGEX = /\.tsx?$/ -export const JS_JSX_REGEX = /\.jsx?$/ -// `extensionsToTreatAsEsm` only accepts `.ts`, `.tsx` and `.jsx`. `.js`, `.cjs`, `.mjs` will throw error -export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx'] +export const TS_TSX_REGEX = /\.m?tsx?$/ +export const JS_JSX_REGEX = /\.m?jsx?$/ +// `extensionsToTreatAsEsm` will throw error with `.mjs` +export const TS_EXT_TO_TREAT_AS_ESM = ['.ts', '.tsx', '.mts'] export const JS_EXT_TO_TREAT_AS_ESM = ['.jsx'] /** * @internal diff --git a/src/legacy/ts-jest-transformer.spec.ts b/src/legacy/ts-jest-transformer.spec.ts index 0ebe8d0b52..34cbdef4d1 100644 --- a/src/legacy/ts-jest-transformer.spec.ts +++ b/src/legacy/ts-jest-transformer.spec.ts @@ -376,7 +376,7 @@ describe('TsJestTransformer', () => { expect(process.env.TS_JEST).toBe('1') }) - test.each(['foo.ts', 'foo.tsx'])('should process ts/tsx file', (filePath) => { + test.each(['foo.ts', 'foo.tsx', 'foo.mts', 'foo.mtsx'])('should process ts/tsx file', (filePath) => { const fileContent = 'const foo = 1' const output = 'var foo = 1' tr.getCacheKey(fileContent, filePath, baseTransformOptions) @@ -391,30 +391,33 @@ describe('TsJestTransformer', () => { }) }) - test.each(['foo.js', 'foo.jsx'])('should process js/jsx file with allowJs true', (filePath) => { - const fileContent = 'const foo = 1' - const output = 'var foo = 1' - const transformOptions = { - ...baseTransformOptions, - config: { - ...baseTransformOptions.config, - globals: { - 'ts-jest': { tsconfig: { allowJs: true } }, + test.each(['foo.js', 'foo.jsx', 'foo.mjs', 'foo.mjsx'])( + 'should process js/jsx file with allowJs true', + (filePath) => { + const fileContent = 'const foo = 1' + const output = 'var foo = 1' + const transformOptions = { + ...baseTransformOptions, + config: { + ...baseTransformOptions.config, + globals: { + 'ts-jest': { tsconfig: { allowJs: true } }, + }, }, - }, - } - tr.getCacheKey(fileContent, filePath, transformOptions) - logTarget.clear() - jest.spyOn(TsJestCompiler.prototype, 'getCompiledOutput').mockReturnValueOnce({ - code: output, - }) + } + tr.getCacheKey(fileContent, filePath, transformOptions) + logTarget.clear() + jest.spyOn(TsJestCompiler.prototype, 'getCompiledOutput').mockReturnValueOnce({ + code: output, + }) - const result = tr.process(fileContent, filePath, transformOptions) + const result = tr.process(fileContent, filePath, transformOptions) - expect(result).toEqual({ - code: output, - }) - }) + expect(result).toEqual({ + code: output, + }) + }, + ) test('should process file with unknown extension and show warning message without babel-jest', () => { const fileContent = 'foo' diff --git a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap index 7d28dbdb8b..6ff107bf3f 100644 --- a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap +++ b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap @@ -61,6 +61,7 @@ Object { ".jsx", ".ts", ".tsx", + ".mts", ], "moduleFileExtensions": Array [ "bar",