diff --git a/docs/testing/frontend-platform-testing-strategy.md b/docs/testing/frontend-platform-testing-strategy.md index d4fa1549..af141a14 100644 --- a/docs/testing/frontend-platform-testing-strategy.md +++ b/docs/testing/frontend-platform-testing-strategy.md @@ -149,7 +149,9 @@ page error, 실패한 request, 끝까지 실행되지 않은 preload를 test 실 WebKit의 "preload했지만 몇 초 안에 쓰지 않았다" 경고는 load 3초 뒤 한 번 하는 추정이라 그 문구만 무시하고, 대신 preload된 build script가 실제로 실행됐는지를 세 엔진에서 같은 방법으로 검사한다(`tests/support/browser/preload-use.ts`, -1103/11-09). 시간은 각 test가 +1103/11-09). 이 검사와 fixture가 그 기록으로 test를 실패시키는지는 +`tests/e2e/preload-use.spec.ts`가 증명한다. fixture의 마지막 판정 +(`browserFailureVerdict`)을 바꾸는 곳은 그 spec 하나다(1105/11-01). 시간은 각 test가 `vi.useFakeTimers()`로 통제하고, 전역 `afterEach`가 실제 timer로 되돌린다 (`tests/setup.ts`). 공용 clock/random/scheduler helper는 두지 않는다. diff --git a/tests/e2e/preload-use.spec.ts b/tests/e2e/preload-use.spec.ts index a891cfa0..65373cd0 100644 --- a/tests/e2e/preload-use.spec.ts +++ b/tests/e2e/preload-use.spec.ts @@ -1,6 +1,6 @@ import { readFile } from "node:fs/promises"; -import { expect, test } from "@playwright/test"; +import { expect, test, type Page } from "@playwright/test"; import { trackPreloadUse } from "../support/browser/preload-use.ts"; import { test as strictTest } from "../support/browser/strict-browser-test.ts"; @@ -12,7 +12,8 @@ import { test as strictTest } from "../support/browser/strict-browser-test.ts"; * reaches a moment later was reported as wasted, and one the page never runs * went unreported when the test ended first. The fixture checks instead, in * every engine, that each preloaded script was run by the end of the test. - * These tests prove that check on a real build. + * These tests prove that check on a real build: the tracker itself, and + * (1105/11-01) the strict fixture that fails a test with what it records. */ type ManifestEntry = Readonly<{ @@ -42,15 +43,19 @@ function staticClosure( return reached; } -test("reports a modulepreload the page never runs", async ({ page }) => { - const preloads = await trackPreloadUse(page); +async function openHome(page: Page): Promise { await page.goto("/"); await expect(page.getByRole("heading", { level: 1 })).toBeVisible(); +} +/** + * Mutation: one build chunk the page has not fetched, preloaded for nothing. + * Returns its path once the browser has fetched it. + */ +async function preloadAChunkThePageNeverRuns(page: Page): Promise { const scripts = Object.values(await buildManifest()) .map((entry) => `/${entry.file}`) .filter((file) => file.endsWith(".js")); - // Mutation: one chunk the page has not fetched, preloaded for nothing. const unused = await page.evaluate(async (files) => { const fetched = new Set( performance @@ -72,6 +77,18 @@ test("reports a modulepreload the page never runs", async ({ page }) => { .some((entry) => new URL(entry.name).pathname === file), unused, ); + return unused; +} + +/** The warning text WebKit writes for its three-second guess. */ +const webKitUnusedPreloadGuess = (url: string) => + `The resource ${url} 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.`; + +test("reports a modulepreload the page never runs", async ({ page }) => { + const preloads = await trackPreloadUse(page); + await openHome(page); + + const unused = await preloadAChunkThePageNeverRuns(page); expect((await preloads.check({ timeoutMs: 1_000 })).failures).toEqual([ `preloadunused:${unused}`, @@ -121,18 +138,23 @@ strictTest( !initial.has(key) && (manifest[key]?.imports ?? []).some((child) => !initial.has(child)), ); + // 1105/11-01. Without that chunk there is nothing to delay and this would + // pass as a plain visit. Point it at another chunk with the same shape, or + // remove the test with a reason. + expect( + parent, + "no feature runtime chunk imports a chunk the boot has not loaded", + ).toBeDefined(); await page.goto("/"); - if (parent !== undefined) { - const file = `/${manifest[parent]?.file}`; - await page.route( - (url) => url.pathname === file, - async (route) => { - await new Promise((resolve) => setTimeout(resolve, 3_500)); - await route.fallback(); - }, - ); - } + const file = `/${manifest[parent ?? ""]?.file}`; + await page.route( + (url) => url.pathname === file, + async (route) => { + await new Promise((resolve) => setTimeout(resolve, 3_500)); + await route.fallback(); + }, + ); await page .getByRole("navigation", { name: "주요 탐색" }) .getByRole("link", { name: "플랫폼 구성" }) @@ -142,3 +164,102 @@ strictTest( ).toBeVisible({ timeout: 15_000 }); }, ); + +/** + * 1105/11-01. The tests above prove the tracker. These prove the strict + * fixture: what it records after the test body, and that it fails the test for + * it. `fixtureReports` names the lines the fixture must hand its verdict, in + * order, and nothing else; the verdict is read once the `page` fixture has + * ended. One object, not an array: `test.use` reads an array as a + * `[value, options]` fixture tuple. + */ +type FixtureReports = Readonly<{ lines: readonly RegExp[] }>; + +const recordedTest = strictTest.extend<{ fixtureReports: FixtureReports }>({ + fixtureReports: [{ lines: [] }, { option: true }], + browserFailureVerdict: async ({ allowedBrowserFailure, fixtureReports }, provide) => { + let reported: readonly string[] | undefined; + await provide( + Object.freeze({ + judge(failures: readonly string[]) { + reported = failures.filter( + (failure) => !allowedBrowserFailure?.test(failure), + ); + }, + }), + ); + // Torn down after `page`, which hands over its record when it ends. + expect(reported, "the strict fixture handed over no record").toBeDefined(); + expect(reported, "what the strict fixture recorded").toHaveLength( + fixtureReports.lines.length, + ); + fixtureReports.lines.forEach((line, index) => { + expect(reported?.[index], "what the strict fixture recorded").toMatch(line); + }); + }, +}); + +recordedTest.describe("the strict fixture's preload check", () => { + recordedTest.use({ + fixtureReports: { lines: [/^preloadunused:\/assets\/[^/]+\.js$/u] }, + }); + + recordedTest( + "records a modulepreload the page never runs", + async ({ page }) => { + await openHome(page); + + await preloadAChunkThePageNeverRuns(page); + }, + ); +}); + +recordedTest.describe("the strict fixture's console check", () => { + const url = "http://127.0.0.1:4173/assets/preload-use-probe.js"; + const guess = webKitUnusedPreloadGuess(url); + recordedTest.use({ + fixtureReports: { + lines: [ + // Chromium's wording of the same warning. + /^console:warning: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 has an appropriate `as` value and it is preloaded intentionally\.$/u, + /^console:warning:The resource \S+ .* \(and more\)$/u, + /^console:error:The resource \S+ was preloaded using link preload /u, + /^console:warning:\[preload-use\] another warning$/u, + ], + }, + }); + + recordedTest( + "ignores WebKit's unused-preload guess word for word and nothing else", + async ({ page }) => { + await openHome(page); + + await page.evaluate( + ({ text, address }) => { + console.warn(text); + console.warn( + `The resource ${address} was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate \`as\` value and it is preloaded intentionally.`, + ); + console.warn(`${text} (and more)`); + console.error(text); + console.warn("[preload-use] another warning"); + }, + { text: guess, address: url }, + ); + }, + ); +}); + +/** + * 1105/11-01. The verdict every other spec gets: a recorded failure fails the + * test. The record is proven above; this proves the verdict. + */ +strictTest( + "fails a test whose page writes a console error", + async ({ page }) => { + strictTest.fail(); + await openHome(page); + + await page.evaluate(() => console.error("[preload-use] this test must fail")); + }, +); diff --git a/tests/support/browser/strict-browser-test.ts b/tests/support/browser/strict-browser-test.ts index 0ba19808..0f144bee 100644 --- a/tests/support/browser/strict-browser-test.ts +++ b/tests/support/browser/strict-browser-test.ts @@ -24,6 +24,14 @@ function ignoredConsole(message: ConsoleMessage) { ); } +/** + * 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 @@ -34,9 +42,30 @@ export const test = base.extend<{ * 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 }], - page: async ({ page, allowedBrowserFailure }, use) => { + 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) => { @@ -63,12 +92,7 @@ export const test = base.extend<{ page.off("console", onConsole); page.off("pageerror", onPageError); page.off("requestfailed", onRequestFailed); - expect( - failures.filter( - (failure) => !allowedBrowserFailure?.test(failure), - ), - "unexpected browser console/page errors", - ).toEqual([]); + browserFailureVerdict.judge(failures); }, });