Skip to content

Commit

Permalink
Set __NEXT_NEW_LINK_BEHAVIOR in Jest tests when `newNextLinkBehavio…
Browse files Browse the repository at this point in the history
…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
TomerAberbach and ijjk committed Sep 22, 2022
1 parent bf8ee1e commit 4970d7a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/next/build/jest/jest.ts
@@ -1,6 +1,7 @@
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'
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down
86 changes: 86 additions & 0 deletions test/production/jest/new-link-behavior.test.ts
@@ -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()
})
})

0 comments on commit 4970d7a

Please sign in to comment.