Problem statement: inline error diagnostics for @monochromatic-dev/module-test #
Context #
@monochromatic-dev/module-test is a self-hosted test harness.
Tests are defined via it({ name, fn }) and grouped via describe({ name, children }).
The harness logs pass/fail status through a tagged logger and propagates failures via throw new Error(name, { cause }).
Current behavior (verified) #
When a test fails,
the harness emits only a summary line at error level.
Full error content (message,
stack trace,
.cause chain,
AggregateError.errors) is not logged by the harness.
In src/it.ts:216-219:
if (threw) {
l.error(`FAIL${runLabel} (${durationMs.toFixed(0,)}ms)`,);
throw new Error(name, { cause: caughtError, },);
}
The l.error(...) call outputs only the test name and duration.
The Error object thrown here carries the full cause chain,
but that chain is only visible when the rejection propagates uncaught to the runtime and the runtime's unhandled-rejection printer dumps it.
Similarly,
in src/describe.ts:297-304,
suite-level failures emit only:
l.error(`FAIL${runLabel} (${durationMs.toFixed(0,)}ms)`,);
throw new Error(name, { cause, },);
Verified consequences #
-
**Real failures show diagnostics only via the runtime printer. **
Running a real failure produces harness log lines with no message content, followed by Bun's cause-chain dump at the bottom:
[error] [real-failure-suite] [will-throw] FAIL (0ms) [error] [real-failure-suite] FAIL (2ms) error: real-failure-suite ← Bun, not the harness error: will-throw ← Bun, not the harness error: something went wrong ← Bun, not the harness -
**Caught rejections show no diagnostics at all. **
Self-tests catch rejections to assert on error shape (
await expect(rejection).rejects.toHaveProperty('cause', original)). Since the rejection never reaches the runtime, the full error content never appears anywhere in output. Self-test output showsFAIL (0ms)lines with adjacent silence. -
**
fails: truetests have a similar gap. **When a test marked
fails: trueunexpectedly passes, the harness emits:FAIL: expected to throw but passed (0ms)But the generated synthetic error (
new Error('Expected test to throw but it passed')) is thrown, not logged inline.
Problem #
The most informative diagnostic output is the least guaranteed to appear. It depends on:
- The rejection propagating uncaught through every parent layer.
- The runtime (Bun) having a competent unhandled-rejection printer.
If a consumer wraps await describe(...) in a custom reporter that catches the rejection,
or runs in an environment without Bun's rich error printer,
the log stream contains FAIL entries with zero diagnostic context.
Desired behavior #
The harness should emit full error content inline,
at error level,
adjacent to the FAIL summary line,
before re-throwing.
The log stream alone should be sufficient to diagnose failures without depending on the runtime printer.
Example of the desired output for a real failure:
[error] [suite] [test] FAIL (Nms)
[error] [suite] [test] Error: something went wrong
[error] [suite] [test] at fn (/path/to/test.ts:9:19)
[error] [suite] [test] at runFnOnce (...)
[error] [suite] FAIL (Nms)
The harness should still re-throw after logging,
preserving the existing Error(name, { cause }) contract for programmatic consumers.
Acceptance criteria #
-
runItinsrc/it.tslogs the full caught error before re-throwing. This includes:- The primary error message and stack.
- The
.causechain (recursive walk). - Expansion of
AggregateError.errorswhen present.
-
Assertion-count failures are also logged inline before throwing.
-
The logger tag chain for inline error lines remains consistent with the
FAILsummary line (same[suite] [test]prefix). -
Existing behavior is preserved: the harness still throws the same errors with the same
.causechains, anddescribestill wraps results the same way. This change is additive (logging) only. -
Self-tests should continue to pass. The added inline logs will cause self-test output to contain error diagnostics where there was previously silence; this is expected and acceptable.
Open design decisions #
-
**Double-print tradeoff. ** Currently Bun prints the full cause chain when the top-level
describe(...)rejects. Adding inline logging means the same error content may appear twice (once inline via the logger, once at the bottom via Bun). Options:- Accept the duplication as a short-term state.
- Add a top-level harness helper (e.g.
run(descriptor)) that catches, logs inline, setsprocess.exitCode = 1, and swallows the rejection to suppress the runtime dump. - Decide this is out of scope for the initial fix and document as a known side effect.
-
**
fails: truesuppression. ** For tests withfails: true, a deliberatethrowis expected behavior and already logsPASSatdebug. Should inline error dumping be suppressed forfails: truetests that throw? If not, every expected failure will spam error-level logs. The current behavior of logging nothing for expected throws may be preferable.Recommendation: suppress inline error logging when
fails !== falseand the test threw (expected behavior). Log inline errors only for:- Unexpected throws (
fails: falseand test threw). - Assertion-count mismatches.
- Unexpected passes (
fails: trueand test passed).
- Unexpected throws (
-
**Formatting helper. ** A
formatErrorDeephelper is needed. It should:- Print
error.message. - Print
error.stackminus the message line (to avoid duplication). - Walk
.causerecursively with indentation or prefix markers. - Expand
AggregateError.errorssimilarly. - Handle cross-realm or malformed error objects gracefully (no
.stack, non-string.message, etc.). - Respect the logger's line-oriented output: multi-line strings should be split and each line prefixed with the current tag.
- Print
Non-goals #
- Do not change the public API of
it,describe, orTestDescriptor. - Do not modify
src/describe.tssuite-level wrapping logic except to decide whether suite-level inline dumping is needed (see acceptance criteria: leaf-level only required). - Do not remove the existing
throwbehavior or change theError(name, { cause })shape.
Files to modify #
package/module/test/src/it.ts: add inline error logging in failure paths.package/module/test/src/self.unit.test.ts: if self-test output assertions exist, update expectations to account for new log lines (or verify if any non-build assertions are affected).