Files
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

312 lines
11 KiB
TypeScript

import { nextTestSetup, isNextDev } from 'e2e-utils'
import { retry } from 'next-test-utils'
import cheerio from 'cheerio'
describe('env-config', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
env: {
PROCESS_ENV_KEY: 'processenvironment',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
skipDeployment: true,
})
if (skipped) return
const getEnvFromHtml = async (path: string) => {
const html = await next.render(path)
const $ = cheerio.load(html)
const env = JSON.parse($('p').text())
env.nextConfigEnv = $('#nextConfigEnv').text()
env.nextConfigPublicEnv = $('#nextConfigPublicEnv').text()
env.nextConfigNewPublicEnv = $('#nextConfigNewPublicEnv').text()
return env
}
const checkEnvData = (
data: Record<string, string | undefined>,
didReload = false
) => {
expect(data.ENV_FILE_KEY).toBe('env')
expect(data.LOCAL_ENV_FILE_KEY).toBe('localenv')
expect(data.DEVELOPMENT_ENV_FILE_KEY).toBe(
isNextDev ? 'development' : undefined
)
expect(data.LOCAL_DEVELOPMENT_ENV_FILE_KEY).toBe(
isNextDev ? 'localdevelopment' : undefined
)
expect(data.TEST_ENV_FILE_KEY).toBe(undefined)
expect(data.LOCAL_TEST_ENV_FILE_KEY).toBe(undefined)
expect(data.PRODUCTION_ENV_FILE_KEY).toBe(
isNextDev ? undefined : 'production'
)
expect(data.LOCAL_PRODUCTION_ENV_FILE_KEY).toBe(
isNextDev ? undefined : 'localproduction'
)
expect(data.ENV_FILE_EXPANDED).toBe('env')
expect(data.ENV_FILE_EXPANDED_CONCAT).toBe('hello-env')
expect(data.ENV_FILE_EXPANDED_ESCAPED).toBe('$ENV_FILE_KEY')
expect(data.ENV_FILE_KEY_EXCLAMATION).toBe('hello!')
expect(data.ENV_FILE_EMPTY_FIRST).toBe('$escaped')
expect(data.ENV_FILE_PROCESS_ENV).toBe('env-cli')
if (didReload) {
expect(data.NEW_ENV_KEY).toBe('true')
expect(data.NEW_ENV_LOCAL_KEY).toBe('hello')
expect(data.NEW_ENV_DEV_KEY).toBe('from-dev')
expect(data.NEXT_PUBLIC_HELLO_WORLD).toBe('again')
}
expect(data.nextConfigEnv).toBe('hello from next.config.js')
expect(data.nextConfigPublicEnv).toBe('hello again from next.config.js')
expect(data.nextConfigNewPublicEnv).toBe('hello set in next.config.js')
}
it('should have process environment override .env', async () => {
const data = await getEnvFromHtml('/')
expect(data.PROCESS_ENV_KEY).toEqual('processenvironment')
})
it('should provide global env to next.config.js', async () => {
const res = await next.fetch('/hello', { redirect: 'manual' })
const { pathname } = new URL(res.headers.get('location')!)
expect(res.status).toBe(307)
expect(pathname).toBe('/another')
})
it('should inline global values during build', async () => {
const browser = await next.browser('/global')
expect(await browser.waitForElementByCss('#global-value').text()).toBe(
'another'
)
})
it('should provide env for SSG', async () => {
const data = await getEnvFromHtml('/some-ssg')
checkEnvData(data)
})
it('should provide env correctly for SSR', async () => {
const data = await getEnvFromHtml('/some-ssp')
checkEnvData(data)
})
it('should provide env correctly for API routes', async () => {
const data = JSON.parse(await next.render('/api/all'))
checkEnvData(data)
})
it('should load env from .env', async () => {
const data = await getEnvFromHtml('/')
expect(data.ENV_FILE_KEY).toEqual('env')
expect(data.ENV_FILE_DEVELOPMENT_OVERRIDE_TEST).toEqual(
isNextDev ? 'development' : 'env'
)
expect(data.ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST).toEqual(
isNextDev ? 'localdevelopment' : 'env'
)
expect(data.ENV_FILE_TEST_OVERRIDE_TEST).toEqual('env')
expect(data.ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST).toBe('env')
expect(data.LOCAL_ENV_FILE_KEY).toBe('localenv')
expect(data.ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST).toEqual(
isNextDev ? 'env' : 'production'
)
expect(data.ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST).toEqual(
isNextDev ? 'env' : 'localproduction'
)
expect(data.NEXT_PUBLIC_EMPTY_ENV_VAR).toEqual('')
const browser = await next.browser('/')
expect(
await browser.waitForElementByCss('#nextPublicEmptyEnvVar').text()
).toBe('content:')
})
if (isNextDev) {
describe('with hot reload', () => {
it('should have updated runtime values after change', async () => {
const envContent = await next.readFile('.env')
const envDevContent = await next.readFile('.env.development')
const envLocalContent = await next.readFile('.env.local')
try {
const initialData = await getEnvFromHtml('/')
expect(initialData.ENV_FILE_KEY).toBe('env')
await next.patchFile('.env', envContent + '\nNEW_ENV_KEY=true')
await next.patchFile(
'.env.local',
envLocalContent + '\nNEW_ENV_LOCAL_KEY=hello'
)
await next.patchFile(
'.env.development',
envDevContent +
'\nNEW_ENV_DEV_KEY=from-dev\nNEXT_PUBLIC_HELLO_WORLD=again'
)
await retry(async () => {
expect(next.cliOutput).toContain('Reload env:')
})
await retry(async () => {
const data = await getEnvFromHtml('/')
expect(data.NEW_ENV_KEY).toBe('true')
expect(data.NEW_ENV_LOCAL_KEY).toBe('hello')
expect(data.NEW_ENV_DEV_KEY).toBe('from-dev')
expect(data.NEXT_PUBLIC_HELLO_WORLD).toBe('again')
})
// Re-verify base env data with didReload=true
const ssgData = await getEnvFromHtml('/some-ssg')
checkEnvData(ssgData, true)
const sspData = await getEnvFromHtml('/some-ssp')
checkEnvData(sspData, true)
const apiData = JSON.parse(await next.render('/api/all'))
checkEnvData(apiData, true)
const outputBefore = next.cliOutput.length
await next.patchFile(
'.env',
envContent.replace('ENV_FILE_KEY=env', 'ENV_FILE_KEY=env-updated') +
'\nNEW_ENV_KEY=true'
)
await retry(async () => {
const recentOutput = next.cliOutput.substring(outputBefore)
expect(recentOutput).toContain('Reload env:')
})
const recentOutput = next.cliOutput.substring(outputBefore)
expect([...recentOutput.matchAll(/Reload env:/g)].length).toBe(1)
expect(recentOutput).not.toContain('.env.local')
await retry(async () => {
const data = await getEnvFromHtml('/')
expect(data.ENV_FILE_KEY).toBe('env-updated')
expect(data.NEW_ENV_KEY).toBe('true')
})
// Now modify .env.local and verify it's detected
const outputBefore2 = next.cliOutput.length
await next.patchFile(
'.env.local',
envLocalContent.replace(
'ENV_FILE_LOCAL_OVERRIDE_TEST=localenv',
'ENV_FILE_LOCAL_OVERRIDE_TEST=localenv-updated'
)
)
await retry(async () => {
const recentOutput2 = next.cliOutput.substring(outputBefore2)
expect(recentOutput2).toContain('Reload env:')
})
expect(next.cliOutput.substring(outputBefore2)).toContain(
'.env.local'
)
await retry(async () => {
const data = await getEnvFromHtml('/')
expect(data.ENV_FILE_KEY).toBe('env-updated')
expect(data.ENV_FILE_LOCAL_OVERRIDE_TEST).toBe('localenv-updated')
})
} finally {
await next.patchFile('.env', envContent)
await next.patchFile('.env.development', envDevContent)
await next.patchFile('.env.local', envLocalContent)
}
})
it('should trigger HMR correctly when NEXT_PUBLIC_ env is changed', async () => {
const envContent = await next.readFile('.env')
const envLocalContent = await next.readFile('.env.local')
try {
const browser = await next.browser('/global')
expect(
await browser.waitForElementByCss('#global-value').text()
).toBe('another')
let outputBefore = next.cliOutput.length
await next.patchFile(
'.env',
envContent.replace(
'NEXT_PUBLIC_TEST_DEST=another',
'NEXT_PUBLIC_TEST_DEST=replaced'
)
)
await retry(async () => {
const recentOutput = next.cliOutput.substring(outputBefore)
expect(recentOutput).toContain('Reload env:')
})
let recentOutput = next.cliOutput.substring(outputBefore)
expect([...recentOutput.matchAll(/Reload env:/g)].length).toBe(1)
expect(recentOutput).not.toContain('.env.local')
await retry(async () => {
expect(
await browser.waitForElementByCss('#global-value').text()
).toBe('replaced')
})
// Override via .env.local
outputBefore = next.cliOutput.length
await next.patchFile(
'.env.local',
envLocalContent + '\nNEXT_PUBLIC_TEST_DEST=overridden'
)
await retry(async () => {
recentOutput = next.cliOutput.substring(outputBefore)
expect(recentOutput).toContain('Reload env:')
})
recentOutput = next.cliOutput.substring(outputBefore)
expect([...recentOutput.matchAll(/Reload env:/g)].length).toBe(1)
expect(recentOutput).toContain('.env.local')
await retry(async () => {
expect(
await browser.waitForElementByCss('#global-value').text()
).toBe('overridden')
})
// Restore .env.local
outputBefore = next.cliOutput.length
await next.patchFile('.env.local', envLocalContent)
await retry(async () => {
recentOutput = next.cliOutput.substring(outputBefore)
expect(recentOutput).toContain('Reload env:')
})
recentOutput = next.cliOutput.substring(outputBefore)
expect([...recentOutput.matchAll(/Reload env:/g)].length).toBe(1)
expect(recentOutput).toContain('.env.local')
await retry(async () => {
expect(
await browser.waitForElementByCss('#global-value').text()
).toBe('replaced')
})
// Restore .env to original
const outputBefore2 = next.cliOutput.length
await next.patchFile('.env', envContent)
await retry(async () => {
const recentOutput2 = next.cliOutput.substring(outputBefore2)
expect(recentOutput2).toContain('Reload env:')
})
await retry(async () => {
expect(await browser.elementByCss('#global-value').text()).toBe(
'another'
)
})
} finally {
await next.patchFile('.env', envContent)
await next.patchFile('.env.local', envLocalContent)
}
})
})
}
})