From 71a8c73d5d9e8940552742cbb7a4d58c54b42666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 16 Nov 2022 20:55:06 +0100 Subject: [PATCH] feat(jest): respect `transpilePackages` in tests (#42987) fixes #42964 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper Co-authored-by: Bryce Kalow --- packages/next/build/jest/jest.ts | 15 ++++++- .../jest/transpile-packages.test.ts | 43 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/production/jest/transpile-packages.test.ts diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index 8b1e704bc1b0a9d..c138f500d007151 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,16 @@ 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` + ...(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)$', diff --git a/test/production/jest/transpile-packages.test.ts b/test/production/jest/transpile-packages.test.ts new file mode 100644 index 000000000000000..fa7042dd5af3d11 --- /dev/null +++ b/test/production/jest/transpile-packages.test.ts @@ -0,0 +1,43 @@ +import { createNext } from 'e2e-utils' +import { NextInstance } from 'test/lib/next-modes/base' +import { renderViaHTTP } from 'next-test-utils' +describe('next/jest', () => { + let next: NextInstance + + beforeAll(async () => { + next = await createNext({ + files: { + 'pages/index.js': `import capitalize from '@hashicorp/platform-util/text/capitalize' + 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': `module.exports = require('next/jest')({ dir: './' })()`, + 'next.config.js': `module.exports = { + experimental: { transpilePackages: ['@hashicorp/platform-util'] }, + }`, + }, + 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') + }) +})