Run the test suite against TypeScript sources behind an uncheatable coverage gate

Merged
UN
4mo ago
+433-31

Why

Coverage in this package was never measured. Tests imported the compiled output from dist/, so the only numbers obtainable pointed at generated JavaScript offsets rather than lines an author can act on, and no threshold was wired into any gate — coverage could regress silently on every merge.

What changed

Tests run against the TypeScript sources. Node has stripped types without a flag since 22.18.0, which is the floor this package already declares in engines, so this needs no new tooling and no build step for the test run. Tests import source modules directly (../index.ts, ../projects.ts), which puts the coverage report on real source lines.

rewriteRelativeImportExtensions keeps the published artifact unchanged. Source files now import siblings as ./x.ts; tsc still emits ./x.js into dist/, so the shipped module graph is identical.

scripts/coverage-gate.ts is the gate. It enforces per-dimension thresholds and reconciles the reported file list against a walk of the source tree. That second check is why this is a script rather than a bare --test-coverage-lines flag:

Node omits files that never load from its coverage report entirely, rather than reporting them at zero. A threshold flag alone is therefore satisfiable by narrowing what the suite imports — a package can show 100% while an entire module goes unexercised.

Negative control, run before opening this PR: adding an untested file and listing it in coverageGate.sources makes the gate exit 1 and name the file. The check is not vacuous.

Thresholds ratchet. Pinned in package.json under coverageGate at the measured baseline so the number can only move up, and visible in review as a deliberate diff whenever it changes. release:check runs the gate in place of a bare test run, and CI runs it on a Node 22 + 26 matrix.

Measured baseline

Both source files, reported against TypeScript lines:

fileline %branch %funcs %
projects.ts97.0081.41100.00
index.ts73.0878.3073.03
all files76.1478.9375.74

index.ts is the target of the follow-up work to reach the mandated 100.

Verification

  • npm run check — passes
  • npm run coverage — 172 tests, 0 failures, both source files present in the report
  • npm run release:check — passes end to end (typecheck, build, coverage gate, prod audit, pack dry-run, changelog check)
  • Negative control — untested source file ⇒ exit 1 naming the file

The Node 22 matrix leg is unobserved locally (this host runs 26.5.0) and is confirmed by this PR's CI.

pm items

  • pm-github-uv1a — Enforce a real coverage gate by running tests against TypeScript sources

Summary by Sourcery

Enforce a source-level coverage gate by running the Node test suite directly against TypeScript entrypoints and wiring coverage thresholds into local and CI workflows.

New Features:

  • Introduce a configurable coverage gate script that runs tests with Node’s built‑in coverage and validates thresholds plus source file presence.
  • Add a dedicated coverage npm script that generates an lcov report and is used as the primary test check in release validation.

Enhancements:

  • Switch tests and helpers to import TypeScript sources instead of compiled dist outputs so coverage maps to real source lines.
  • Enable TypeScript options to rewrite relative import extensions and allow importing .ts modules, keeping the emitted JavaScript graph unchanged.
  • Record coverage gate configuration in package.json, including source roots, test patterns, thresholds, and ignores.

Build:

  • Update npm scripts so release:check runs the coverage gate instead of a plain test run.

CI:

  • Run the coverage-gated test suite in CI against a Node 22 and 26 version matrix instead of a single Node version.

Tests:

  • Point all test entrypoints and helpers at the TypeScript modules and test files directly rather than the built dist outputs.

Chores:

  • Add project management artifacts documenting the coverage gate feature.

Summary by cubic

Run tests directly against TypeScript sources behind a strict coverage gate that enforces thresholds and checks file presence. CI runs the gate on Node 22 and 26; the published JS output is unchanged.

  • Bug Fixes
    • Gate enumerates sources for --test-coverage-include; rejects missing, non‑TS, or .d.ts entries; normalizes lcov SF: paths to repo‑relative POSIX.
    • Deletes stale coverage/lcov.info before each run; exits with runner status before reading the report; pins TZ=UTC for stable coverage.
    • Validates coverageGate.ignore by verifying emitted JS is type‑only (strips block and line comments); resolves effective tsconfig via tsc --showConfig and fails closed if it cannot be resolved.
    • Chains build:test into npm run coverage; tsconfig.test.json set to no‑emit with repo rootDir; thresholds in package.json set to lines 76, branches 78, functions 75; npm test runs node --test on test/*.test.ts.