Skip to content

Commit

Permalink
Enable jest hoist transform when using next/jest
Browse files Browse the repository at this point in the history
Fixes vercel#32539

Implements what was shared at vercel#32539 (comment).
  • Loading branch information
timneutkens committed Jan 27, 2022
1 parent 0d642f1 commit 4d36b46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/next/build/swc/options.js
Expand Up @@ -38,6 +38,14 @@ function getBaseSWCOptions({
},

transform: {
// Enables https://github.com/swc-project/swc/blob/0359deb4841be743d73db4536d4a22ac797d7f65/crates/swc_ecma_ext_transforms/src/jest.rs
...(jest
? {
hidden: {
jest: true,
},
}
: {}),
legacyDecorator: enableDecorators,
react: {
importSource: jsConfig?.compilerOptions?.jsxImportSource || 'react',
Expand Down
24 changes: 24 additions & 0 deletions test/mock.test.js
@@ -0,0 +1,24 @@
// home.test.js
import router from 'next/router'
// const router = require('next/router')
// jest.setup.js
jest.mock('next/router', () => ({
push: jest.fn(),
back: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
},
asPath: jest.fn().mockReturnThis(),
beforePopState: jest.fn(() => null),
useRouter: () => ({
push: jest.fn(),
}),
}))

describe(`Page / Home`, () => {
it(`call mocked`, async () => {
console.log(router)
expect(typeof router.useRouter).toBe('function') // mocked
})
})

0 comments on commit 4d36b46

Please sign in to comment.