Skip to content

Commit

Permalink
feat(jest): respect transpilePackages in tests (#42987)
Browse files Browse the repository at this point in the history
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 <jj@jjsweb.site>
Co-authored-by: Bryce Kalow <br.kalow@gmail.com>
  • Loading branch information
3 people committed Nov 16, 2022
1 parent e6a7d78 commit 71a8c73
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
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')
})
})

0 comments on commit 71a8c73

Please sign in to comment.