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

Enable jest hoist transform when using next/jest #33731

Merged
merged 5 commits into from Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
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
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
})
})