import { expect, test as base, type ConsoleMessage, type Page, } from "@playwright/test"; import { trackPreloadUse } from "./preload-use.ts"; /** * 1103/11-09. WebKit's guess, three seconds after the load event, that a * preload went unused. It fired for chunks the page ran a moment later and * missed preloads the page never ran once the test had ended. The fixture * checks preload use itself, in every engine (`preloadunused:` below). */ const WEBKIT_UNUSED_PRELOAD_GUESS = /^The resource \S+ was preloaded using link preload but not used within a few seconds from the window's load event\. Please make sure it wasn't preloaded for nothing\.$/u; function ignoredConsole(message: ConsoleMessage) { return ( message.type() === "warning" && (message.text().includes("NO_COLOR") || WEBKIT_UNUSED_PRELOAD_GUESS.test(message.text())) ); } /** * 1105/11-01. What the strict `page` fixture does with every failure it * recorded, once the test has ended. */ export type BrowserFailureVerdict = Readonly<{ judge(failures: readonly string[]): void; }>; export const test = base.extend<{ /** * Browser failures a test provokes on purpose, matched against the recorded * `console:…`, `pageerror:…`, `requestfailed:…` or `preloadunused:…` line * (for example the browser's log line for a mocked 4xx, or the chunks * preloaded with a chunk the test removed). Everything else still fails. One * pattern, not an array: `test.use` reads an array as a `[value, options]` * fixture tuple. */ allowedBrowserFailure: RegExp | undefined; /** * Fails the test for every recorded failure `allowedBrowserFailure` does not * match. The record is made after the test body, where no test can read it, * so tests/e2e/preload-use.spec.ts replaces this verdict to read what the * fixture recorded and prove the record itself. No other spec replaces it. */ browserFailureVerdict: BrowserFailureVerdict; }>({ allowedBrowserFailure: [undefined, { option: true }], browserFailureVerdict: async ({ allowedBrowserFailure }, use) => { await use( Object.freeze({ judge(failures: readonly string[]) { expect( failures.filter( (failure) => !allowedBrowserFailure?.test(failure), ), "unexpected browser console/page errors", ).toEqual([]); }, }), ); }, page: async ({ page, browserFailureVerdict }, use) => { const preloads = await trackPreloadUse(page); const failures: string[] = []; const onConsole = (message: ConsoleMessage) => { if ( ["error", "warning"].includes(message.type()) && !ignoredConsole(message) ) { failures.push(`console:${message.type()}:${message.text()}`); } }; const onPageError = (error: Error) => { failures.push(`pageerror:${error.name}`); }; const onRequestFailed = (request: import("@playwright/test").Request) => { failures.push( `requestfailed:${request.method()}:${new URL(request.url()).pathname}:${request.failure()?.errorText ?? "unknown"}`, ); }; page.on("console", onConsole); page.on("pageerror", onPageError); page.on("requestfailed", onRequestFailed); await use(page); failures.push(...(await preloads.check()).failures); page.off("console", onConsole); page.off("pageerror", onPageError); page.off("requestfailed", onRequestFailed); browserFailureVerdict.judge(failures); }, }); export { expect }; export type { Page };