Files
vercel__next.js/test/e2e/prerender-preview/prerender-preview.test.ts
Tim Neutkens 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

372 lines
12 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import cheerio from 'cheerio'
import cookie from 'cookie'
import qs from 'querystring'
function getData(html: string) {
const $ = cheerio.load(html)
const nextData = $('#__NEXT_DATA__')
const preEl = $('#props-pre')
const routerData = JSON.parse($('#router').text())
return {
nextData: JSON.parse(nextData.html()),
pre: preEl.text(),
routerData,
}
}
describe('Prerender Preview Mode', () => {
const { next, isNextDev, isNextStart } = nextTestSetup({
files: __dirname,
dependencies: {
cookie: '0.7.2',
},
})
if (isNextStart) {
it('should return prerendered page on first request', async () => {
const html = await next.render('/')
const { nextData, pre, routerData } = getData(html)
expect(nextData).toMatchObject({ isFallback: false })
expect(nextData.isPreview).toBeUndefined()
expect(pre).toBe('false and null')
expect(routerData.isPreview).toBe(false)
})
it('should return prerendered page on second request', async () => {
const html = await next.render('/')
const { nextData, pre, routerData } = getData(html)
expect(nextData).toMatchObject({ isFallback: false })
expect(nextData.isPreview).toBeUndefined()
expect(pre).toBe('false and null')
expect(routerData.isPreview).toBe(false)
})
}
it('should throw error when setting too large of preview data', async () => {
const res = await next.fetch('/api/preview?tooBig=true')
expect(res.status).toBe(500)
expect(await res.text()).toBe('too big')
})
let previewCookieString: string
it('should enable preview mode', async () => {
const res = await next.fetch('/api/preview?lets=goooo')
expect(res.status).toBe(200)
const originalCookies = res.headers.get('set-cookie')!.split(',')
const cookies = originalCookies.map((cookieRaw) => cookie.parse(cookieRaw))
if (isNextStart) {
expect(originalCookies.every((c) => c.includes('; Secure;'))).toBe(true)
}
expect(cookies.length).toBe(2)
if (isNextStart) {
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'None' })
}
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
if (isNextStart) {
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'None' })
}
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
previewCookieString =
cookie.serialize('__prerender_bypass', cookies[0].__prerender_bypass) +
'; ' +
cookie.serialize('__next_preview_data', cookies[1].__next_preview_data)
})
it('should expire cookies with a maxAge', async () => {
const expiry = '60'
const res = await next.fetch(`/api/preview?cookieMaxAge=${expiry}`)
expect(res.status).toBe(200)
const originalCookies = res.headers.get('set-cookie')!.split(',')
const cookies = originalCookies.map((cookieRaw) => cookie.parse(cookieRaw))
if (isNextStart) {
expect(originalCookies.every((c) => c.includes('; Secure;'))).toBe(true)
}
expect(cookies.length).toBe(2)
if (isNextStart) {
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'None' })
}
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]['Max-Age']).toBe(expiry)
if (isNextStart) {
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'None' })
}
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]['Max-Age']).toBe(expiry)
})
it('should set custom path cookies', async () => {
const path = '/path'
const res = await next.fetch(`/api/preview?cookiePath=${path}`)
expect(res.status).toBe(200)
const originalCookies = res.headers.get('set-cookie')!.split(',')
const cookies = originalCookies.map((cookieRaw) => cookie.parse(cookieRaw))
if (isNextStart) {
expect(originalCookies.every((c) => c.includes('; Secure;'))).toBe(true)
}
expect(cookies.length).toBe(2)
if (isNextStart) {
expect(cookies[0]).toMatchObject({ Path: path, SameSite: 'None' })
}
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]['Path']).toBe(path)
if (isNextStart) {
expect(cookies[1]).toMatchObject({ Path: path, SameSite: 'None' })
}
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]['Path']).toBe(path)
})
it('should not return fallback page on preview request', async () => {
const res = await next.fetch('/', {
headers: { Cookie: previewCookieString },
})
const html = await res.text()
const { nextData, pre, routerData } = getData(html)
if (isNextStart) {
expect(res.headers.get('cache-control')).toBe(
'private, no-cache, no-store, max-age=0, must-revalidate'
)
}
expect(nextData).toMatchObject({ isFallback: false, isPreview: true })
expect(pre).toBe('true and {"lets":"goooo"}')
expect(routerData.isPreview).toBe(true)
})
if (isNextStart) {
it('should return correct caching headers for data preview request', async () => {
const res = await next.fetch(
`/_next/data/${encodeURI(next.buildId)}/index.json`,
{ headers: { Cookie: previewCookieString } }
)
const json = await res.json()
expect(res.headers.get('cache-control')).toBe(
'private, no-cache, no-store, max-age=0, must-revalidate'
)
expect(json).toMatchObject({
pageProps: {
preview: true,
previewData: { lets: 'goooo' },
},
})
})
}
it('should return cookies to be expired on reset request', async () => {
const res = await next.fetch('/api/reset', {
headers: { Cookie: previewCookieString },
})
expect(res.status).toBe(200)
const cookies = res.headers
.get('set-cookie')!
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map((cookieRaw) => cookie.parse(cookieRaw))
expect(cookies.length).toBe(2)
if (isNextStart) {
expect(cookies[0]).toMatchObject({
Path: '/',
SameSite: 'None',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
}
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
if (isNextStart) {
expect(cookies[1]).toMatchObject({
Path: '/',
SameSite: 'None',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
}
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
})
it('should return cookies to be expired on reset request with path specified', async () => {
const res = await next.fetch('/api/reset?cookiePath=/blog', {
headers: { Cookie: previewCookieString },
})
expect(res.status).toBe(200)
const cookies = res.headers
.get('set-cookie')!
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map((cookieRaw) => cookie.parse(cookieRaw))
expect(cookies.length).toBe(2)
if (isNextStart) {
expect(cookies[0]).toMatchObject({
Path: '/blog',
SameSite: 'None',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
}
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
if (isNextStart) {
expect(cookies[1]).toMatchObject({
Path: '/blog',
SameSite: 'None',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
}
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
})
it('should pass undefined to API routes when not in preview', async () => {
const res = await next.fetch('/api/read')
const json = await res.json()
expect(json).toMatchObject({})
})
it('should pass the preview data to API routes', async () => {
const res = await next.fetch('/api/read', {
headers: { Cookie: previewCookieString },
})
const json = await res.json()
expect(json).toMatchObject({
preview: true,
previewData: { lets: 'goooo' },
})
})
if (isNextStart) {
it('should compile successfully', async () => {
expect(next.cliOutput).toMatch(/Compiled successfully/)
expect(next.cliOutput).not.toContain('Build error occurred')
})
it('should start production application', async () => {
const res = await next.fetch('/')
expect(res.status).toBe(200)
})
}
if (isNextDev) {
it('should start development application', async () => {
const html = await next.render('/')
expect(html).toBeTruthy()
})
it('should enable preview mode in dev', async () => {
const res = await next.fetch(
'/api/preview?' + qs.stringify({ lets: 'goooo' })
)
expect(res.status).toBe(200)
const cookies = res.headers
.get('set-cookie')!
.split(',')
.map((cookieRaw) => cookie.parse(cookieRaw))
expect(cookies.length).toBe(2)
})
it('should return cookies to be expired after dev server reboot', async () => {
const res = await next.fetch('/', {
headers: {
Cookie:
'__prerender_bypass=stale-value; __next_preview_data=stale-data',
},
})
expect(res.status).toBe(200)
const body = await res.text()
expect(body).not.toContain('"err"')
expect(body).not.toContain('TypeError')
expect(body).not.toContain('previewModeId')
const cookies = res.headers
.get('set-cookie')!
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map((cookieRaw) => cookie.parse(cookieRaw))
expect(cookies.length).toBe(2)
})
it('should start the client-side browser', async () => {
const browser = await next.browser(
'/api/preview?' + qs.stringify({ client: 'mode' })
)
const url = await browser.url()
expect(url).toContain('/api/preview')
})
it('should fetch preview data on SSR via browser', async () => {
const browser = await next.browser(
'/api/preview?' + qs.stringify({ client: 'mode' })
)
await browser.loadPage(next.url + '/')
await browser.waitForElementByCss('#props-pre')
expect(await browser.elementById('props-pre').text()).toBe(
'true and {"client":"mode"}'
)
})
it('should fetch preview data on CST', async () => {
const browser = await next.browser(
'/api/preview?' + qs.stringify({ client: 'mode' })
)
await browser.loadPage(next.url + '/to-index')
await browser.waitForElementByCss('#to-index')
await browser.eval('window.itdidnotrefresh = "hello"')
await browser.elementById('to-index').click()
await browser.waitForElementByCss('#props-pre')
expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
expect(await browser.elementById('props-pre').text()).toBe(
'true and {"client":"mode"}'
)
})
it('should fetch prerendered data', async () => {
const browser = await next.browser('/api/reset')
await browser.loadPage(next.url + '/')
await browser.waitForElementByCss('#props-pre')
expect(await browser.elementById('props-pre').text()).toBe(
'false and null'
)
})
it('should fetch live static props with preview active', async () => {
const browser = await next.browser(
'/api/preview?' + qs.stringify({ client: 'mode' })
)
await browser.loadPage(next.url + '/')
await browser.waitForElementByCss('#ssg-random')
const initialRandom = await browser.elementById('ssg-random').text()
await browser.elementById('reload-props').click()
await browser.waitForElementByCss('#ssg-reloaded')
expect(await browser.elementById('ssg-random').text()).not.toBe(
initialRandom
)
})
}
})