diff --git a/tests/e2e/old-tab-chunk-recovery.spec.ts b/tests/e2e/old-tab-chunk-recovery.spec.ts index 9ef45f8c..142eed2f 100644 --- a/tests/e2e/old-tab-chunk-recovery.spec.ts +++ b/tests/e2e/old-tab-chunk-recovery.spec.ts @@ -21,10 +21,9 @@ import { startBuildServer } from "./support/service-worker-builds.ts"; const PLATFORM = ROUTE_REGISTRY.EXAMPLES_PLATFORM; const UI = ROUTE_REGISTRY.EXAMPLES_UI; -const PAGES = [ - "src/presentation/examples/platform-overview-page.tsx", - "src/presentation/examples/ui-gallery-page.tsx", -] as const; +const PLATFORM_PAGE = "src/presentation/examples/platform-overview-page.tsx"; +const UI_PAGE = "src/presentation/examples/ui-gallery-page.tsx"; +const PAGES = [PLATFORM_PAGE, UI_PAGE] as const; let builds: FixtureBuilds<"a" | "b"> | undefined; @@ -60,20 +59,57 @@ async function routeChunk(dist: string, chunkId: string): Promise { // Each missing page chunk fails its request and the dynamic import. Chromium // may also log the 404 itself (without its URL), Firefox logs the error // object React caught, and WebKit logs its own import error without the URL. -// Nothing else is allowed. +// Nothing else is allowed, but for the WebKit warning below. const MISSING_PAGE = String.raw`/assets/(?:platform-overview|ui-gallery)-page-[\w-]+\.js`; +const MISSING_PAGE_FAILURES = [ + String.raw`^requestfailed:GET:${MISSING_PAGE}:`, + String.raw`^console:error:TypeError: Failed to fetch dynamically imported module: http://127\.0\.0\.1:\d+${MISSING_PAGE}$`, + String.raw`^console:error:\[JavaScript Error: "Loading module from “http://127\.0\.0\.1:\d+${MISSING_PAGE}” was blocked`, + String.raw`^console:error:Failed to load resource: the server responded with a status of 404 \(Not Found\)$`, + String.raw`^console:error:JSHandle@object$`, + String.raw`^console:error:TypeError: Importing a module script failed\.$`, +]; + +/** The chunks Vite preloads with `page`: its static imports in `dist`. */ +async function preloadedWith(dist: string, page: string): Promise { + const manifest = JSON.parse( + await readFile(path.join(dist, ".vite/manifest.json"), "utf8"), + ) as Record; + if (!manifest[page]) throw new Error(`${page} is not in ${dist}'s manifest.`); + const files = new Set(); + const pending = [...(manifest[page].imports ?? [])]; + for (let key = pending.pop(); key !== undefined; key = pending.pop()) { + const entry = manifest[key]; + if (!entry || files.has(entry.file)) continue; + files.add(entry.file); + pending.push(...(entry.imports ?? [])); + } + return [...files]; +} + +// WebKit only. Three seconds after a document's load event, WebKit warns once +// for each link-preloaded resource the document has not used yet. Vite +// preloads a page chunk together with the chunks it imports, so a missing page +// leaves the chunks preloaded with it unused: in the A tab those of A's +// platform page (the tab is still on A while recovery reads the release +// manifest), in the B tab those of B's UI page. Whether that check runs before +// the reload, or before the test ends, is timing, so the warnings come and go. +// They are the provoked failure's own effect; only those chunks are allowed. test.use({ - allowedBrowserFailure: new RegExp( - [ - String.raw`^requestfailed:GET:${MISSING_PAGE}:`, - String.raw`^console:error:TypeError: Failed to fetch dynamically imported module: http://127\.0\.0\.1:\d+${MISSING_PAGE}$`, - String.raw`^console:error:\[JavaScript Error: "Loading module from “http://127\.0\.0\.1:\d+${MISSING_PAGE}” was blocked`, - String.raw`^console:error:Failed to load resource: the server responded with a status of 404 \(Not Found\)$`, - String.raw`^console:error:JSHandle@object$`, - String.raw`^console:error:TypeError: Importing a module script failed\.$`, - ].join("|"), - "u", - ), + allowedBrowserFailure: async ({ browserName }, provide) => { + const patterns = [...MISSING_PAGE_FAILURES]; + if (browserName === "webkit") { + if (!builds) throw new Error("Fixture builds were not prepared."); + const files = [ + ...(await preloadedWith(builds.dist.a, PLATFORM_PAGE)), + ...(await preloadedWith(builds.dist.b, UI_PAGE)), + ].map((file) => file.replaceAll(".", String.raw`\.`)); + patterns.push( + String.raw`^console:warning:The resource http://127\.0\.0\.1:\d+/(?:${files.join("|")}) 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\.$`, + ); + } + await provide(new RegExp(patterns.join("|"), "u")); + }, }); test("an old tab reloads once into the new build, then stops reloading", async ({