From e02552998d138e93e6cbd27c00d21802a17f409b Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:15:13 +0900 Subject: [PATCH] =?UTF-8?q?test(e2e):=20strict=20fixture=EA=B0=80=20preloa?= =?UTF-8?q?d=20=EA=B8=B0=EB=A1=9D=EC=9C=BC=EB=A1=9C=20test=EB=A5=BC=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=EC=8B=9C=ED=82=A4=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=EA=B3=A0=EC=A0=95=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제 tests/e2e/preload-use.spec.ts는 preload tracker만 증명한다. 안 쓴 preload를 넣는 test는 맨 test에서 tracker를 직접 부르고, strict fixture를 쓰는 test는 "통과해야 한다" 방향 하나뿐이다. 그래서 strict-browser-test.ts가 tracker 결과를 버려도(11-R3) 커밋된 test는 하나도 실패하지 않았고, WebKit 무시 정규식을 넓혀도 볼 test가 없다. 3번 test는 지연시킬 청크를 못 찾으면 조용히 평범한 방문이 된다. 고른 방법과 버린 대안 - strict fixture의 마지막 판정을 browserFailureVerdict fixture로 뺀다. 기본값은 지금과 같다(allowedBrowserFailure로 거르고 빈 목록 단언). page fixture는 기록을 그 판정에 넘긴다. - spec은 그 판정을 "기록을 받아 두는" 판정으로 바꾼 recordedTest를 둔다. page fixture가 판정에 의존하므로 판정의 teardown은 page teardown 뒤에 돈다. 거기서 fixture가 넘긴 줄이 fixtureReports와 정확히 같은지 본다. - 안 쓴 modulepreload 하나 → `preloadunused:/assets/<청크>.js` 한 줄. - WebKit 문구 그대로(무시), Chromium 문구, WebKit 문구 + 뒤 글자, 같은 문구의 console.error, 다른 warning → 뒤의 네 줄만, 순서대로. - 기본 판정이 실제로 test를 실패시키는지는 strictTest.fail()로 console error 한 줄을 내는 test 하나가 본다. 기록이 맞다는 것은 위 두 test가 따로 증명하므로, 이 test가 엉뚱한 이유로 실패해도 놓치는 것이 적다. - 3번 test는 지연시킬 청크가 없으면 이유를 적고 실패한다. - 모든 probe를 test.fail()로만 고정하는 안은 버렸다. 어떤 이유로든 실패하면 통과해서 fixture가 무엇을 기록했는지 보지 못한다. - 커밋한 문서: docs/testing/frontend-platform-testing-strategy.md에 증명 위치와 판정을 바꾸는 곳이 그 spec 하나라는 것을 적는다. 위험·한계 - 모든 browser spec이 쓰는 fixture의 구조가 바뀐다. 판정 내용은 같다. - 다른 spec도 browserFailureVerdict를 바꿔 strict 검사를 끌 수 있다. allowedBrowserFailure를 /.*/로 주는 것과 같은 수준의 노출이고 막는 검사는 더하지 않았다. - 샘플을 지운 제품에서 기능 실행부 모양이 바뀌면 3번 test가 실패한다. 그때는 test를 다른 청크로 옮기거나 이유를 적고 지워야 한다. 검증 - desktop batch에서 확인: corepack pnpm exec playwright test tests/e2e/preload-use.spec.ts (세 엔진) - 테스트 장치만 바꾼 커밋이라 RED는 변이로 본다. - 11-R3(strict fixture가 check 결과를 버림): "records a modulepreload the page never runs"가 세 엔진에서 실패해야 한다. - WEBKIT_UNUSED_PRELOAD_GUESS를 /.*/로: "ignores WebKit's unused-preload guess word for word and nothing else"가 실패해야 한다. - browserFailureVerdict 기본값의 expect 제거: "fails a test whose page writes a console error"가 "expected to fail" 위반으로 실패해야 한다. Refs: 1105/11-01 --- .../frontend-platform-testing-strategy.md | 4 +- tests/e2e/preload-use.spec.ts | 151 ++++++++++++++++-- tests/support/browser/strict-browser-test.ts | 38 ++++- 3 files changed, 170 insertions(+), 23 deletions(-) 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); }, });