From af856aae6756490f2cbf1c9b836ee505cfb63bd8 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 22 Feb 2022 17:52:17 -0600 Subject: [PATCH 1/3] Ensure mjs files are transformed with jest --- packages/next/build/jest/jest.ts | 2 +- packages/next/build/swc/jest-transformer.js | 4 ---- test/unit/fixtures/lib/hello.mjs | 5 +++++ test/unit/jest-next-swc.test.ts | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 test/unit/fixtures/lib/hello.mjs 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/unit/fixtures/lib/hello.mjs b/test/unit/fixtures/lib/hello.mjs new file mode 100644 index 000000000000..f702904785d4 --- /dev/null +++ b/test/unit/fixtures/lib/hello.mjs @@ -0,0 +1,5 @@ +import path from 'path' + +export default function hello() { + return path.join('hello', 'world') +} \ No newline at end of file diff --git a/test/unit/jest-next-swc.test.ts b/test/unit/jest-next-swc.test.ts index 55e10a32998c..2937e5713d05 100644 --- a/test/unit/jest-next-swc.test.ts +++ b/test/unit/jest-next-swc.test.ts @@ -1,7 +1,14 @@ /* eslint-env jest */ +import path from 'path' +// @ts-ignore +import hello from './fixtures/lib/hello.mjs' describe('jest next-swc preset', () => { it('should have correct env', async () => { expect(process.env.NODE_ENV).toBe('test') }) + + it('should transpile .mjs file correctly', async () => { + expect(hello()).toBe(path.join('hello', 'world')) + }) }) From 22fdfa5f50c1841c25450ec4b3d4897d88f33f7d Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 22 Feb 2022 18:11:04 -0600 Subject: [PATCH 2/3] lint-fix --- test/unit/fixtures/lib/hello.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/fixtures/lib/hello.mjs b/test/unit/fixtures/lib/hello.mjs index f702904785d4..089421e19f34 100644 --- a/test/unit/fixtures/lib/hello.mjs +++ b/test/unit/fixtures/lib/hello.mjs @@ -2,4 +2,4 @@ import path from 'path' export default function hello() { return path.join('hello', 'world') -} \ No newline at end of file +} From f13a1428a78a13382213a60d79b682dd7fa76ca0 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 23 Feb 2022 08:26:19 -0600 Subject: [PATCH 3/3] Move test --- test/production/next/jest/index.test.ts | 15 +++++++++++++++ test/unit/fixtures/lib/hello.mjs | 5 ----- test/unit/jest-next-swc.test.ts | 8 -------- 3 files changed, 15 insertions(+), 13 deletions(-) delete mode 100644 test/unit/fixtures/lib/hello.mjs 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/fixtures/lib/hello.mjs b/test/unit/fixtures/lib/hello.mjs deleted file mode 100644 index 089421e19f34..000000000000 --- a/test/unit/fixtures/lib/hello.mjs +++ /dev/null @@ -1,5 +0,0 @@ -import path from 'path' - -export default function hello() { - return path.join('hello', 'world') -} diff --git a/test/unit/jest-next-swc.test.ts b/test/unit/jest-next-swc.test.ts index 2937e5713d05..a74277e8d815 100644 --- a/test/unit/jest-next-swc.test.ts +++ b/test/unit/jest-next-swc.test.ts @@ -1,14 +1,6 @@ /* eslint-env jest */ -import path from 'path' -// @ts-ignore -import hello from './fixtures/lib/hello.mjs' - describe('jest next-swc preset', () => { it('should have correct env', async () => { expect(process.env.NODE_ENV).toBe('test') }) - - it('should transpile .mjs file correctly', async () => { - expect(hello()).toBe(path.join('hello', 'world')) - }) })