Files
next.js/test/e2e/app-dir/logging/fetch-logging.test.ts
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

430 lines
15 KiB
TypeScript

import path from 'path'
import fs from 'fs'
import stripAnsi from 'strip-ansi'
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
const cacheReasonRegex = /Cache (missed|skipped) reason: /
interface ParsedLog {
method: string
url: string
statusCode: number
responseTime: number
cache: string
}
function parseLogsFromCli(cliOutput: string) {
const logs = stripAnsi(cliOutput)
.split('\n')
.filter((log) => cacheReasonRegex.test(log) || log.includes('GET'))
return logs.reduce<ParsedLog[]>((parsedLogs, log) => {
if (cacheReasonRegex.test(log)) {
// cache miss/skip reason
// Example of `log`: "│ │ Cache skipped reason: (cache: no-cache)"
const reasonSegment = log.split(cacheReasonRegex, 3)[2].trim()
const reason = reasonSegment.slice(1, -1)
parsedLogs[parsedLogs.length - 1].cache = reason
} else {
// request info
const trimmedLog = log.replace(/^[^a-zA-Z]+/, '')
const [method, url, statusCode, responseTime] = trimmedLog.split(' ', 5)
parsedLogs.push({
method,
url,
statusCode: parseInt(statusCode, 10),
responseTime: parseInt(responseTime, 10),
cache: undefined,
})
}
return parsedLogs
}, [])
}
describe('app-dir - fetch logging', () => {
const { next, isNextDev } = nextTestSetup({
skipDeployment: true,
files: __dirname,
})
isNextDev &&
it('should not log requests for HMR refreshes', async () => {
const browser = await next.browser('/fetch-no-store')
let headline = await browser.waitForElementByCss('h1').text()
expect(headline).toBe('Hello World!')
const outputIndex = next.cliOutput.length
await next.patchFile(
'app/fetch-no-store/page.js',
(content) => content.replace('Hello World!', 'Hello Test!'),
async () => {
await retry(async () => {
headline = await browser.waitForElementByCss('h1').text()
expect(headline).toBe('Hello Test!')
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toInclude(' GET /fetch-no-store')
expect(logs).not.toInclude(` │ GET `)
// TODO: remove custom duration in case we increase the default.
}, 5000)
}
)
})
// TODO: remove when there is a test for isNextDev === false
it('placeholder to satisfy at least one test when isNextDev is false', async () => {
expect(true).toBe(true)
})
})
describe('app-dir - logging', () => {
const { next, isNextDev, skipped } = nextTestSetup({
skipDeployment: true,
files: __dirname,
})
if (skipped) return
function runTests({
withFetchesLogging,
withFullUrlFetches = false,
}: {
withFetchesLogging: boolean
withFullUrlFetches?: boolean
}) {
if (withFetchesLogging) {
it('should only log requests in development mode', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
if (isNextDev) {
expect(logs).toContain('GET /default-cache 200')
} else {
expect(logs).not.toContain('GET /default-cache 200')
}
})
})
if (isNextDev) {
it("should log 'skip' cache status with a reason when cache: 'no-cache' is used", async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
await retry(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) =>
log.url.includes('api/random?no-cache')
)
expect(logs.some((log) => log.url.includes('..'))).toBe(
!withFullUrlFetches
)
expect(logEntry?.cache).toBe('cache: no-cache')
})
})
it("should log 'skip' cache status with a reason when revalidate: 0 is used", async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
await retry(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) =>
log.url.includes('api/random?revalidate-0')
)
expect(logEntry?.cache).toBe('revalidate: 0')
})
})
it("should log 'skip' cache status with a reason when the browser indicates caching should be ignored", async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache', {
headers: { 'Cache-Control': 'no-cache' },
})
await retry(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) =>
log.url.includes('api/random?auto-cache')
)
expect(logEntry?.cache).toBe(
'cache-control: no-cache (hard refresh)'
)
})
})
it('should log requests with correct indentation', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
const expectedUrl = withFullUrlFetches
? 'https://next-data-api-endpoint.vercel.app/api/random'
: 'https://next-data-api-en../api/random'
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toIncludeRepeated(' GET /default-cache', 1)
expect(logs).toIncludeRepeated(` │ GET ${expectedUrl}`, 7)
expect(logs).toIncludeRepeated(' │ │ Cache skipped reason', 3)
})
})
it('should not limit the number of requests that are logged', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/many-requests')
const expectedUrl = withFullUrlFetches
? 'https://next-data-api-endpoint.vercel.app/api/random'
: 'https://next-data-api-en../api/random'
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toIncludeRepeated(` │ GET ${expectedUrl}`, 6)
expect(logs).toIncludeRepeated(` │ POST ${expectedUrl}`, 6)
})
})
it('should show cache reason of noStore when use with fetch', async () => {
const logLength = next.cliOutput.length
await next.fetch('/no-store')
await retry(() => {
const output = stripAnsi(next.cliOutput.slice(logLength))
expect(output).toContain('Cache skipped reason: (noStore call)')
})
})
it('should respect request.init.cache when use with fetch input is instance', async () => {
const logLength = next.cliOutput.length
await next.fetch('/fetch-no-store')
await retry(() => {
const output = stripAnsi(next.cliOutput.slice(logLength))
expect(output).toContain('Cache skipped reason: (cache: no-store)')
})
})
it('should log each page request only once', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/')
await retry(() => {
const logsAfterRequest = stripAnsi(
next.cliOutput.slice(outputIndex)
)
// Only show `GET /` once
expect(logsAfterRequest.split('GET /').length).toBe(2)
})
})
it('should exclude Middleware invoked and _rsc requests', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/link')
await browser.elementByCss('a#foo').click()
await browser.waitForElementByCss('h2')
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).not.toContain('/_next/static')
expect(logs).not.toContain('?_rsc')
})
it('should not log _rsc query for client navigation RSC request', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/')
await browser.elementByCss('a#nav-headers').click()
await browser.waitForElementByCss('p')
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toContain('GET /')
expect(logs).toContain('GET /headers')
expect(logs).not.toContain('/_next/static')
expect(logs).not.toContain('?_rsc')
})
it('should log requests for client-side navigations', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/')
await browser.elementById('nav-default-cache').click()
await browser.waitForElementByCss('h1')
const expectedUrl = withFullUrlFetches
? 'https://next-data-api-endpoint.vercel.app/api/random'
: 'https://next-data-api-en../api/random'
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toIncludeRepeated(` │ GET ${expectedUrl}`, 7)
})
})
it('should log requests for after revalidation via server action', async () => {
let outputIndex = next.cliOutput.length
const browser = await next.browser('/default-cache')
const expectedUrl = withFullUrlFetches
? 'https://next-data-api-endpoint.vercel.app/api/random'
: 'https://next-data-api-en../api/random'
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toIncludeRepeated(` │ GET ${expectedUrl}`, 7)
})
outputIndex = next.cliOutput.length
await browser.elementById('revalidate-button').click()
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).toIncludeRepeated(` │ GET ${expectedUrl}`, 7)
})
})
describe('when logging.fetches.hmrRefreshes is true', () => {
beforeAll(async () => {
await next.patchFile('next.config.js', (content) =>
content.replace('// hmrRefreshes: true', 'hmrRefreshes: true')
)
})
afterAll(async () => {
await next.patchFile('next.config.js', (content) =>
content.replace('hmrRefreshes: true', '// hmrRefreshes: true')
)
})
it('should log requests for HMR refreshes', async () => {
const browser = await next.browser('/fetch-no-store')
let headline = await browser.waitForElementByCss('h1').text()
expect(headline).toBe('Hello World!')
const outputIndex = next.cliOutput.length
await next.patchFile(
'app/fetch-no-store/page.js',
(content) => content.replace('Hello World!', 'Hello Test!'),
async () => {
const expectedUrl = withFullUrlFetches
? 'https://next-data-api-endpoint.vercel.app/api/random'
: 'https://next-data-api-en../api/random'
return retry(async () => {
headline = await browser.waitForElementByCss('h1').text()
expect(headline).toBe('Hello Test!')
const logs = stripAnsi(
next.cliOutput.slice(outputIndex)
).replace(/\d+ms/g, '1ms')
expect(logs).toInclude(' GET /fetch-no-store')
expect(logs).toInclude(
` │ GET ${expectedUrl}?request-input 200 in 1ms (HMR cache)`
)
// TODO: remove custom duration in case we increase the default.
}, 5000)
}
)
})
})
}
} else {
// No fetches logging enabled
it('should not log fetch requests at all', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
await retry(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).not.toContain('GET /default-cache 200')
})
})
}
if (isNextDev) {
it('should not contain trailing word page for app router routes', async () => {
const logLength = next.cliOutput.length
await next.fetch('/')
await retry(() => {
const output = stripAnsi(next.cliOutput.slice(logLength))
expect(output).toContain('/')
expect(output).not.toContain('/page')
})
})
it('should not contain metadata internal segments for dynamic metadata routes', async () => {
const logLength = next.cliOutput.length
await next.fetch('/dynamic/big/icon')
await retry(() => {
const output = stripAnsi(next.cliOutput.slice(logLength))
expect(output).toContain('/dynamic/big/icon')
expect(output).not.toContain('/(group)')
expect(output).not.toContain('[__metadata_id__]')
expect(output).not.toContain('/route')
})
})
}
}
describe('with fetches verbose logging', () => {
runTests({ withFetchesLogging: true, withFullUrlFetches: true })
})
describe('with fetches default logging', () => {
const curNextConfig = fs.readFileSync(
path.join(__dirname, 'next.config.js'),
{ encoding: 'utf-8' }
)
beforeAll(async () => {
await next.stop()
await next.patchFile(
'next.config.js',
curNextConfig.replace('fullUrl: true', 'fullUrl: false')
)
await next.start()
})
afterAll(async () => {
await next.patchFile('next.config.js', curNextConfig)
})
runTests({ withFetchesLogging: true, withFullUrlFetches: false })
})
describe('with verbose logging for edge runtime', () => {
beforeAll(async () => {
await next.stop()
const layoutContent = await next.readFile('app/layout.js')
await next.patchFile(
'app/layout.js',
layoutContent + `\nexport const runtime = 'edge'`
)
await next.start()
})
runTests({ withFetchesLogging: true, withFullUrlFetches: true })
})
describe('with default logging', () => {
const curNextConfig = fs.readFileSync(
path.join(__dirname, 'next.config.js'),
{ encoding: 'utf-8' }
)
beforeAll(async () => {
await next.stop()
await next.deleteFile('next.config.js')
await next.start()
})
afterAll(async () => {
await next.patchFile('next.config.js', curNextConfig)
})
runTests({ withFetchesLogging: false })
})
})