Playwright가 재시도하지 않아 실패가 흔들림인지 회귀인지 run에 남지
않는다. CI=true에서만 한 번 더 돌고, 재시도에서 통과해도 실패로
끝낸다. 설정 helper로만 주고 검사기가 설정 객체의 key를 거절한다.
failOnFlakyTests만으로는 재시도에서만 test.fail()에 닿은 시험의 첫
실패가 통과로 바뀐다. helper의 reporter가 실패한 시도를 따로 판정하고
다른 보고에 없는 원인을 색 코드 없이 찍는다. e2e 공유 fixture 빌드는 재시도에서 앞
시도의 실패 기록을 다시 쓰지 않는다(카드 C9). package script와 게이트
args는 029 파서와 이어 쓴 script 때문에 버렸다. CI 판별은 러너 정책이라
CI env를 읽는다.
위험·한계: 많이 깨진 run은 브라우저 게이트가 최대 약 두 배 걸린다
(추정). 명령줄 --reporter는 이 판정 reporter를 뺀다. JUnit에서 실패한
시험의 시간과 failure/error 분류는 첫 시도 기준이 된다. 검사기는 spec
안의 describe.configure({ retries })를 보지 않는다. 빌드가 실패 기록
없이 끊기면(worker 종료, hook 시간 초과) 재시도도 기다리다 실패한다.
검증: main으로 옮기기 전 트리 c8df70ab에서 test:unit 3632개, types,
lint, FE-GATE-029, verify:documentation rc=0, 변이 30개 검출. desktop
실제 브라우저: 일시 빌드 실패는 6 flaky, 계속 실패는 3 failed, 숨은
실패는 rc=1과 원인 출력. 트리 3dea9c06에서 숨은 expect 실패의 색 코드
시험이 고치기 전 1개 실패, 고친 뒤 35개 통과.
Refs: CI-FE-0001
83 lines
3.5 KiB
TypeScript
83 lines
3.5 KiB
TypeScript
import { fileURLToPath } from "node:url";
|
||
import { stripVTControlCharacters } from "node:util";
|
||
|
||
import type { FullConfig, FullResult, Reporter, TestCase, TestError, TestResult } from "@playwright/test/reporter";
|
||
|
||
/**
|
||
* FLAKY-3 (docs/work/reference/decisions.md "CI 계획 D-3"). In CI a failed
|
||
* Playwright test runs once more only to tell a flaky test from a broken one:
|
||
* a test that passes on its retry is reported flaky and the run still fails.
|
||
* Outside CI nothing retries. Configs spread this and name the reporter below;
|
||
* they set neither option themselves (scripts/check-test-evidence.ts).
|
||
*/
|
||
export function flakyJudgement(environment: NodeJS.ProcessEnv = process.env) {
|
||
const ci = environment["CI"] === "true";
|
||
return Object.freeze({ retries: ci ? 1 : 0, failOnFlakyTests: ci });
|
||
}
|
||
|
||
/** This module as an entry of a config's `reporter` list. */
|
||
export const flakyJudgementReporter = [fileURLToPath(import.meta.url)] as const;
|
||
|
||
type FailedAttempt = Readonly<{ test: TestCase; result: TestResult }>;
|
||
|
||
/**
|
||
* Whether the list reporter leaves out why this attempt failed. It prints the
|
||
* errors of a test that ends flaky or failed, and "Expected to fail, but
|
||
* passed." only while the last attempt still expects a failure.
|
||
*/
|
||
function leftOutOfTheReport({ test, result }: FailedAttempt): boolean {
|
||
return test.outcome() === "expected" || (result.status === "passed" && test.expectedStatus !== "failed");
|
||
}
|
||
|
||
function errorText({ message, value, stack, cause }: TestError): string {
|
||
const text = [stack ?? message ?? value, cause && `[cause]: ${errorText(cause)}`].filter(Boolean).join("\n");
|
||
return stripVTControlCharacters(text);
|
||
}
|
||
|
||
/** One attempt, headed like the list reporter's failures so ci:gate quotes it (ci-gate-failure.ts). */
|
||
function failureBlock({ test, result }: FailedAttempt, index: number): string {
|
||
const [, project, , ...titles] = test.titlePath();
|
||
const { file, line, column } = test.location;
|
||
const causes = result.status === "passed" ? ["Expected to fail, but passed."] : result.errors.map(errorText);
|
||
return [
|
||
` ${index + 1}) ${project ? `[${project}] › ` : ""}${file}:${line}:${column} › ${titles.join(" › ")}`,
|
||
" failed an attempt before the retry changed what it expects; the run fails",
|
||
...causes,
|
||
"",
|
||
].join("\n");
|
||
}
|
||
|
||
/**
|
||
* What failOnFlakyTests misses. Playwright 1.62 judges every attempt of a test
|
||
* by the expected status of the last one, so when only the retry reaches
|
||
* test.fail() (a fixture failed before the body called it), the first failure
|
||
* becomes an expected one and the run passes. This judges each attempt when it
|
||
* ends: a failed attempt fails the run, as it does without a retry, and its
|
||
* error is printed here when no other report has it.
|
||
*/
|
||
export default class FlakyJudgementReporter implements Reporter {
|
||
#judging = false;
|
||
readonly #failed: FailedAttempt[] = [];
|
||
|
||
onBegin(config: FullConfig): void {
|
||
this.#judging = config.failOnFlakyTests;
|
||
}
|
||
|
||
onTestEnd(test: TestCase, result: TestResult): void {
|
||
if (result.status !== "skipped" && result.status !== test.expectedStatus) this.#failed.push({ test, result });
|
||
}
|
||
|
||
async onEnd(result: FullResult): Promise<{ status: "failed" } | undefined> {
|
||
if (!this.#judging || this.#failed.length === 0) return undefined;
|
||
for (const [index, attempt] of this.#failed.filter(leftOutOfTheReport).entries()) {
|
||
process.stderr.write(failureBlock(attempt, index));
|
||
}
|
||
return result.status === "passed" ? { status: "failed" } : undefined;
|
||
}
|
||
|
||
// The report stays the list reporter's.
|
||
printsToStdio(): boolean {
|
||
return false;
|
||
}
|
||
}
|