Unverified Commit 4970d7a0 authored by Tomer Aberbach's avatar Tomer Aberbach Committed by GitHub

Set `__NEXT_NEW_LINK_BEHAVIOR` in Jest tests when `newNextLinkBehavior` 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: 's avatarJJ Kasper <jj@jjsweb.site>
parent bf8ee1ed
import { loadEnvConfig } from '@next/env'
import { resolve, join } from 'path'
import loadConfig from '../../server/config'
import { NextConfigComplete } from '../../server/config-shared'
import { PHASE_TEST } from '../../shared/lib/constants'
import loadJsConfig from '../load-jsconfig'
import * as Log from '../output/log'
......@@ -27,6 +28,16 @@ function loadClosestPackageJson(dir: string, attempts = 1): any {
}
}
/** Loads dotenv files and sets environment variables based on next config. */
function setUpEnv(dir: string, nextConfig: NextConfigComplete) {
const dev = false
loadEnvConfig(dir, dev, Log)
if (nextConfig.experimental.newNextLinkBehavior) {
process.env.__NEXT_NEW_LINK_BEHAVIOR = 'true'
}
}
/*
// Usage in jest.config.js
const nextJest = require('next/jest');
......@@ -61,7 +72,7 @@ export default function nextJest(options: { dir?: string } = {}) {
isEsmProject = packageConfig.type === 'module'
nextConfig = await getConfig(resolvedDir)
loadEnvConfig(resolvedDir, false, Log)
setUpEnv(resolvedDir, nextConfig)
// TODO: revisit when bug in SWC is fixed that strips `.css`
const result = await loadJsConfig(resolvedDir, nextConfig)
jsConfig = result.jsConfig
......
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()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment