Files
clean-architecture-frontend…/tests/e2e/preload-use.spec.ts
T
DongHyeonka 794a51a37d test(e2e): 참조 기능 대신 생성 기능을 넣은 빌드로 기능 경로를 확인한다
참조 기능 e2e 3개가 "기능 하나가 실제 브라우저에서 라우트·폼·
저장까지 된다"를 맡는다. 청크 복구, 부트 실패, preload 시험도 설치된
기능에 기댄다.

S4 9.1절 (가)대로 한다. scripts/lib/generated-feature.ts가 저장소
사본에 create:feature order-feature --with-create --keyed를 만들고,
spec은 그 사본의 빌드를 띄워 돈다. 참조 e2e의 검사(로그인, 목록, 폼
검증·이탈 확인·320px, en-US, 저장과 멱등 키, 429 뒤 다시 시도, 결과
불명과 같은 키 재전송, 키보드 포커스, 상태별 axe)를 생성 기능에
옮겼다. 오래된 탭의 청크 복구는 생성 기능 목록 라우트를, 부트 실패는
생성 기능을 넣은 사본을, preload 시험은 생성 기능 실행부 청크를 쓴다.

위험: 생성 기능의 동작만 본다. 제품 기능은 자기 spec이 필요하다.
사본 빌드가 spec과 브라우저마다 돈다(빌드 1초 안팎).

검증(desktop): 샘플을 지운 최종 트리에서 test:e2e 183 passed,
test:a11y 통과.
2026-11-08 14:07:53 +09:00

311 lines
11 KiB
TypeScript

import { readFile } from "node:fs/promises";
import path from "node:path";
import { expect, test, type Page } from "@playwright/test";
import { testRunPath } from "../../scripts/lib/run-isolation.ts";
import { trackPreloadUse } from "../support/browser/preload-use.ts";
import { test as strictTest } from "../support/browser/strict-browser-test.ts";
import {
prepareFixtureBuilds,
removeFixtureBuilds,
type FixtureBuilds,
} from "./support/fixture-builds.ts";
import { addGeneratedFeature } from "./support/generated-feature.ts";
import { startBuildServer } from "./support/service-worker-builds.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;
}>;
/**
* The build the preview serves: this run's own under TEST_RUN_ID (1105/12-04),
* or `dist`.
*/
async function buildManifest(
dist = testRunPath("dist"),
): Promise<Readonly<Record<string, ManifestEntry>>> {
return JSON.parse(await readFile(path.join(dist, ".vite/manifest.json"), "utf8")) as Record<
string,
ManifestEntry
>;
}
/** The platform overview has drawn its contract tables, or their empty state. */
async function expectContractTables(page: Page): Promise<void> {
await expect(
page
.getByRole("table", { name: "설치된 HTTP 오퍼레이션" })
.or(page.getByText("설치된 HTTP 오퍼레이션이 없습니다.")),
).toBeVisible();
}
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 expectContractTables(page);
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. The template installs no
* feature, so this runs on a build of a copy with the one `create:feature`
* writes (support/generated-feature.ts).
*/
let featureBuild: FixtureBuilds<"a"> | undefined;
strictTest.describe("with a feature installed", () => {
strictTest.beforeAll(async () => {
strictTest.setTimeout(300_000);
featureBuild = await prepareFixtureBuilds("preload-use", [
{ name: "a", edit: addGeneratedFeature },
]);
});
strictTest.afterAll(async () => {
await removeFixtureBuilds(featureBuild);
});
strictTest(
"passes a route whose deepest preloaded chunk is reached after WebKit's check",
async ({ page }) => {
if (!featureBuild) throw new Error("The feature build was not prepared.");
const manifest = await buildManifest(featureBuild.dist.a);
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();
const server = await startBuildServer({ a: featureBuild.dist.a, b: featureBuild.dist.a });
try {
await page.goto(`${server.origin}/`);
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 });
} finally {
await server.close();
}
},
);
});
/**
* 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"));
},
);