rsnext/test/lib/e2e-utils.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

299 lines
7.9 KiB
TypeScript
Raw Permalink Normal View History

import path from 'path'
import assert from 'assert'
import { flushAllTraces, setGlobal, trace } from 'next/dist/trace'
2022-12-16 09:58:04 +01:00
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants'
import { NextInstance, NextInstanceOpts } from './next-modes/base'
import { NextDevInstance } from './next-modes/next-dev'
import { NextStartInstance } from './next-modes/next-start'
import { NextDeployInstance } from './next-modes/next-deploy'
test(integration): allow to run `--turbo` dev server tests dynamically (#42967) This PR is companion to https://github.com/vercel/next.js/pull/42656. Previous PR https://github.com/vercel/next.js/pull/41908 allowed to set up integration / e2e tests spawn `next dev` with `--turbo` as needed. This PR leverages those, change all the tests suites to run with `--turbo`. One additional change is it can be configured dynamically via env variable. Setting env `__INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH` to glob pattern will selectively enables tests to run with --turbo, normally it'll skip and only current devserver will be used. These changes allow gradual integration between https://github.com/vercel/turbo to next.js. Plan is to run these tests with latest turbopack dev branch. Each time turbopack fixes / implements changes to enable certain set of tests, its CI will change its env variable without manually patching next.js's test cases. Once those change goes to upstream `next-swc` those tests can be permanantly enabled. For those reasons, PR have somewhat verbose changes to touch individual test cases runs devserver. Changed file counts are lot, but mostly identical changes. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a 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 a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-24 00:00:49 +01:00
import { shouldRunTurboDevTest } from './next-test-utils'
2023-02-02 17:22:54 +01:00
export type { NextInstance }
// increase timeout to account for pnpm install time
test(integration): emits successful test output for continue on error (#46008) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: --> - closes WEB-600 - partially resolves WEB-544 This PR applies minor ergonomics changes to the test runner. First, allows to emit successful test reports if continue_on_error is enabled: this allows to track total test stats with --turbo runs. Secondly allows to specify custom timeouts for the e2e - as written in comment otherwise it can exceed total 6 hours of job limit due to having lots of timeout-related failing tests. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/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` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-17 00:44:38 +01:00
// if either test runs for the --turbo or have a custom timeout, set reduced timeout instead.
// this is due to current --turbo test have a lot of tests fails with timeouts, ends up the whole
// test job exceeds the 6 hours limit.
let testTimeout = shouldRunTurboDevTest()
? (240 * 1000) / 4
: (process.platform === 'win32' ? 240 : 120) * 1000
test(integration): emits successful test output for continue on error (#46008) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: --> - closes WEB-600 - partially resolves WEB-544 This PR applies minor ergonomics changes to the test runner. First, allows to emit successful test reports if continue_on_error is enabled: this allows to track total test stats with --turbo runs. Secondly allows to specify custom timeouts for the e2e - as written in comment otherwise it can exceed total 6 hours of job limit due to having lots of timeout-related failing tests. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/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` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-17 00:44:38 +01:00
if (process.env.NEXT_E2E_TEST_TIMEOUT) {
try {
testTimeout = parseInt(process.env.NEXT_E2E_TEST_TIMEOUT, 10)
} catch (_) {
// ignore
}
}
jest.setTimeout(testTimeout)
const testsFolder = path.join(__dirname, '..')
let testFile
const testFileRegex = /\.test\.(js|tsx?)/
const visitedModules = new Set()
const checkParent = (mod) => {
if (!mod?.parent || visitedModules.has(mod)) return
testFile = mod.parent.filename || ''
visitedModules.add(mod)
if (!testFileRegex.test(testFile)) {
checkParent(mod.parent)
}
}
checkParent(module)
process.env.TEST_FILE_PATH = testFile
let testMode = process.env.NEXT_TEST_MODE
if (!testFileRegex.test(testFile)) {
throw new Error(
`e2e-utils imported from non-test file ${testFile} (must end with .test.(js,ts,tsx)`
)
}
const testFolderModes = ['e2e', 'development', 'production']
const testModeFromFile = testFolderModes.find((mode) =>
testFile.startsWith(path.join(testsFolder, mode))
)
if (testModeFromFile === 'e2e') {
const validE2EModes = ['dev', 'start', 'deploy']
if (!process.env.NEXT_TEST_JOB && !testMode) {
require('console').warn(
'Warn: no NEXT_TEST_MODE set, using default of start'
)
testMode = 'start'
}
assert(
validE2EModes.includes(testMode),
`NEXT_TEST_MODE must be one of ${validE2EModes.join(
', '
)} for e2e tests but received ${testMode}`
)
} else if (testModeFromFile === 'development') {
testMode = 'dev'
} else if (testModeFromFile === 'production') {
testMode = 'start'
}
if (testMode === 'dev') {
;(global as any).isNextDev = true
} else if (testMode === 'deploy') {
;(global as any).isNextDeploy = true
} else {
;(global as any).isNextStart = true
}
/**
Rename process.env.TURBOPACK -> process.env.TURBOPACK_DEV in test skips (#63665) ## What? Follow-up to #63653. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> Closes NEXT-2911
2024-03-25 14:17:56 +01:00
* Whether the test is running in development mode.
* Based on `process.env.NEXT_TEST_MODE` and the test directory.
*/
export const isNextDev = testMode === 'dev'
/**
* Whether the test is running in deploy mode.
* Based on `process.env.NEXT_TEST_MODE`.
*/
export const isNextDeploy = testMode === 'deploy'
/**
* Whether the test is running in start mode.
* Default mode. `true` when both `isNextDev` and `isNextDeploy` are false.
*/
export const isNextStart = !isNextDev && !isNextDeploy
if (!testMode) {
throw new Error(
`No 'NEXT_TEST_MODE' set in environment, this is required for e2e-utils`
)
}
require('console').warn(
`Using test mode: ${testMode} in test folder ${testModeFromFile}`
)
/**
* FileRef is wrapper around a file path that is meant be copied
* to the location where the next instance is being created
*/
export class FileRef {
public fsPath: string
constructor(path: string) {
this.fsPath = path
}
}
let nextInstance: NextInstance | undefined = undefined
if (typeof afterAll === 'function') {
afterAll(async () => {
if (nextInstance) {
await nextInstance.destroy()
throw new Error(
`next instance not destroyed before exiting, make sure to call .destroy() after the tests after finished`
)
}
})
}
2022-12-16 09:58:04 +01:00
const setupTracing = () => {
if (!process.env.NEXT_TEST_TRACE) return
setGlobal('distDir', './test/.trace')
// This is a hacky way to use tracing utils even for tracing test utils.
// We want the same treatment as DEVELOPMENT_SERVER - adds a reasonable treshold for logs size.
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
}
/**
* Sets up and manages a Next.js instance in the configured
* test mode. The next instance will be isolated from the monorepo
* to prevent relying on modules that shouldn't be
*/
export async function createNext(
opts: NextInstanceOpts & { skipStart?: boolean }
): Promise<NextInstance> {
try {
if (nextInstance) {
throw new Error(`createNext called without destroying previous instance`)
}
2022-12-16 09:58:04 +01:00
setupTracing()
return await trace('createNext').traceAsyncFn(async (rootSpan) => {
const useTurbo = !!process.env.TEST_WASM
? false
: opts?.turbo ?? shouldRunTurboDevTest()
2022-12-16 09:58:04 +01:00
if (testMode === 'dev') {
// next dev
rootSpan.traceChild('init next dev instance').traceFn(() => {
nextInstance = new NextDevInstance({
...opts,
turbo: useTurbo,
})
})
} else if (testMode === 'deploy') {
// Vercel
rootSpan.traceChild('init next deploy instance').traceFn(() => {
nextInstance = new NextDeployInstance({
...opts,
turbo: false,
})
})
} else {
// next build + next start
rootSpan.traceChild('init next start instance').traceFn(() => {
nextInstance = new NextStartInstance({
...opts,
turbo: false,
})
})
}
nextInstance.on('destroy', () => {
nextInstance = undefined
test(integration): allow to run `--turbo` dev server tests dynamically (#42967) This PR is companion to https://github.com/vercel/next.js/pull/42656. Previous PR https://github.com/vercel/next.js/pull/41908 allowed to set up integration / e2e tests spawn `next dev` with `--turbo` as needed. This PR leverages those, change all the tests suites to run with `--turbo`. One additional change is it can be configured dynamically via env variable. Setting env `__INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH` to glob pattern will selectively enables tests to run with --turbo, normally it'll skip and only current devserver will be used. These changes allow gradual integration between https://github.com/vercel/turbo to next.js. Plan is to run these tests with latest turbopack dev branch. Each time turbopack fixes / implements changes to enable certain set of tests, its CI will change its env variable without manually patching next.js's test cases. Once those change goes to upstream `next-swc` those tests can be permanantly enabled. For those reasons, PR have somewhat verbose changes to touch individual test cases runs devserver. Changed file counts are lot, but mostly identical changes. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a 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 a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-24 00:00:49 +01:00
})
2022-12-16 09:58:04 +01:00
await nextInstance.setup(rootSpan)
2022-12-16 09:58:04 +01:00
if (!opts.skipStart) {
await rootSpan
.traceChild('start next instance')
.traceAsyncFn(async () => {
await nextInstance.start()
})
}
2022-12-16 09:58:04 +01:00
return nextInstance!
})
} catch (err) {
require('console').error('Failed to create next instance', err)
try {
Always throw error when build fails during test (#64116) ## What? When creating the Next.js instance fails for whatever reason (most commonly `next build` failing) it currently exits the process, this causes Jest to not report these tests as failed. The change in this PR ensures that anytime creating the instance fails it throws an error that is caught by Jest and then causes Jest to report the tests as failed. The reporting of tests is relevant as our monitoring tools (i.e. DataDog / Turbopack test manifest) rely on correct reporting of all failing tests. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> Closes NEXT-3016
2024-04-05 21:33:19 +02:00
await nextInstance?.destroy()
} catch (_) {}
Always throw error when build fails during test (#64116) ## What? When creating the Next.js instance fails for whatever reason (most commonly `next build` failing) it currently exits the process, this causes Jest to not report these tests as failed. The change in this PR ensures that anytime creating the instance fails it throws an error that is caught by Jest and then causes Jest to report the tests as failed. The reporting of tests is relevant as our monitoring tools (i.e. DataDog / Turbopack test manifest) rely on correct reporting of all failing tests. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> Closes NEXT-3016
2024-04-05 21:33:19 +02:00
nextInstance = undefined
// Throw instead of process exit to ensure that Jest reports the tests as failed.
throw err
2022-12-16 09:58:04 +01:00
} finally {
flushAllTraces()
}
}
export function nextTestSetup(
options: Parameters<typeof createNext>[0] & {
skipDeployment?: boolean
dir?: string
}
): {
isNextDev: boolean
isNextDeploy: boolean
isNextStart: boolean
isTurbopack: boolean
next: NextInstance
skipped: boolean
} {
let skipped = false
if (options.skipDeployment) {
// When the environment is running for deployment tests.
if (isNextDeploy) {
// eslint-disable-next-line jest/no-focused-tests
it.only('should skip next deploy', () => {})
// No tests are run.
skipped = true
}
}
let next: NextInstance | undefined
if (!skipped) {
beforeAll(async () => {
next = await createNext(options)
})
afterAll(async () => {
// Gracefully destroy the instance if `createNext` success.
// If next instance is not available, it's likely beforeAll hook failed and unnecessarily throws another error
// by attempting to destroy on undefined.
await next?.destroy()
})
}
const nextProxy = new Proxy<NextInstance>({} as NextInstance, {
get: function (_target, property) {
if (!next) {
throw new Error(
'next instance is not initialized yet, make sure you call methods on next instance in test body.'
)
}
const prop = next[property]
return typeof prop === 'function' ? prop.bind(next) : prop
},
})
return {
get isNextDev() {
return isNextDev
},
get isTurbopack(): boolean {
return Boolean(
!process.env.TEST_WASM && (options.turbo ?? shouldRunTurboDevTest())
)
},
get isNextDeploy() {
return isNextDeploy
},
get isNextStart() {
return isNextStart
},
get next() {
return nextProxy
},
skipped,
}
}