Files
clean-architecture-frontend…/tests/e2e/preload-use.spec.ts
T
DongHyeonka e02552998d test(e2e): strict fixture가 preload 기록으로 test를 실패시키는지 고정한다
문제
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
2026-11-06 01:05:22 +09:00

266 lines
9.3 KiB
TypeScript

import { readFile } from "node:fs/promises";
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";
/**
* 1103/11-09. The strict browser fixture ignores WebKit's "preloaded using
* link preload but not used within a few seconds" warning. WebKit checks once,
* three seconds after the load event, so a preload whose module the page
* 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: the tracker itself, and
* (1105/11-01) the strict fixture that fails a test with what it records.
*/
type ManifestEntry = Readonly<{
file: string;
imports?: readonly string[];
isEntry?: boolean;
}>;
async function buildManifest(): Promise<Readonly<Record<string, ManifestEntry>>> {
return JSON.parse(await readFile("dist/.vite/manifest.json", "utf8")) as Record<
string,
ManifestEntry
>;
}
function staticClosure(
manifest: Readonly<Record<string, ManifestEntry>>,
roots: readonly string[],
): Set<string> {
const reached = new Set<string>();
const pending = [...roots];
for (let key = pending.pop(); key !== undefined; key = pending.pop()) {
if (reached.has(key) || !manifest[key]) continue;
reached.add(key);
pending.push(...(manifest[key].imports ?? []));
}
return reached;
}
async function openHome(page: Page): Promise<void> {
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<string> {
const scripts = Object.values(await buildManifest())
.map((entry) => `/${entry.file}`)
.filter((file) => file.endsWith(".js"));
const unused = await page.evaluate(async (files) => {
const fetched = new Set(
performance
.getEntriesByType("resource")
.map((entry) => new URL(entry.name).pathname),
);
const file = files.find((candidate) => !fetched.has(candidate));
if (file === undefined) throw new Error("every build chunk was fetched");
const link = document.createElement("link");
link.rel = "modulepreload";
link.href = file;
document.head.append(link);
return file;
}, scripts);
await page.waitForFunction(
(file) =>
performance
.getEntriesByType("resource")
.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}`,
]);
});
test("counts the chunks a route preloads and then runs as used", async ({
page,
}) => {
const preloads = await trackPreloadUse(page);
await page.goto("/");
await page
.getByRole("navigation", { name: "주요 탐색" })
.getByRole("link", { name: "플랫폼 구성" })
.click();
await expect(
page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }),
).toBeVisible();
const result = await preloads.check();
expect(result.preloaded.length).toBeGreaterThan(0);
expect(result.failures).toEqual([]);
});
/**
* The 1103/11-09 failure, made deterministic: the route's runtime chunks are
* preloaded right after load, and the chunk that imports the deepest one
* arrives 3.5 s later. WebKit's check runs in between and warns about a chunk
* the page then runs. The strict fixture must pass.
*/
strictTest(
"passes a route whose deepest preloaded chunk is reached after WebKit's check",
async ({ page }) => {
const manifest = await buildManifest();
const initial = staticClosure(manifest, [
...Object.keys(manifest).filter((key) => manifest[key]?.isEntry),
"src/bootstrap/boot.tsx",
]);
// A feature runtime's static import that itself imports a chunk the
// boot has not loaded: the module graph reaches that chunk only once
// this one has arrived.
const parent = Object.entries(manifest)
.filter(([key]) => /^src\/features\/[^/]+\/feature-runtime\.ts$/u.test(key))
.flatMap(([, runtime]) => runtime.imports ?? [])
.find(
(key) =>
!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("/");
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: "플랫폼 구성" })
.click();
await expect(
page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }),
).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"));
},
);