Testing (Consumer Test Coverage)
Overview
Improve coverage by exercising consumer-visible behavior with infra mocked and behavior preserved.
Inputs / Outputs
Inputs: Spec/contract artifacts from spec or plan (optional but preferred); consumer-facing entrypoints to test.
Outputs: Test suite pinning consumer-visible behavior; coverage report. Consumed by finish and review.
Workflow
- Read relevant specs (system + service) and map them to consumer-visible flows and invariants.
- Identify consumer-facing entrypoints: HTTP/gRPC handlers, public service methods, event consumers, cache/storage adapters, jobs.
GATE: Do not write tests until consumer-facing entrypoints are identified (step 2). If no entrypoints are listed, go back — tests without identified entrypoints tend to test implementation details.
- Add tests for success and failure paths that a consumer can observe (invalid input, downstream failures, permissions, timeouts where applicable).
- Mock infra boundaries (DB, Redis, network listeners, clocks/timers). Prefer calling handlers/functions directly instead of running real servers.
- Run focused coverage and iterate until the target is met (default 80% unless the spec says otherwise).
Minimum viable execution
When context or time is constrained, these are the load-bearing steps:
- Read specs and map to consumer-visible flows (step 1) — tests must trace back to spec'd behavior.
- Identify consumer-facing entrypoints (step 2) — determines what to test.
- Write success + failure path tests (step 3) — both paths, not just happy path.
- Run coverage (step 5) — verify the tests actually exercise the code.
Steps that can be cut under pressure: mocking strategy optimization (step 4), coverage iteration beyond first pass.
Chooser (What Test Type Where)
- New endpoint / handler change: consumer-visible tests — call handler with mocked dependencies, assert response shape + status codes + error handling.
- Refactor (no behavior change): characterization tests first — pin existing behavior before changing implementation.
- New event consumer / job: feed mixed payloads (valid, invalid, missing fields, duplicates); assert side effects and idempotency.
- Boundary change (DB/cache/client): adapter tests — cover happy path, empty/null results, connection failures, timeouts.
- Cross-service contract change: consumer-contract tests — verify your consumer expectations match the provider's contract.
- Coverage gap (existing code): start with the riskiest paths — auth/permissions, error handling, input validation, state transitions.
Clarifying Questions
- What entrypoints are affected (HTTP handler, gRPC method, consumer, job, adapter)?
- Are there existing specs/contracts that define expected behavior?
- Is this new behavior (need new tests) or existing behavior (need characterization tests before refactoring)?
- What is the target coverage level (default: 80%)?
- What test runner and mocking setup does the project use?
Testing Patterns
- Handler paths: call handler with mocked service, assert response, metrics, and error handling.
- Event consumers: feed mixed payload shapes (missing type, struct/list values, invalid entries).
- Cache/storage: cover cache hit/miss, null/empty results, invalidation behavior.
- Jobs: use fake timers; cover interval runs and error logging branches.
- Observability: assert metrics render and logging mixins without external services.
- Vitest note: if mocked values are referenced by
vi.mock factories, use vi.hoisted to avoid init-order bugs.
Guardrails
- Preserve externally visible behavior and API shapes.
- Avoid real network/listen calls in unit tests; mock them.
- Keep tests consumer-focused; do not assert internal implementation details beyond outputs/side effects.
Common failure modes
- Tests implementation details instead of consumer-visible behavior (e.g., asserting internal method call counts instead of response shape).
- Defaults to unit tests regardless of context — should use the chooser to pick the right test type.
- Mocks the thing being tested instead of its dependencies — the test exercises the mock, not the code.
- Tests happy path only, skips failure modes — missing tests for invalid input, downstream failures, permission denials, and timeouts.
Commands
- Vitest example:
npx vitest run apps/<service>/**/*.test.ts --coverage --coverage.include='apps/<service>/src/**'
- Generic:
cd apps/<service> && npm test -- --coverage
References
Output Template
When applying this skill, return:
- What consumer-visible behavior is now pinned (happy path + key failure modes).
- What tests were added/changed (by entrypoint: handler/consumer/job/adapter).
- Coverage/verification results (commands run + outcomes) and any notable gaps/follow-ups.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: testing-803description: Create or expand test suites for microservices (unit, integration, consumer-contract tests for HTTP/gRPC handlers, service flows, event consumers, caches, jobs). Use when adding tests, raising coverage, writing regression tests, or validating consumer-facing behavior. NOT for adversarial code review (use review); NOT for final ship-readiness checks (use finish). Use when this capability is needed.4---56# Testing (Consumer Test Coverage)78## Overview910Improve coverage by exercising consumer-visible behavior with infra mocked and behavior preserved.1112## Inputs / Outputs1314**Inputs**: Spec/contract artifacts from `spec` or `plan` (optional but preferred); consumer-facing entrypoints to test.15**Outputs**: Test suite pinning consumer-visible behavior; coverage report. Consumed by `finish` and `review`.1617## Workflow18191. Read relevant specs (system + service) and map them to consumer-visible flows and invariants.202. Identify consumer-facing entrypoints: HTTP/gRPC handlers, public service methods, event consumers, cache/storage adapters, jobs.2122> **GATE**: Do not write tests until consumer-facing entrypoints are identified (step 2). If no entrypoints are listed, go back — tests without identified entrypoints tend to test implementation details.23243. Add tests for success and failure paths that a consumer can observe (invalid input, downstream failures, permissions, timeouts where applicable).254. Mock infra boundaries (DB, Redis, network listeners, clocks/timers). Prefer calling handlers/functions directly instead of running real servers.265. Run focused coverage and iterate until the target is met (default 80% unless the spec says otherwise).2728## Minimum viable execution2930When context or time is constrained, these are the load-bearing steps:31321. **Read specs and map to consumer-visible flows** (step 1) — tests must trace back to spec'd behavior.332. **Identify consumer-facing entrypoints** (step 2) — determines what to test.343. **Write success + failure path tests** (step 3) — both paths, not just happy path.354. **Run coverage** (step 5) — verify the tests actually exercise the code.3637Steps that can be cut under pressure: mocking strategy optimization (step 4), coverage iteration beyond first pass.3839## Chooser (What Test Type Where)4041- **New endpoint / handler change**: consumer-visible tests — call handler with mocked dependencies, assert response shape + status codes + error handling.42- **Refactor (no behavior change)**: characterization tests first — pin existing behavior before changing implementation.43- **New event consumer / job**: feed mixed payloads (valid, invalid, missing fields, duplicates); assert side effects and idempotency.44- **Boundary change (DB/cache/client)**: adapter tests — cover happy path, empty/null results, connection failures, timeouts.45- **Cross-service contract change**: consumer-contract tests — verify your consumer expectations match the provider's contract.46- **Coverage gap (existing code)**: start with the riskiest paths — auth/permissions, error handling, input validation, state transitions.4748## Clarifying Questions4950- What entrypoints are affected (HTTP handler, gRPC method, consumer, job, adapter)?51- Are there existing specs/contracts that define expected behavior?52- Is this new behavior (need new tests) or existing behavior (need characterization tests before refactoring)?53- What is the target coverage level (default: 80%)?54- What test runner and mocking setup does the project use?5556## Testing Patterns5758- Handler paths: call handler with mocked service, assert response, metrics, and error handling.59- Event consumers: feed mixed payload shapes (missing type, struct/list values, invalid entries).60- Cache/storage: cover cache hit/miss, null/empty results, invalidation behavior.61- Jobs: use fake timers; cover interval runs and error logging branches.62- Observability: assert metrics render and logging mixins without external services.63 - Vitest note: if mocked values are referenced by `vi.mock` factories, use `vi.hoisted` to avoid init-order bugs.6465## Guardrails6667- Preserve externally visible behavior and API shapes.68- Avoid real network/listen calls in unit tests; mock them.69- Keep tests consumer-focused; do not assert internal implementation details beyond outputs/side effects.7071## Common failure modes7273- Tests implementation details instead of consumer-visible behavior (e.g., asserting internal method call counts instead of response shape).74- Defaults to unit tests regardless of context — should use the chooser to pick the right test type.75- Mocks the thing being tested instead of its dependencies — the test exercises the mock, not the code.76- Tests happy path only, skips failure modes — missing tests for invalid input, downstream failures, permission denials, and timeouts.7778## Commands7980- Vitest example: `npx vitest run apps/<service>/**/*.test.ts --coverage --coverage.include='apps/<service>/src/**'`81- Generic: `cd apps/<service> && npm test -- --coverage`8283## References8485- Specs and contracts as test sources: [`spec`](../spec/SKILL.md)86- TypeScript test skeletons: [`references/snippets/typescript.md`](references/snippets/typescript.md)87- Related patterns: [`Consumer-side contract test`](../architecture/references/consumer-side-contract-test.md), [`Service integration contract test`](../architecture/references/service-integration-contract-test.md), [`Service component test`](../architecture/references/service-component-test.md)88- Telemetry verification (when tests cover boundary logging/metrics): [`observability`](../observability/SKILL.md)8990## Output Template9192When applying this skill, return:9394- What consumer-visible behavior is now pinned (happy path + key failure modes).95- What tests were added/changed (by entrypoint: handler/consumer/job/adapter).96- Coverage/verification results (commands run + outcomes) and any notable gaps/follow-ups.9798---99> Converted and distributed by [TomeVault](https://tomevault.io/claim/bricerising) — claim your Tome and manage your conversions.100<!-- tomevault:4.0:skill_md:2026-04-13 -->