Practical Testing Strategy
Compare the test pyramid vs testing trophy, decide what to test where, test with in-memory fakes, tame flaky tests, and use coverage as a signal—with this site's real setup.
TL;DR
Put fast pure logic and invariants in unit tests, verify module boundaries and UI in integration tests with in-memory fakes, and keep E2E / accessibility / Lighthouse focused on critical paths. Treat coverage as a signal (not a target), and fix or delete flaky tests immediately.
Why it exists
Teams without a clear testing strategy either write zero tests (shipping regressions every week) or build an inverted pyramid of slow, flaky Playwright suites that take 40 minutes in CI and fail on unrelated CSS animations.
Mental model
Each test layer trades speed and isolation for real-world confidence. Push checks as far down the stack as can still catch the real bug class.
Core concepts
1. Test pyramid vs testing trophy
- Test pyramid: Many fast unit tests at the base, fewer integration tests in the middle, and a handful of end-to-end (E2E) browser tests at the top. Works well for algorithm-heavy backends and domain cores.
- Testing trophy: Static analysis (TypeScript + ESLint) at the base, a thick middle of integration tests (e.g., React Testing Library + real HTTP/DB boundaries), and small unit and E2E layers. Works well for CRUD web apps where bugs live in glue code.
2. What to test where (and contract tests)
| Layer | What belongs here | What to avoid |
|---|---|---|
| Unit | Pure functions, state machines, layout math, schema validators | Mocking 6 dependencies just to test a 5-line wrapper |
| Integration | Component trees, DB queries against a real/test DB, port + fake adapters | Testing every CSS pixel in JSDOM |
| Contract | OpenAPI / schema compatibility between API producer and consumer | Duplicating full business logic tests in contract specs |
| E2E & QA | Critical user journeys, rendered layout, accessibility (axe), performance | Testing every validation permutation through a browser |
3. Testing with fakes (not mock soup)
As shown in Dependency Injection, swap external I/O ports for in-memory fakes rather than fragile vi.fn() call-order assertions. In this repo's labs/arch-order-service/, InMemoryOrderRepo and InMemoryOrderGateway let tests run in milliseconds with zero Postgres setup while still exercising real business rules.
4. This repo's real setup
- Vitest (
pnpm test): Runslib/security-headers.test.ts,components/flow/Flow.test.tsx, andcomponents/diagram/invariants.test.ts(over 500 automated layout/overlap/label checks across every<Flow>and<Diagram>incontent/docs/). - Lab tests (
labs/): Node's built-innode:testrunner verifieslabs/bridge-pattern-tsandlabs/arch-order-service(layered,hexagonal,clean). - Browser QA: Playwright scripts (
scripts/qa-shots.mjs,scripts/qa-diagrams.mjs,scripts/qa-axe.mjswith@axe-core/playwright) and@lhci/clicheck real rendered widths (360px,1280px), light/dark themes, accessibility violations, and Core Web Vitals against the production build.
5. Flaky tests and coverage
- Flaky tests destroy trust: If a test fails 1 in 20 runs without code changes, developers stop reading CI failures. Fix non-determinism (shared DB rows,
setTimeout, real clockDate.now(), unawaited promises) or remove the test. - Coverage is a signal, not a goal: Use coverage reports to find untested critical branches (auth, money, error handling)—never enforce an arbitrary 95% gate that incentivizes assertion-free smoke tests.
When you DON'T need this
- Throwaway spikes or one-off scripts: A 2-hour prototype you plan to delete doesn't need a Playwright suite.
- Static prose-only pages: You don't write unit tests for markdown paragraphs—instead, this repo tests the build invariants (frontmatter schema, static route export, diagram overlap math, and accessibility).
- Cost / complexity trade-off: More test layers mean more CI minutes and test maintenance when requirements change. Invest heavily where bugs are expensive (money, auth, data integrity, layout invariants) and lightly where they are cheap.
Gotchas
- Testing implementation details: Asserting that
repo.savewas called with exact internal arguments breaks every refactor; assert the resulting state or returned output instead. - Mocking the database for SQL tests: In-memory mocks can't catch Postgres syntax errors, index misses, or constraint violations—run SQL queries against a real Postgres instance in CI.
- Ignoring
prefers-reduced-motionin E2E: Unsettled CSS transitions cause random click/screenshot flakes unless Playwright emulates reduced motion or waits on stable locators.
Production checklist
-
pnpm lint,pnpm typecheck, andpnpm testrun on every pull request in CI. - Business logic is decoupled behind ports so unit/integration tests use fast in-memory fakes.
- E2E tests cover only critical flows, use condition-based waits (no
sleep), and run in isolation. - Accessibility (
axe) and responsive screenshots (360px/1280px) are verified on UI changes.
Check yourself
Check Yourself
1. Why are in-memory fakes generally preferred over heavy mocking in application tests?
2. A team enforces 95% line coverage in CI, and developers start writing tests that call functions without meaningful assertions. What principle did they miss?
Sources
Practical Code Review (Including AI-Generated Code)
What to review (correctness, security, tests, readability), PR sizing, Conventional Comments, turnaround targets, self-review, and verifying AI-generated PRs.
Incident Management and Blameless Postmortems
Severity levels, incident roles (IC and comms), mitigation-first timelines, a blameless postmortem template with actions, and a training scenario.