Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set
__NEXT_NEW_LINK_BEHAVIOR
in Jest tests when `newNextLinkBehavio…
…r` is true (#40702) Fixes #40463 Could use some help figuring out where to add a test! I looked around and found [`jest-next-swc.test.ts`](https://github.com/vercel/next.js/blob/canary/test/unit/jest-next-swc.test.ts), but I don't think I can use that to test this fix. Anyways, from my local testing this PR seems to fix the issue. Co-authored-by: JJ Kasper <jj@jjsweb.site>
- Loading branch information
1 parent
bf8ee1e
commit 4970d7a
Showing
2 changed files
with
98 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
|
||
describe('next/jest', () => { | ||
let next: NextInstance | ||
|
||
if (process.env.NEXT_TEST_REACT_VERSION === '^17') { | ||
// react testing library is specific to react version | ||
it('should bail on react v17', () => {}) | ||
return | ||
} | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/index.jsx': ` | ||
import Link from 'next/link' | ||
export default function Page() { | ||
return <Link href='https://example.com'><a>Hello World!</a></Link> | ||
} | ||
`, | ||
'test/index.test.jsx': ` | ||
import { render, screen, act } from '@testing-library/react' | ||
import Page from '../pages/index' | ||
it('Link', () => { | ||
act(() => { | ||
render(<Page />) | ||
const link = screen.getByRole('link', { name: 'Hello World!' }) | ||
expect(link.getAttribute('href')).toBe('https://example.com') | ||
}) | ||
}) | ||
`, | ||
'jest.config.js': ` | ||
const nextJest = require('next/jest') | ||
const createJestConfig = nextJest({ dir: './' }) | ||
module.exports = createJestConfig({ | ||
testEnvironment: 'jest-environment-jsdom', | ||
}) | ||
`, | ||
}, | ||
dependencies: { | ||
jest: '27.4.7', | ||
'@testing-library/react': '12.1.2', | ||
}, | ||
packageJson: { | ||
scripts: { | ||
build: 'next build && yarn jest --forceExit test/index.test.jsx', | ||
}, | ||
}, | ||
skipStart: true, | ||
buildCommand: `yarn build`, | ||
}) | ||
}) | ||
|
||
afterAll(() => next.destroy()) | ||
|
||
it(`should use normal Link behavior when newNextLinkBehavior is unset`, async () => { | ||
await next.start() | ||
}) | ||
|
||
it(`should use new link behavior when newNextLinkBehavior is true`, async () => { | ||
await next.stop() | ||
|
||
await next.patchFile( | ||
'pages/index.jsx', | ||
` | ||
import Link from 'next/link' | ||
export default function Page() { | ||
return <Link href='https://example.com'><div>Hello World!</div></Link> | ||
} | ||
` | ||
) | ||
await next.patchFile( | ||
'next.config.js', | ||
` | ||
module.exports = { experimental: { newNextLinkBehavior: true } } | ||
` | ||
) | ||
|
||
await next.start() | ||
}) | ||
}) |