From acbd54322f447caaee09b1262e4687434ea0268c Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 9 Mar 2022 06:49:58 -0600 Subject: [PATCH] Ensure mjs files are transformed with jest (#34698) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/next/build/jest/jest.ts | 2 +- packages/next/build/swc/jest-transformer.js | 4 ---- test/production/next/jest/index.test.ts | 15 +++++++++++++++ test/unit/jest-next-swc.test.ts | 1 - 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index 45733ea25322..4af9d9afb83a 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -100,7 +100,7 @@ export default function nextJest(options: { dir?: string } = {}) { transform: { // Use SWC to compile tests - '^.+\\.(js|jsx|ts|tsx)$': [ + '^.+\\.(js|jsx|ts|tsx|mjs)$': [ require.resolve('../swc/jest-transformer'), { nextConfig, diff --git a/packages/next/build/swc/jest-transformer.js b/packages/next/build/swc/jest-transformer.js index b58b9a102fc7..18c15b6f2a94 100644 --- a/packages/next/build/swc/jest-transformer.js +++ b/packages/next/build/swc/jest-transformer.js @@ -37,10 +37,6 @@ const isSupportEsm = 'Module' in vm module.exports = { createTransformer: (inputOptions) => ({ process(src, filename, jestOptions) { - if (!/\.[jt]sx?$/.test(filename)) { - return src - } - const jestConfig = getJestConfig(jestOptions) let swcTransformOpts = getJestSWCOptions({ diff --git a/test/production/next/jest/index.test.ts b/test/production/next/jest/index.test.ts index 73265c52fcdb..f981254067a4 100644 --- a/test/production/next/jest/index.test.ts +++ b/test/production/next/jest/index.test.ts @@ -80,6 +80,21 @@ describe('next/jest', () => { }); }); `, + 'lib/hello.mjs': ` + import path from 'path' + + export default function hello() { + return path.join('hello', 'world') + } + `, + 'test/mjs-support.test.js': ` + import path from 'path' + import hello from '../lib/hello.mjs' + + it('should transpile .mjs file correctly', async () => { + expect(hello()).toBe(path.join('hello', 'world')) + }) + `, 'test/mock.test.js': ` import router from 'next/router' diff --git a/test/unit/jest-next-swc.test.ts b/test/unit/jest-next-swc.test.ts index 55e10a32998c..a74277e8d815 100644 --- a/test/unit/jest-next-swc.test.ts +++ b/test/unit/jest-next-swc.test.ts @@ -1,5 +1,4 @@ /* eslint-env jest */ - describe('jest next-swc preset', () => { it('should have correct env', async () => { expect(process.env.NODE_ENV).toBe('test')