Skip to content

Testing and quality ​

CommandWhat it does
bun run testunit tests (Vitest)
bun run test:coveragethe same with v8 coverage; thresholds (90% statements/lines, 85% branches/functions) fail the run
bun run test:perfperformance budgets, scaling and memory checks
bun run benchbenchmarks (vitest bench) for tracking numbers over time
bun run test:mutationmutation testing with Stryker
bun run test:distsmoke test of the built package (after bun run build): subpaths in ESM and CommonJS, one copy of every class
bun run lint / typecheckESLint (+ @stylistic) / tsc --noEmit

Why tests use a custom transform ​

vitest.shared.ts compiles .ts with esbuild.transform() and legacy decorators - the same toolchain tsup uses for the published build. Don't swap in SWC or oxc: class-field and Proxy timing differ subtly between transpilers, and this library's readonly/created logic depends on it.

Performance tests ​

tests/perf/budgets.perf.ts asserts median time per operation against budgets that are 5-8x the numbers measured on a laptop, so a slow CI runner doesn't flake but an accidental O(n²) or a per-call allocation storm trips them. Loosen on a slow machine with PERF_FACTOR=3 bun run test:perf.

What is covered:

  • absolute budgets - create() (10/100 fields, nested order, new Model), read, write, toJSON(), clone(), isTouched(), createFromCollection() of 1000 items, isTouched() / isDirty() of a tracked model (true and 'shallow') with a 2000-row collection, Object.keys() / JSON.stringify() of a model with 10 fields;
  • listeners are cheap - five listeners add < 5x to a write; a class-level listener on a 3-level inheritance chain stays sub-8µs;
  • scaling - 10x more fields costs < 25x per create(), 5x more collection items < 9x, a 50-level self-nested model doesn't overflow the stack, a 10k-element array field stays within budget;
  • memory (needs --expose-gc, set in vitest.perf.config.ts) - dropped models with instance and class listeners are garbage collected; tracked snapshots don't accumulate; an instance with 10 fields costs < 1.5 KB.

Reference numbers on a laptop (ns/op): create() 10 fields ≈ 7k, 100 fields ≈ 74k; write ≈ 270 (plain object: 4); read ≈ 37; toJSON() of 10 fields ≈ 570; isTouched() ≈ 340. A write costs ~65x a plain assignment because every write walks the pipeline (fillable → readonly → validator → events); that is the price of the guarantees, and it is still well under a microsecond.

Mutation testing ​

Coverage says a line ran; mutation testing says a test would notice if it were wrong. Stryker changes the code (flips a condition, drops a statement, swaps an operator) and checks that some test fails. A surviving mutant means a behavior nobody pins down.

bash
bun run test:mutation        # HTML report: reports/mutation/index.html

The score is gated (break: 90) in stryker.config.mjs, and scripts/mutation-gate.mjs also enforces a per-file minimum (overall score is currently ≈ 95%); runs are incremental (reports/stryker-incremental.json), so only changed code is re-mutated. In CI it runs weekly and on demand (quality.yml), not on every PR - it takes minutes.

Packaging and tree-shaking ​

  • tests/treeshake.test.ts bundles every entry point (esbuild, minified) and checks that no unwanted module (containers, scalars, the model) ends up in the bundle and that the size stays within its budget.
  • tests/package-entries.test.ts keeps tsup.config.ts, exports and typesVersions in agreement: there is no root entry and no src/index.ts, internals are not exported, there are no runtime dependencies.
  • bun run test:dist loads the built package by subpath in ESM and CommonJS and checks that instanceof, events and registries work across entry points. CI and publishing run it after bun run build.
  • tests/clone.test.ts checks the library's own deep clone against lodash on a set of structures.