From 95e7d1107c5e85edfc3ce039b2b888f261e16a8f Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sun, 1 Nov 2026 22:12:59 +0900 Subject: [PATCH] =?UTF-8?q?test(vitest):=20skip=C2=B7todo=C2=B7only?= =?UTF-8?q?=EA=B0=80=20=EB=82=A8=EC=9D=80=20=EC=8B=A4=ED=96=89=EC=9D=84=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=EC=8B=9C=ED=82=A8=EB=8B=A4=20(10-04)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: Vitest는 skip·todo 테스트를 통과로 센다. `it.skip`이나 `it.todo`가 든 파일은 CI=true에서도 종료 코드 0으로 끝난다. `context.skip()`도 같다. `.only`는 Vitest 기본값 때문에 CI에서만 막히고, 로컬에서는 같은 파일의 나머지 테스트를 조용히 건너뛴다. check:test-evidence의 소스 검사가 quarantine 주석 없는 `.skip(`·`.only(` 글자를 잡지만, `it.todo`, `context.skip()`, `skipIf`는 보지 못한다. 고른 방법: - vitest.config.ts에 allowOnly: false를 둬 `.only`를 어디서나 막는다. - scripts/lib/no-skipped-tests-reporter.ts reporter가 실행 끝에 state가 skipped인 테스트를 찾으면 이름을 출력하고 exitCode를 1로 둔다. -t, 태그 필터, file:line처럼 호출자가 일부러 좁힌 실행은 예외다. 게이트는 실행을 좁히지 않는다. - reporter는 scripts/run-vitest.ts가 붙인다. CLI --reporter는 config의 reporters를 대체하므로 config에 두면 package script에서 빠진다. run-vitest.ts는 package script와 scale tier가 모두 거치는 입구다. 호출자가 reporter를 하나도 주지 않으면 default를 함께 붙인다. 버린 대안: - 리뷰 최소 개선안의 junit `skipped > 0` 검사: Vitest junit은 mode가 skip·todo인 테스트만 로 적고 `context.skip()`은 통과처럼 적는다. junit을 쓰지 않는 실행(scale tier, http-scenario-catalog, 로컬 단일 파일)은 아예 보지 못한다. - tests/setup.ts의 afterAll 검사: 모든 테스트가 skip인 파일은 hook이 돌지 않아 잡지 못한다. - eslint-plugin-vitest no-disabled-tests: 새 의존성이 생기고 `context.skip()`을 못 잡는다. 위험·한계: - run-vitest.ts를 거치지 않는 직접 `vitest` 실행에는 reporter가 붙지 않는다. allowOnly는 config라서 그때도 적용된다. - 호출자가 reporter를 주지 않으면 default reporter를 고정해서 붙인다. Vitest가 AI 에이전트 환경에서 고르는 agent reporter 대신 default가 나온다. - 본문 없는 `describe.todo`처럼 테스트가 없는 묶음은 세지 않는다. - check:test-evidence는 quarantine 주석이 있는 `.skip(`을 허용하지만 이 reporter는 Vitest에서 그것도 실패시킨다. 지금 quarantine은 Playwright spec에만 있어 충돌하는 테스트는 없다. 전략 문서 16.5절에 "quarantine은 Vitest 테스트를 통과시키지 않는다"고 적었다. Vitest에도 quarantine이 필요해지면 허용 목록을 따로 둬야 한다. - 지금 저장소의 Vitest 테스트에는 skip·todo·only가 없다. 검증: - RED(P-B 재현, run-vitest.ts로 skip+todo, only, context.skip() 파일을 CI 없음/CI=true로): 수정 전 6경우 중 5경우 exit 0("1 passed | 1 skipped | 1 todo"). only+CI=true만 exit 1. - 새 tests/unit/no-skipped-tests-reporter.test.ts는 수정 전 "Cannot find module ../../scripts/lib/no-skipped-tests-reporter.ts"로 실패. - GREEN: 같은 6경우 모두 exit 1. skip·todo·context.skip()은 "N test(s) did not run" 목록, only는 "Unexpected .only modifier". junit reporter를 준 실행도 exit 1. -t로 좁힌 실행과 file:line 실행은 exit 0. 새 테스트 11 passed. - 빠른 계층 전체(unit·component·integration·contract·runtime-schema· platform·architecture·features·release·scripts·recipes, pnpm 설치를 하는 fixture-node-modules.test.ts 제외, maxWorkers 4): 3,228 passed, 1 failed. 실패는 provider-guardian-transaction의 fs.watch ENOSPC(호스트 inotify 한도)이고 reporter 없이 vitest를 직접 돌려도 같다. skip 목록 출력은 없었다. - tsc -p tsconfig.node.json, tsconfig.test.json, eslint(바꾼 4파일), check-ci-contract, generate-ci-workflow --check, verify-documentation-readiness(PASS_SCOPED) 통과. Refs: docs/reviews/2026-11-01-maturity-review/10-tests-fast-layers.md 10-04 --- .../frontend-platform-testing-strategy.md | 6 ++ scripts/lib/no-skipped-tests-reporter.ts | 78 +++++++++++++++ scripts/run-vitest.ts | 14 ++- tests/unit/no-skipped-tests-reporter.test.ts | 98 +++++++++++++++++++ vitest.config.ts | 4 + 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 scripts/lib/no-skipped-tests-reporter.ts create mode 100644 tests/unit/no-skipped-tests-reporter.test.ts 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,