Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest): respect transpilePackages in tests #42987

Merged
merged 9 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/next/build/jest/jest.ts
Expand Up @@ -95,6 +95,9 @@ export default function nextJest(options: { dir?: string } = {}) {
await lockfilePatchPromise.cur
}

const transpiled = (
nextConfig?.experimental?.transpilePackages ?? []
).join('|')
return {
...resolvedJestConfig,

Expand Down Expand Up @@ -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)$',

Expand Down
43 changes: 43 additions & 0 deletions 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')
})
})