module-test architecture improvement plan #
This plan deepens @monochromatic-dev/module-test by concentrating failure reporting in a runner-owned Module.
It records decisions from the architecture review.
It keeps the package focused on suite and assertion primitives.
Decisions already made #
module-testremains a suite primitive. File discovery, per-file process isolation, and aggregate execution belong to the rootmisetest:unittemplate. This is recorded indoc/adr/0001-module-test-suite-primitive.md.- Scheduling stays numeric through
describe({ concurrency }). Node method replacement throughctx.sinonhas attempt-owned isolation under issue #481. Other shared-state meaning remains caller-owned documentation and test structure. - Matcher declarations stay explicit across
MatcherSet, matcher builders, key lists, wrappers, docs, and tests. - Existing negative-path primitives stay:
fails,.rejects, and.resolvesremain the compatibility surface. - The accepted deepening is a runner-owned failure reporter Module.
Context-owned method replacement #
The accepted #481 implementation keeps the existing test syntax and numeric scheduling API.
it-attempt.ts owns a fresh sandbox generation for each body attempt,
including repeats and timed-out bodies.
It irreversibly completes ownership before cleanup.
sandbox.ts exposes the test capability and retains runner-owned restoration separately.
On 2026-10-04 this file moved to package/module/test-sandbox/src/sandbox.ts;
it-attempt.ts remains in the runner.
The operation adapter delegates actual fake creation to Sinon on private method facades.
A realm-shared registry selects those facades using the existing Node async execution context.
The target's getter returns the current reader's fake or its original method;
it does not dispatch to another fake at invocation time.
Only own, configurable, writable data methods receive contextual isolation. Other mutation families retain ordinary semantics with collision preflight and lifetime guards. Browsers retain ordinary Sinon method replacement rather than a claimed browser async-context implementation.
The supported behavior and deliberate reflection/assignment boundaries are documented in
README.md.
This addition does not implement or supersede the failure-reporter work in this plan.
Accepted deepening: failure reporter Module #
Files involved #
package/module/test/src/it.tspackage/module/test/src/describe.tspackage/module/test/src/format-error.ts. Moved on 2026-10-04 topackage/module/test-diagnostic/src/format-error.ts.package/module/test/src/assertion-source.ts. Moved on 2026-10-04 topackage/module/test-diagnostic/src/assertion-source.ts.package/module/test/src/harness-frames.ts. Moved on 2026-10-04 topackage/module/test-diagnostic/src/harness-frames.ts.package/module/test/src/index.tspackage/module/test/src/*.unit.test.tspackage/module/test/README.md
Problem #
format-error.ts is already deep for formatting,
but the runner paths are still shallow.
it.ts and describe.ts each decide when to log,
how to summarize,
what synthetic cause to create,
and what to throw.
Every branch must remember both diagnostics and throw-shape policy.
Deletion test:
deleting formatFailure would push stack and cause formatting back into runner code,
but deleting the runner failure branches would still leave failure policy scattered.
The deeper Module should own the whole failure outcome:
log useful diagnostics,
then preserve the correct throw contract.
Target shape #
Add an exported,
underscored,
testable internal Module,
likely src/failure-reporter.ts:
// package/module/test/src/failure-reporter.ts
export async function _reportItFailure(input: _ReportItFailureInput): Promise<never>;
export async function _reportDescribeFailure(input: _ReportDescribeFailureInput): Promise<never>;
export type _ReportItFailureInput = ...;
export type _ReportDescribeFailureInput = ...;
The reporter functions throw inside the reporter after logging. The runner should not construct the final error or remember to throw it.
The reporter owns these failure outcomes:
- unexpected
itthrow or timeout, wrapping asError(name, { cause }) failstest that unexpectedly passed, with a synthetic cause created by the reporter- assertion-count mismatch, with a synthetic cause created by the reporter
hasAssertions()mismatch, with a synthetic cause created by the reporter- suite child failure,
preserving current single-cause versus
AggregateErrorbehavior - empty-name suite child failure, preserving direct cause rethrow
- suite timeout, preserving current raw timeout rethrow
format-error.ts,
assertion-source.ts,
and harness-frames.ts remain implementation helpers behind the reporter seam.
Their current property tests stay useful,
but runner-level reporter tests become the main test surface for failure behavior.
Implementation plan #
- Add
src/failure-reporter.tswith_reportItFailure,_reportDescribeFailure, and their input types. - Move log-and-throw policy from
runItinto_reportItFailure. - Move suite failure and timeout log-and-throw policy from
runDescribeinto_reportDescribeFailure. - Keep
formatFailurefocused on rendering. Do not make callers format summaries directly in runner branches. - Export the underscored functions and input types from
src/index.tsfor package self-tests. - Add focused reporter unit tests that use a logger adapter capturing
errorcalls. - Keep integration tests through
itanddescribeto verify the public suite Interface still behaves the same. - Update
README.mdonly where it documents failure output or internal self-test conventions.
Test plan #
- Reporter unit tests:
- assert each reporter variant logs a
FAILline with formatted cause detail - assert each reporter variant rejects with the current throw shape
- assert synthetic causes are created only by reporter-owned synthetic variants
- assert each reporter variant logs a
- Runner integration tests:
itunexpected throw still rejects withError(testName, { cause })fails: trueunexpected pass still rejects withError(testName, { cause })- assertion-count failures still reject with
Error(testName, { cause }) - non-empty
describestill wraps child failures with suite name - empty-name
describestill rethrows the child cause directly - suite timeout still rethrows the raw timeout error
- Formatting property tests remain in
format-error.property.unit.test.ts. - Verification command:
mise run //package/module/test:buildAndTest.
Non-goals #
- Do not add a file-level runner to
module-test. - Do not replace numeric
concurrencywith shared-state declarations. - Do not introduce a metadata-driven matcher catalog.
- Do not replace
fails,.rejects, or.resolveswith a stricter expected-outcome Interface in this plan. - Do not change public
describe,it, orexpectbehavior.
Acceptance criteria #
- Failure reporting has one locality point in the reporter Module.
it.tsanddescribe.tsno longer construct synthetic failure causes or format failure logs inline.- The existing throw contracts are preserved.
- The log stream alone remains sufficient for diagnosis.
mise run //package/module/test:buildAndTestpasses.