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"), ); } }