Files
next.js/scripts/run-for-change.mjs
Tim NeutkensandGitHub 8141dcf12e Convert test/integration to isolated tests (#93247)
### What?

Converts every test under `test/integration/` to an isolated test
running through `nextTestSetup` (under `test/e2e/`, `test/production/`,
`test/development/`, or `test/unit/`), then deletes `test/integration/`
along with the legacy CI orchestration that was specific to it.

- `test/integration/` removed entirely (~327 test suites)
- New isolated suites added across the existing folders:
  - `test/e2e/` — 175
  - `test/production/` — 130
  - `test/development/` — 43
  - `test/unit/` — 1
- `.github/workflows/build_and_test.yml` and `run-tests.js` no longer
have any `integration` branches
- `nextTestSetup` gained a `baseUrl` option on `next.browser()` so a
small number of tests that drive their own proxy/static-export server
can keep using `next.browser(...)` instead of importing `next-webdriver`
directly

### Why?

`test/integration/` predated `nextTestSetup` and ran tests directly
against the source checkout via custom helpers (`launchApp`,
`nextBuild`, `nextStart`, `runNextCommand`, `webdriver`, `fetchViaHTTP`,
…). Each suite hand-rolled its own dev/start/build orchestration,
fixture mutation, and process management.

The isolated test model used by the rest of the repo gives each suite an
isolated working directory containing a packed `next.tgz` install, a
uniform `next.start()` / `next.build()` / `next.fetch()` /
`next.browser()` API, and the same lifecycle for dev, start, and deploy
modes — so a single set of assertions covers all three. Deploy-mode
skips and per-feature gates are expressed declaratively
(`skipDeployment`, `disableAutoSkewProtection`, `if (skipped) return`)
instead of branching on `process.env`.

Removing `test/integration/` lets us:

- Delete the bespoke orchestration code in the CI workflow and
`run-tests.js`
- Run every converted suite consistently in dev, start, and deploy modes
(where applicable)
- Reproduce every test locally with the same `pnpm
test-{dev,start}-{turbo,webpack}` commands; no separate `integration`
path
- Open the door to running `test/production` against deployments in the
future (the converted suites already declare `skipDeployment` so they
can be flipped on)

### How?

Mechanical conversion per suite, with targeted clean-ups:

1. **Per-suite conversion.** Each
`test/integration/<name>/test/index.test.{js,ts}` was rewritten into a
single `<name>.test.ts` under the right folder based on what the
original exercised:
   - `launchApp` / dev-only assertions → `test/development/`
- `nextBuild` + `nextStart` / start-only assertions → `test/production/`
   - Both → `test/e2e/`
- The one pure jsdom render check (`link-without-router`) → `test/unit/`

2. **API mapping.** Custom helpers were replaced by `nextTestSetup`
equivalents: `launchApp` → `next.start()`, `nextBuild` → `next.build()`,
`runNextCommand` → `next.runCommand`, `fetchViaHTTP` → `next.fetch`,
`webdriver(...)` → `next.browser(...)`. Fixture mutations switched from
raw `fs.writeFile`/`fs.rename` to `next.patchFile` (with the 3-arg
`runWithTempContent` callback when the change has a defined scope) and
`next.deleteFile`.

3. **Deploy-mode handling.** Suites that can't run in deploy mode (use
`patchFile` / `next.build()` / depend on local CLI output) declare
`skipDeployment: true` and early-return on the `skipped` boolean. Suites
where Vercel's edge mutates URLs (`&dpl=`, immutable assets) declare
`disableAutoSkewProtection: true`.

4. **`next.browser({ baseUrl })`.** A handful of tests
(`prerender-export`, `cdn-cache-busting`, `preload-viewport`, both
`react-virtualized` suites) need to drive a separate server (a
static-export server or an `http-proxy` instance) rather than the
Next.js process. Instead of importing `next-webdriver` directly, those
tests now pass `{ baseUrl: <port|url> }` to `next.browser()`. For the
proxy cases, the proxy was moved into `server.js` inside the fixture and
`http-proxy` declared via the `dependencies` option of `nextTestSetup`,
so the test runs with a fully isolated dependency graph.

5. **CI clean-up.** With `test/integration` gone, the `test
integration*` jobs and `integration-tests-manifest`-related logic in
`.github/workflows/build_and_test.yml` were removed, and `run-tests.js`
no longer has the `integration` test-folder branch.

6. **Validation.** The PR was iterated against multiple full CI runs;
the remaining failures on the latest run are pre-existing flakes
(segment-cache 60s `act` timeouts in turbopack-prod) or transient
infrastructure issues unrelated to the conversion.
2026-05-11 13:55:47 +02:00

183 lines
5.0 KiB
JavaScript

// @ts-check
import { promisify } from 'util'
import { exec as execOrig, spawn } from 'child_process'
import { getDiffRevision, getGitInfo } from './git-info.mjs'
const exec = promisify(execOrig)
const CHANGE_ITEM_GROUPS = {
docs: [
'.agents',
'.claude',
'.cursor',
'bench',
'docs',
'errors',
'examples',
'UPGRADING.md',
'contributing.md',
'contributing',
'CODE_OF_CONDUCT.md',
'readme.md',
'.github/ISSUE_TEMPLATE',
'.github/actions/pr-auto-label/src/config.json',
'.github/pull_request_template.md',
'packages/next-plugin-storybook/readme.md',
'packages/next/license.md',
'packages/next/README.md',
'packages/eslint-plugin-next/README.md',
'packages/next-codemod/license.md',
'packages/next-codemod/README.md',
'crates/wasm/README.md',
'packages/next-swc/README.md',
'packages/next-bundle-analyzer/readme.md',
'packages/next-mdx/license.md',
'packages/next-mdx/readme.md',
'packages/react-dev-overlay/README.md',
'packages/react-refresh-utils/README.md',
'packages/create-next-app/README.md',
'packages/font/README.md',
'packages/next-env/README.md',
'packages/next/src/client/components/react-dev-overlay/README.md',
],
cna: [
'packages/create-next-app',
'test/production/create-next-app',
'examples/basic-css',
'examples/mdx-pages',
'examples/with-sass',
'examples/with-eslint',
],
'next-codemod': ['packages/next-codemod'],
'next-swc': [
'packages/next-swc',
'scripts/normalize-version-bump.js',
'test/production/create-next-app',
'scripts/send-trace-to-jaeger',
],
}
async function main() {
const { branchName, remoteUrl, isCanary } = await getGitInfo()
const diffRevision = await getDiffRevision()
const changesResult = await exec(
`git diff ${diffRevision} --name-only`
).catch((err) => {
console.error(err)
return { stdout: '' }
})
console.error({ branchName, remoteUrl, isCanary, changesResult })
const changedFilesOutput = changesResult.stdout
const typeIndex = process.argv.indexOf('--type')
const type = typeIndex > -1 && process.argv[typeIndex + 1]
const isNegated = process.argv.indexOf('--not') > -1
const alwaysCanary = process.argv.indexOf('--always-canary') > -1
if (!type) {
throw new Error(
`Missing "--type" flag, e.g. "node run-for-change.mjs --type docs"`
)
}
const execArgIndex = process.argv.indexOf('--exec')
const listChangedDirectories = process.argv.includes(
'--listChangedDirectories'
)
if (execArgIndex < 0 && !listChangedDirectories) {
throw new Error(
'Invalid: must provide either "--exec" or "--listChangedDirectories" flag'
)
}
let hasMatchingChange = false
const changeItems = CHANGE_ITEM_GROUPS[type]
const execArgs = process.argv.slice(execArgIndex + 1)
if (execArgs.length < 1 && !listChangedDirectories) {
throw new Error('Missing exec arguments after "--exec"')
}
if (!changeItems) {
throw new Error(
`Invalid change type, allowed types are ${Object.keys(
CHANGE_ITEM_GROUPS
).join(', ')}`
)
}
let changedFilesCount = 0
let changedDirectories = new Set()
// always run for canary if flag is enabled
if (alwaysCanary && branchName === 'canary') {
changedFilesCount += 1
hasMatchingChange = true
}
for (let file of changedFilesOutput.split('\n')) {
file = file.trim().replace(/\\/g, '/')
if (file) {
changedFilesCount += 1
// if --not flag is provided we execute for any file changed
// not included in the change items otherwise we only execute
// if a change item is changed
const matchesItem = changeItems.some((item) => {
const found = file.startsWith(item)
if (found) {
changedDirectories.add(item)
}
return found
})
if (!matchesItem && isNegated) {
hasMatchingChange = true
break
}
if (matchesItem && !isNegated) {
hasMatchingChange = true
break
}
}
}
// if we fail to detect the changes run the command
if (changedFilesCount < 1) {
console.error(`No changed files detected:\n${changedFilesOutput}`)
hasMatchingChange = true
}
if (hasMatchingChange) {
if (listChangedDirectories) {
console.log(Array.from(changedDirectories).join('\n'))
return
}
const cmd = spawn(execArgs[0], execArgs.slice(1))
cmd.stdout.pipe(process.stdout)
cmd.stderr.pipe(process.stderr)
await new Promise((resolve, reject) => {
cmd.on('exit', (code) => {
if (code !== 0) {
return reject(new Error('command failed with code: ' + code))
}
resolve()
})
cmd.on('error', (err) => reject(err))
})
} else if (!listChangedDirectories) {
console.log(
`No matching changed files for ${isNegated ? 'not ' : ''}"${type}":\n` +
changedFilesOutput.trim()
)
}
}
main().catch((err) => {
console.error('Failed to detect changes', err)
process.exit(1)
})