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

Set __NEXT_NEW_LINK_BEHAVIOR in Jest tests when newNextLinkBehavior is true #40702

Merged
merged 5 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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()
})
})