diff --git a/docs/testing/frontend-platform-testing-strategy.md b/docs/testing/frontend-platform-testing-strategy.md index 0e53b1df..f9cd37f4 100644 --- a/docs/testing/frontend-platform-testing-strategy.md +++ b/docs/testing/frontend-platform-testing-strategy.md @@ -1154,6 +1154,12 @@ signed manual accessibility review는 release gate `FE-GATE-028`이다 ### 16.5 Flaky policy - merge를 통과시키기 위한 임의 `test.skip` 금지 + - Vitest: `scripts/run-vitest.ts`가 모든 실행에 붙이는 + `scripts/lib/no-skipped-tests-reporter.ts`가 skip·todo·`context.skip()`이 + 남은 실행을 실패시킨다. `-t`나 `file:line`으로 일부러 좁힌 실행은 예외다. + `.only`는 `vitest.config.ts`의 `allowOnly: false`로 로컬에서도 실패한다. + 아래 quarantine 주석은 Vitest 테스트를 통과시키지 않는다. 지금 + quarantine은 Playwright spec에만 있다. - quarantine에는 owner, defect ID, 영향 route, 만료일 필요 - retry 후 pass도 성공으로만 집계하지 않고 flaky report에 기록 - 동일 test가 반복되면 clock/network/focus/shared-state 원인을 제거 diff --git a/scripts/lib/no-skipped-tests-reporter.ts b/scripts/lib/no-skipped-tests-reporter.ts new file mode 100644 index 00000000..a57bb92e --- /dev/null +++ b/scripts/lib/no-skipped-tests-reporter.ts @@ -0,0 +1,78 @@ +import type { + Reporter, + TestModule, + TestSpecification, + Vitest, +} from "vitest/node"; + +/** + * 10-04. Vitest counts a skipped or todo test as a pass, so an `it.skip` left + * behind merges with exit code 0 and the rule that test kept quietly goes + * away. scripts/run-vitest.ts adds this reporter to every run. It fails the + * run when a test did not run (`skip`, `todo`, `context.skip()`). + * + * A run the caller narrowed on purpose (`-t`, a tag filter, `file:line`) + * leaves the rest out by design and is exempt. No gate narrows its run. + * `.only` is refused separately by `allowOnly: false` in vitest.config.ts. + */ + +type RunFilters = Readonly<{ + testNamePattern?: RegExp | undefined; + tagsFilter?: readonly string[] | undefined; +}>; + +export function isNarrowedRun( + config: RunFilters, + specifications: readonly TestSpecification[], +): boolean { + return ( + config.testNamePattern !== undefined || + (config.tagsFilter?.length ?? 0) > 0 || + specifications.some( + (specification) => + specification.testNamePattern !== undefined || + (specification.testLines?.length ?? 0) > 0 || + (specification.testIds?.length ?? 0) > 0 || + (specification.testTagsFilter?.length ?? 0) > 0, + ) + ); +} + +export function unrequestedSkips( + testModules: readonly TestModule[], +): string[] { + return testModules.flatMap((testModule) => + [...testModule.children.allTests()] + .filter((test) => test.result().state === "skipped") + .map((test) => `${testModule.relativeModuleId} > ${test.fullName}`), + ); +} + +export default class NoSkippedTestsReporter implements Reporter { + #vitest: Vitest | undefined; + #narrowed = false; + + onInit(vitest: Vitest): void { + this.#vitest = vitest; + } + + onTestRunStart(specifications: readonly TestSpecification[]): void { + this.#narrowed = isNarrowedRun(this.#vitest?.config ?? {}, specifications); + } + + onTestRunEnd(testModules: readonly TestModule[]): void { + if (this.#narrowed) return; + const skipped = unrequestedSkips(testModules); + if (skipped.length === 0) return; + process.exitCode = 1; + process.stderr.write( + [ + "", + `${skipped.length} test(s) did not run. A skipped or todo test fails the run (10-04):`, + ...skipped.map((name) => ` - ${name}`), + "Make the test pass or delete it. To run part of a file on purpose, narrow the run with -t or file:line.", + "", + ].join("\n"), + ); + } +} diff --git a/scripts/run-vitest.ts b/scripts/run-vitest.ts index 62a9ca93..23403b67 100644 --- a/scripts/run-vitest.ts +++ b/scripts/run-vitest.ts @@ -59,9 +59,21 @@ if (generated.status !== 0) { process.exit(generated.status ?? 1); } +// 10-04. Every run fails when a test was skipped or left as todo. A CLI +// --reporter replaces the config's reporters, so the reporter is added here, +// where every package script and scale tier starts Vitest. When the caller +// names no reporter, the default one is kept next to it. +const callerNamesReporter = process.argv + .slice(2) + .some((argument) => argument === "--reporter" || argument.startsWith("--reporter=")); +const reporterArguments = [ + ...(callerNamesReporter ? [] : ["--reporter=default"]), + `--reporter=${fileURLToPath(new URL("./lib/no-skipped-tests-reporter.ts", import.meta.url))}`, +]; + const result = spawnSync( process.execPath, - [vitestEntry, ...process.argv.slice(2)], + [vitestEntry, ...process.argv.slice(2), ...reporterArguments], { stdio: "inherit", env: testEnvironment, diff --git a/tests/unit/no-skipped-tests-reporter.test.ts b/tests/unit/no-skipped-tests-reporter.test.ts new file mode 100644 index 00000000..044cbd82 --- /dev/null +++ b/tests/unit/no-skipped-tests-reporter.test.ts @@ -0,0 +1,98 @@ +import { readFileSync } from "node:fs"; + +import { describe, expect, it } from "vitest"; + +import vitestConfig from "../../vitest.config.ts"; +import { + isNarrowedRun, + unrequestedSkips, +} from "../../scripts/lib/no-skipped-tests-reporter.ts"; + +// 10-04. A skipped or todo test must fail the run, unless the caller asked +// for a narrower run on purpose. + +function testModule( + relativeModuleId: string, + tests: readonly (readonly [string, string])[], +) { + return { + relativeModuleId, + children: { + *allTests() { + for (const [fullName, state] of tests) { + yield { fullName, result: () => ({ state }) }; + } + }, + }, + }; +} + +describe("unrequestedSkips", () => { + it("names every test that did not run, whatever skipped it", () => { + expect( + unrequestedSkips([ + testModule("tests/unit/a.test.ts", [ + ["runs", "passed"], + ["it.skip", "skipped"], + ["it.todo", "skipped"], + ]), + testModule("tests/unit/b.test.ts", [ + ["fails", "failed"], + ["context.skip()", "skipped"], + ]), + ] as never), + ).toEqual([ + "tests/unit/a.test.ts > it.skip", + "tests/unit/a.test.ts > it.todo", + "tests/unit/b.test.ts > context.skip()", + ]); + }); + + it("is empty when every test ran", () => { + expect( + unrequestedSkips([ + testModule("tests/unit/a.test.ts", [ + ["runs", "passed"], + ["fails", "failed"], + ]), + ] as never), + ).toEqual([]); + }); +}); + +describe("isNarrowedRun", () => { + const whole = { testNamePattern: undefined, tagsFilter: undefined }; + const spec = { + testLines: undefined, + testIds: undefined, + testNamePattern: undefined, + testTagsFilter: undefined, + }; + + it("treats a run of whole files as not narrowed", () => { + expect(isNarrowedRun(whole, [spec, spec] as never)).toBe(false); + expect(isNarrowedRun(whole, [] as never)).toBe(false); + }); + + it.each([ + ["-t ", { ...whole, testNamePattern: /name/u }, spec], + ["--tags-filter", { ...whole, tagsFilter: ["slow"] }, spec], + ["file:line", whole, { ...spec, testLines: [12] }], + ["test ids", whole, { ...spec, testIds: ["abc_0"] }], + ["a per-file name pattern", whole, { ...spec, testNamePattern: /x/u }], + ["a per-file tag filter", whole, { ...spec, testTagsFilter: ["a"] }], + ])("treats %s as a run narrowed on purpose", (_label, config, narrowed) => { + expect(isNarrowedRun(config, [spec, narrowed] as never)).toBe(true); + }); +}); + +describe("wiring", () => { + it("refuses .only locally as well as in CI", () => { + expect(vitestConfig.test?.allowOnly).toBe(false); + }); + + it("adds the reporter to every run that goes through run-vitest.ts", () => { + const runner = readFileSync("scripts/run-vitest.ts", "utf8"); + expect(runner).toContain("./lib/no-skipped-tests-reporter.ts"); + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index d89eec90..3a4aac84 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -25,6 +25,10 @@ const systemTestsRequested = process.argv.some((argument) => export default defineConfig({ test: { globals: false, + // 10-04. Vitest refuses `.only` only when CI is set; locally it quietly + // drops the rest of the file. Refuse it everywhere. Skipped and todo + // tests are failed by scripts/lib/no-skipped-tests-reporter.ts. + allowOnly: false, setupFiles: ["./tests/setup.ts"], restoreMocks: true, clearMocks: true,