testing/error_coverage.ts

Error reachability coverage tracking.

Tracks which declared error codes are actually exercised in tests. ErrorCoverageCollector records the error responses observed during test runs, each by its body's error code where it is known, then assert_error_coverage compares against each route's error map (resolve_route_error_map) to find uncovered error paths — one per declared code at each status, read from the map's definitions. An observation with a code covers that one path; one recorded with a null code covers every code declared at its status.

view source

Declarations
#

6 declarations

assert_error_coverage
#

testing/error_coverage.ts view source

(collector: ErrorCoverageCollector, route_specs: RouteSpec[], options?: ErrorCoverageOptions | undefined): void import {assert_error_coverage} from '@fuzdev/fuz_app/testing/error_coverage.js';

Assert error coverage meets a minimum threshold.

Computes the ratio of exercised error paths to total declared error paths, each declared code at each status of a route's error map (collector.route_error_maps) one path. An observation with a code covers that path alone; a status-only observation (a null code) covers every declared code at its status (the "any-code" rule).

When min_coverage is 0 (default), logs coverage info without failing. When > 0, fails if coverage is below the threshold.

collector

route_specs

type RouteSpec[]

options?

optional

returns

void

throws

  • AssertionError - if `min_coverage > 0` and the covered/total ratio falls below the threshold — the failure message lists every uncovered route + status (+ code).

CoverageFilterOptions
#

testing/error_coverage.ts view source

CoverageFilterOptions import type {CoverageFilterOptions} from '@fuzdev/fuz_app/testing/error_coverage.js';

Options controlling which routes/statuses are considered for coverage.

ignore_routes?

Routes to skip, in 'METHOD /path' format.

type string[]

ignore_statuses?

HTTP status codes to skip.

type number[]

DEFAULT_INTEGRATION_ERROR_COVERAGE
#

testing/error_coverage.ts view source

0.2 import {DEFAULT_INTEGRATION_ERROR_COVERAGE} from '@fuzdev/fuz_app/testing/error_coverage.js';

Default minimum error coverage threshold for the standard integration and admin test suites. Conservative — not all error paths are exercisable in the composable suites. Consumers should increase as their test suites mature.

ErrorCoverageCollector
#

testing/error_coverage.ts view source

import {ErrorCoverageCollector} from '@fuzdev/fuz_app/testing/error_coverage.js';

Tracks which route × status (and route × status × code) combinations have been exercised in tests.

Use record() to log an observed status with the body's error code (null for none), or assert_and_record() to combine response validation with tracking (reads body.error from the response). After all tests, call uncovered() to find declared error paths never exercised.

An observation with a code covers exactly that (route, status, code): a forbidden_origin 403 leaves a declared credential_type_required 403 uncovered. An observation recorded with a null code — a response that carries none — satisfies the "any-code" rule for its status: every code declared there counts as covered.

route_error_maps

Each route's error map, keyed METHOD path (AppSurfaceSpec.route_error_maps), which the declared paths and the response checks read; without one, a route's own map without middleware.

type ReadonlyMap<string, RouteErrorMap> | null

readonly

constructor

type new (route_error_maps?: ReadonlyMap<string, RouteErrorMap> | null | undefined): ErrorCoverageCollector

route_error_maps?

each route's error map, keyed METHOD path (AppSurfaceSpec.route_error_maps)

type ReadonlyMap<string, RouteErrorMap> | null
optional

observed

Observed keys: "METHOD /spec-path:STATUS:CODE" for an observation with a code, "METHOD /spec-path:STATUS" for one without, which covers every code declared at the status.

type Set<string>

readonly

record

Record an observed error status and its body's error code for a route.

Resolves the concrete request path back to the spec template path (e.g., /api/accounts/abc → /api/accounts/:id). A code covers that code alone; null, for a response that truly carries no code, covers every code declared at the status — so any-code coverage is asked for, never had by leaving the code out.

type (route_specs: RouteSpec[], method: string, path: string, status: number, code: string | null): void

route_specs

type RouteSpec[]

method

type string

path

request path (may be concrete)

type string

status

type number

code

the body's error code, or null when the response has none

type string | null
returns void

mutates

  • this.observed — adds `"METHOD /spec-path:STATUS:CODE"` for a code, `"METHOD /spec-path:STATUS"` for `null`.

assert_and_record

Validate a response against its route spec and record the status.

Wraps assert_response_matches_spec and records the status with the body's error code, read from a clone so the original stream stays usable; a body with no string error records null. Pass code when the body was already consumed.

type (route_specs: RouteSpec[], method: string, path: string, response: Response, code?: string | null | undefined): Promise<void>

route_specs

type RouteSpec[]

method

type string

path

type string

response

type Response

code?

the body's error code, or null when it has none; read from the body when omitted

type string | null
optional
returns Promise<void>

throws

  • Error - if the response body fails the route spec's declared schemas (propagated from `assert_response_matches_spec`), or if the body was consumed and no `code` was given.

mutates

  • this.observed — via `record` after `assert_response_matches_spec` succeeds.

uncovered

Find declared error paths that were never observed.

Computes the declared set from each route's error map (resolve_route_error_map, this.route_error_maps): one row per declared code at each status. An observation with a code covers that row alone; a status-only observation (a null code) covers every row at its status — the "any-code" rule.

type (route_specs: RouteSpec[], options?: CoverageFilterOptions | undefined): UncoveredEntry[]

route_specs

type RouteSpec[]

options?

optional
returns UncoveredEntry[]

ErrorCoverageOptions
#

UncoveredEntry
#

testing/error_coverage.ts view source

UncoveredEntry import type {UncoveredEntry} from '@fuzdev/fuz_app/testing/error_coverage.js';

Uncovered entry — a declared code at a status of a route, never observed.

method

type string

path

type string

status

type number

code

The declared code missing.

type string

Depends on
#

Imported by
#