5d7e1425d7
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)
162 lines
4 KiB
TypeScript
162 lines
4 KiB
TypeScript
import path from 'path'
|
|
import assert from 'assert'
|
|
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'
|
|
import { shouldRunTurboDevTest } from './next-test-utils'
|
|
|
|
// increase timeout to account for yarn install time
|
|
jest.setTimeout(240 * 1000)
|
|
|
|
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
|
|
}
|
|
|
|
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`
|
|
)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 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`)
|
|
}
|
|
|
|
const useTurbo = !!process.env.TEST_WASM
|
|
? false
|
|
: opts?.turbo ?? shouldRunTurboDevTest()
|
|
|
|
if (testMode === 'dev') {
|
|
// next dev
|
|
nextInstance = new NextDevInstance({
|
|
...opts,
|
|
turbo: useTurbo,
|
|
})
|
|
} else if (testMode === 'deploy') {
|
|
// Vercel
|
|
nextInstance = new NextDeployInstance({
|
|
...opts,
|
|
turbo: false,
|
|
})
|
|
} else {
|
|
// next build + next start
|
|
nextInstance = new NextStartInstance({
|
|
...opts,
|
|
turbo: false,
|
|
})
|
|
}
|
|
|
|
nextInstance.on('destroy', () => {
|
|
nextInstance = undefined
|
|
})
|
|
|
|
await nextInstance.setup()
|
|
|
|
if (!opts.skipStart) {
|
|
await nextInstance.start()
|
|
}
|
|
return nextInstance!
|
|
} catch (err) {
|
|
require('console').error('Failed to create next instance', err)
|
|
try {
|
|
nextInstance.destroy()
|
|
} catch (_) {}
|
|
process.exit(1)
|
|
}
|
|
}
|