forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable jest hoist transform when using next/jest (vercel#33731)
Fixes vercel#32539 Implements what was shared at vercel#32539 (comment). ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have 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 helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
- Loading branch information
1 parent
0f6a025
commit 1e71865
Showing
6 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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': ` | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} | ||
`, | ||
'jest.config.js': ` | ||
// jest.config.js | ||
const nextJest = require('next/jest') | ||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: './', | ||
}) | ||
// Add any custom config to be passed to Jest | ||
const customJestConfig = { | ||
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work | ||
moduleDirectories: ['node_modules', '<rootDir>/'], | ||
testEnvironment: 'jest-environment-jsdom', | ||
} | ||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig) | ||
`, | ||
'test/mock.test.js': ` | ||
import router from 'next/router' | ||
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(), | ||
}), | ||
})) | ||
it('call mocked', async () => { | ||
expect(router.push._isMockFunction).toBeTruthy() | ||
}) | ||
`, | ||
}, | ||
dependencies: { | ||
jest: '27.4.7', | ||
}, | ||
packageJson: { | ||
scripts: { | ||
build: 'next build && yarn jest test/mock.test.js', | ||
}, | ||
}, | ||
buildCommand: `yarn build`, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should work', async () => { | ||
const html = await renderViaHTTP(next.url, '/') | ||
expect(html).toContain('hello world') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters