From cbe937036bce30fc5b58ba396b6863acdd762fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 10:14:54 +0100 Subject: [PATCH 1/8] feat(jest): respect `transpilePackages` in tests --- packages/next/build/jest/jest.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index 8b1e704bc1b0a9d..d5502e300d3d9c9 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -95,6 +95,9 @@ export default function nextJest(options: { dir?: string } = {}) { await lockfilePatchPromise.cur } + const transpiled = ( + nextConfig?.experimental?.transpilePackages ?? [] + ).join('|') return { ...resolvedJestConfig, @@ -151,8 +154,10 @@ export default function nextJest(options: { dir?: string } = {}) { }, transformIgnorePatterns: [ - // To match Next.js behavior node_modules is not transformed - '/node_modules/', + // To match Next.js behavior node_modules is not transformed, only `transpiledPackages` + `/node_modules/(?!(${transpiled}))/`, + `/node_modules/.pnpm/(?!(${transpiled.replace(/\//g, '\\+')})@)`, + // CSS modules are mocked so they don't need to be transformed '^.+\\.module\\.(css|sass|scss)$', From 38dcc83273a80c53e6bd90a405f6dddfe1478be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 11:28:46 +0100 Subject: [PATCH 2/8] add test --- .../jest/transpile-packages/app/index.test.ts | 5 +++ .../transpile-packages/app/jest.config.js | 1 + .../transpile-packages/app/next.config.js | 8 +++++ .../transpile-packages/app/pages/index.js | 6 ++++ .../jest/transpile-packages/index.test.ts | 36 +++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 test/production/jest/transpile-packages/app/index.test.ts create mode 100644 test/production/jest/transpile-packages/app/jest.config.js create mode 100644 test/production/jest/transpile-packages/app/next.config.js create mode 100644 test/production/jest/transpile-packages/app/pages/index.js create mode 100644 test/production/jest/transpile-packages/index.test.ts diff --git a/test/production/jest/transpile-packages/app/index.test.ts b/test/production/jest/transpile-packages/app/index.test.ts new file mode 100644 index 000000000000000..13e1373e55d6c37 --- /dev/null +++ b/test/production/jest/transpile-packages/app/index.test.ts @@ -0,0 +1,5 @@ +import capitalize from '@hashicorp/platform-util/text/capitalize' + +it('should work', () => { + expect(capitalize('test')).toEqual('Test') +}) diff --git a/test/production/jest/transpile-packages/app/jest.config.js b/test/production/jest/transpile-packages/app/jest.config.js new file mode 100644 index 000000000000000..e7210365694fb5e --- /dev/null +++ b/test/production/jest/transpile-packages/app/jest.config.js @@ -0,0 +1 @@ +module.exports = require('next/jest')({ dir: './' })() diff --git a/test/production/jest/transpile-packages/app/next.config.js b/test/production/jest/transpile-packages/app/next.config.js new file mode 100644 index 000000000000000..b85eb36f6f57db5 --- /dev/null +++ b/test/production/jest/transpile-packages/app/next.config.js @@ -0,0 +1,8 @@ +/** @type {import("next").NextConfig} */ +const nextConfig = { + experimental: { + transpilePackages: ['@hashicorp/platform-util'], + }, +} + +module.exports = nextConfig diff --git a/test/production/jest/transpile-packages/app/pages/index.js b/test/production/jest/transpile-packages/app/pages/index.js new file mode 100644 index 000000000000000..617ad105cfa66b9 --- /dev/null +++ b/test/production/jest/transpile-packages/app/pages/index.js @@ -0,0 +1,6 @@ +import capitalize from '@hashicorp/platform-util/text/capitalize' + +/** Add your relevant code here for the issue to reproduce */ +export default function Home() { + return capitalize('test') +} diff --git a/test/production/jest/transpile-packages/index.test.ts b/test/production/jest/transpile-packages/index.test.ts new file mode 100644 index 000000000000000..b0c391c261bbf9c --- /dev/null +++ b/test/production/jest/transpile-packages/index.test.ts @@ -0,0 +1,36 @@ +import { createNext, FileRef } from 'e2e-utils' +import { NextInstance } from 'test/lib/next-modes/base' +import { renderViaHTTP } from 'next-test-utils' +import { join } from 'path' +describe('next/jest', () => { + let next: NextInstance + + beforeAll(async () => { + next = await createNext({ + files: { + pages: new FileRef(join(__dirname, 'app/pages')), + 'index.test.ts': new FileRef(join(__dirname, 'app/index.test.ts')), + 'jest.config.js': new FileRef(join(__dirname, 'app/jest.config.js')), + 'next.config.js': new FileRef(join(__dirname, 'app/next.config.js')), + }, + packageJson: { + scripts: { + // Runs jest and bails if jest fails + build: 'next build && yarn jest', + }, + }, + buildCommand: `yarn build`, + dependencies: { + '@hashicorp/platform-util': '0.2.0', + '@types/react': 'latest', + jest: '27.4.7', + }, + }) + }) + afterAll(() => next.destroy()) + + it('should work', async () => { + const html = await renderViaHTTP(next.url, '/') + expect(html).toContain('Test') + }) +}) From fbfe247b23bcbfe2fa682c021c12cead47d5a195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 11:35:13 +0100 Subject: [PATCH 3/8] fix test --- test/production/jest/transpile-packages/app/index.test.ts | 5 ----- test/production/jest/transpile-packages/index.test.ts | 6 +++++- 2 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 test/production/jest/transpile-packages/app/index.test.ts diff --git a/test/production/jest/transpile-packages/app/index.test.ts b/test/production/jest/transpile-packages/app/index.test.ts deleted file mode 100644 index 13e1373e55d6c37..000000000000000 --- a/test/production/jest/transpile-packages/app/index.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import capitalize from '@hashicorp/platform-util/text/capitalize' - -it('should work', () => { - expect(capitalize('test')).toEqual('Test') -}) diff --git a/test/production/jest/transpile-packages/index.test.ts b/test/production/jest/transpile-packages/index.test.ts index b0c391c261bbf9c..8e638786176d260 100644 --- a/test/production/jest/transpile-packages/index.test.ts +++ b/test/production/jest/transpile-packages/index.test.ts @@ -9,7 +9,11 @@ describe('next/jest', () => { next = await createNext({ files: { pages: new FileRef(join(__dirname, 'app/pages')), - 'index.test.ts': new FileRef(join(__dirname, 'app/index.test.ts')), + 'index.test.ts': `import capitalize from '@hashicorp/platform-util/text/capitalize' + it('should work', () => { + expect(capitalize('test')).toEqual('Test') + }) + `, 'jest.config.js': new FileRef(join(__dirname, 'app/jest.config.js')), 'next.config.js': new FileRef(join(__dirname, 'app/next.config.js')), }, From 0cf2ea6649cef0570b4a0d8b48a3b535107fd893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 11:37:34 +0100 Subject: [PATCH 4/8] inline app --- ...dex.test.ts => transpile-packages.test.ts} | 19 ++++++++++++------- .../transpile-packages/app/jest.config.js | 1 - .../transpile-packages/app/next.config.js | 8 -------- .../transpile-packages/app/pages/index.js | 6 ------ 4 files changed, 12 insertions(+), 22 deletions(-) rename test/production/jest/{transpile-packages/index.test.ts => transpile-packages.test.ts} (63%) delete mode 100644 test/production/jest/transpile-packages/app/jest.config.js delete mode 100644 test/production/jest/transpile-packages/app/next.config.js delete mode 100644 test/production/jest/transpile-packages/app/pages/index.js diff --git a/test/production/jest/transpile-packages/index.test.ts b/test/production/jest/transpile-packages.test.ts similarity index 63% rename from test/production/jest/transpile-packages/index.test.ts rename to test/production/jest/transpile-packages.test.ts index 8e638786176d260..352254da145e715 100644 --- a/test/production/jest/transpile-packages/index.test.ts +++ b/test/production/jest/transpile-packages.test.ts @@ -1,21 +1,26 @@ -import { createNext, FileRef } from 'e2e-utils' +import { createNext } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { renderViaHTTP } from 'next-test-utils' -import { join } from 'path' describe('next/jest', () => { let next: NextInstance beforeAll(async () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'app/pages')), + 'pages/index.js': `import capitalize from '@hashicorp/platform-util/text/capitalize' + + /** Add your relevant code here for the issue to reproduce */ + export default function Home() { + return capitalize('test') + }`, 'index.test.ts': `import capitalize from '@hashicorp/platform-util/text/capitalize' it('should work', () => { expect(capitalize('test')).toEqual('Test') - }) - `, - 'jest.config.js': new FileRef(join(__dirname, 'app/jest.config.js')), - 'next.config.js': new FileRef(join(__dirname, 'app/next.config.js')), + })`, + 'jest.config.js': `module.exports = require('next/jest')({ dir: './' })()`, + 'next.config.js': `module.exports = { + experimental: { transpilePackages: ['@hashicorp/platform-util'] }, + }`, }, packageJson: { scripts: { diff --git a/test/production/jest/transpile-packages/app/jest.config.js b/test/production/jest/transpile-packages/app/jest.config.js deleted file mode 100644 index e7210365694fb5e..000000000000000 --- a/test/production/jest/transpile-packages/app/jest.config.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('next/jest')({ dir: './' })() diff --git a/test/production/jest/transpile-packages/app/next.config.js b/test/production/jest/transpile-packages/app/next.config.js deleted file mode 100644 index b85eb36f6f57db5..000000000000000 --- a/test/production/jest/transpile-packages/app/next.config.js +++ /dev/null @@ -1,8 +0,0 @@ -/** @type {import("next").NextConfig} */ -const nextConfig = { - experimental: { - transpilePackages: ['@hashicorp/platform-util'], - }, -} - -module.exports = nextConfig diff --git a/test/production/jest/transpile-packages/app/pages/index.js b/test/production/jest/transpile-packages/app/pages/index.js deleted file mode 100644 index 617ad105cfa66b9..000000000000000 --- a/test/production/jest/transpile-packages/app/pages/index.js +++ /dev/null @@ -1,6 +0,0 @@ -import capitalize from '@hashicorp/platform-util/text/capitalize' - -/** Add your relevant code here for the issue to reproduce */ -export default function Home() { - return capitalize('test') -} From a3320858106870077012a3fca00230015ee8b096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 11:39:13 +0100 Subject: [PATCH 5/8] remove comment --- test/production/jest/transpile-packages.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/production/jest/transpile-packages.test.ts b/test/production/jest/transpile-packages.test.ts index 352254da145e715..fa7042dd5af3d11 100644 --- a/test/production/jest/transpile-packages.test.ts +++ b/test/production/jest/transpile-packages.test.ts @@ -8,8 +8,6 @@ describe('next/jest', () => { next = await createNext({ files: { 'pages/index.js': `import capitalize from '@hashicorp/platform-util/text/capitalize' - - /** Add your relevant code here for the issue to reproduce */ export default function Home() { return capitalize('test') }`, From 1b8940cb01d7a1ce0909677130ece94daa7db825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 12:10:25 +0100 Subject: [PATCH 6/8] Update packages/next/build/jest/jest.ts Co-authored-by: JJ Kasper --- packages/next/build/jest/jest.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index d5502e300d3d9c9..a3c6bbfd0b2933a 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -155,9 +155,10 @@ export default function nextJest(options: { dir?: string } = {}) { transformIgnorePatterns: [ // To match Next.js behavior node_modules is not transformed, only `transpiledPackages` + ...(transpiled ? [ `/node_modules/(?!(${transpiled}))/`, `/node_modules/.pnpm/(?!(${transpiled.replace(/\//g, '\\+')})@)`, - + ] : ['/node_modules/']) // CSS modules are mocked so they don't need to be transformed '^.+\\.module\\.(css|sass|scss)$', From a5364695e164d960aae97be334df7466140e86ef Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 16 Nov 2022 11:13:51 -0800 Subject: [PATCH 7/8] Apply suggestions from code review Co-authored-by: Bryce Kalow --- packages/next/build/jest/jest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index a3c6bbfd0b2933a..482aa9d77c5c5b9 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -158,7 +158,7 @@ export default function nextJest(options: { dir?: string } = {}) { ...(transpiled ? [ `/node_modules/(?!(${transpiled}))/`, `/node_modules/.pnpm/(?!(${transpiled.replace(/\//g, '\\+')})@)`, - ] : ['/node_modules/']) + ] : ['/node_modules/']), // CSS modules are mocked so they don't need to be transformed '^.+\\.module\\.(css|sass|scss)$', From 6766b99800986d7f53b649fb6dbb870f1d782eb0 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 16 Nov 2022 11:15:23 -0800 Subject: [PATCH 8/8] lint-fix --- packages/next/build/jest/jest.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index 482aa9d77c5c5b9..c138f500d007151 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -155,10 +155,15 @@ export default function nextJest(options: { dir?: string } = {}) { transformIgnorePatterns: [ // To match Next.js behavior node_modules is not transformed, only `transpiledPackages` - ...(transpiled ? [ - `/node_modules/(?!(${transpiled}))/`, - `/node_modules/.pnpm/(?!(${transpiled.replace(/\//g, '\\+')})@)`, - ] : ['/node_modules/']), + ...(transpiled + ? [ + `/node_modules/(?!(${transpiled}))/`, + `/node_modules/.pnpm/(?!(${transpiled.replace( + /\//g, + '\\+' + )})@)`, + ] + : ['/node_modules/']), // CSS modules are mocked so they don't need to be transformed '^.+\\.module\\.(css|sass|scss)$',