Files
clean-architecture-frontend…/tests/support/browser/strict-browser-test.ts
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

101 lines
3.5 KiB
TypeScript

import {
expect,
test as base,
type ConsoleMessage,
type Page,
} from "@playwright/test";
import { trackPreloadUse } from "./preload-use.ts";
/**
* 1103/11-09. WebKit's guess, three seconds after the load event, that a
* preload went unused. It fired for chunks the page ran a moment later and
* missed preloads the page never ran once the test had ended. The fixture
* checks preload use itself, in every engine (`preloadunused:` below).
*/
const WEBKIT_UNUSED_PRELOAD_GUESS =
/^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 wasn't preloaded for nothing\.$/u;
function ignoredConsole(message: ConsoleMessage) {
return (
message.type() === "warning" &&
(message.text().includes("NO_COLOR") ||
WEBKIT_UNUSED_PRELOAD_GUESS.test(message.text()))
);
}
/**
* 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
* `console:…`, `pageerror:…`, `requestfailed:…` or `preloadunused:…` line
* (for example the browser's log line for a mocked 4xx, or the chunks
* preloaded with a chunk the test removed). Everything else still fails. One
* pattern, not an array: `test.use` reads an array as a `[value, options]`
* 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 }],
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) => {
if (
["error", "warning"].includes(message.type()) &&
!ignoredConsole(message)
) {
failures.push(`console:${message.type()}:${message.text()}`);
}
};
const onPageError = (error: Error) => {
failures.push(`pageerror:${error.name}`);
};
const onRequestFailed = (request: import("@playwright/test").Request) => {
failures.push(
`requestfailed:${request.method()}:${new URL(request.url()).pathname}:${request.failure()?.errorText ?? "unknown"}`,
);
};
page.on("console", onConsole);
page.on("pageerror", onPageError);
page.on("requestfailed", onRequestFailed);
await use(page);
failures.push(...(await preloads.check()).failures);
page.off("console", onConsole);
page.off("pageerror", onPageError);
page.off("requestfailed", onRequestFailed);
browserFailureVerdict.judge(failures);
},
});
export { expect };
export type { Page };