Files
Tim NeutkensandGitHub e860cec656 test: migrate webdriver callers to next.browser (#93941)
### What?

Migrate remaining direct `next-webdriver` test callers that have a
`NextInstance` to `next.browser()`, and expose the shared `Playwright`
browser type from `e2e-utils`.

### Why?

`NextInstance.browser` should be the supported browser-opening interface
for test fixtures, with `next-webdriver` kept as the private
implementation detail.

### How?

Updated affected development, e2e, and production tests to call
`next.browser()` directly, passing `baseUrl` where tests intentionally
target a manually spawned or proxied server. Shared helpers now receive
browser callbacks from the test context, and browser types import
`Playwright` from `e2e-utils` instead of deriving from `next.browser` or
importing from private paths.

<!-- NEXT_JS_LLM_PR -->
2026-05-22 14:01:58 +02:00

127 lines
3.7 KiB
TypeScript

/* eslint-env jest */
// These tests are defined here and used in `app-dir.test.ts` and
// `pages-dir.test.ts` so that both test suites can be run in parallel.
import cheerio from 'cheerio'
import type { NextInstance, Playwright } from 'e2e-utils'
export function testShouldRedirect(
next: NextInstance,
expectations: [string, string][]
) {
it.each(expectations)(
'%s should redirect to %s',
async (route, expectedLocation) => {
const res = await next.fetch(route, { redirect: 'manual' })
expect(res.status).toBe(308)
const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe(expectedLocation)
}
)
}
export function testShouldResolve(
next: NextInstance,
expectations: [string, string, string][]
) {
it.each(expectations)(
'%s should resolve to %s, with router path %s',
async (route, expectedPage, expectedRouterPath) => {
const res = await next.fetch(route, { redirect: 'error' })
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('#page-marker').text()).toBe(expectedPage)
expect($('#router-pathname').text()).toBe(expectedRouterPath)
}
)
it.each(expectations)(
'%s should client side render %s, with router path %s',
async (route, expectedPage, expectedRouterPath) => {
let browser: Playwright | undefined
try {
browser = await next.browser(route)
await browser.waitForElementByCss('#hydration-marker', {
state: 'attached',
})
const text = await browser.elementByCss('#page-marker').text()
expect(text).toBe(expectedPage)
const routerPathname = await browser
.elementByCss('#router-pathname')
.text()
expect(routerPathname).toBe(expectedRouterPath)
} finally {
if (browser) await browser.close()
}
}
)
}
export function testExternalLinkShouldRewriteTo(
next: NextInstance,
expectations: [string, string][]
) {
it.each(expectations)(
'%s should have href %s',
async (linkPage, expectedHref) => {
const $ = await next.render$(linkPage)
expect($('#link').attr('href')).toBe(expectedHref)
}
)
}
export function testLinkShouldRewriteTo(
next: NextInstance,
expectations: [string, string][]
) {
it.each(expectations)(
'%s should have href %s',
async (linkPage, expectedHref) => {
const $ = await next.render$(linkPage)
expect($('#link').attr('href')).toBe(expectedHref)
}
)
it.each(expectations)(
'%s should navigate to %s',
async (linkPage, expectedHref) => {
let browser: Playwright | undefined
try {
browser = await next.browser(linkPage)
await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#hydration-marker', {
state: 'attached',
})
const url = new URL(await browser.eval('window.location.href'))
const pathname = url.href.slice(url.origin.length)
expect(pathname).toBe(expectedHref)
} finally {
if (browser) await browser.close()
}
}
)
it.each(expectations)(
'%s should push route to %s',
async (linkPage, expectedHref) => {
let browser: Playwright | undefined
try {
browser = await next.browser(linkPage)
await browser.elementByCss('#route-pusher').click()
await browser.waitForElementByCss('#hydration-marker', {
state: 'attached',
})
const url = new URL(await browser.eval('window.location.href'))
const pathname = url.href.slice(url.origin.length)
expect(pathname).toBe(expectedHref)
} finally {
if (browser) await browser.close()
}
}
)
}