Skip to Content
Mengty Learns
Process

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.

explanationintermediateTested with vitest 5.1.1, playwright 1.65.1, axe_core 4.14.1, lhci 0.16.1Updated Nov 27, 2026

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.

This repo's test layers: fast Vitest invariants first, then Playwright layout/axe/Lighthouse QA.

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)

LayerWhat belongs hereWhat to avoid
UnitPure functions, state machines, layout math, schema validatorsMocking 6 dependencies just to test a 5-line wrapper
IntegrationComponent trees, DB queries against a real/test DB, port + fake adaptersTesting every CSS pixel in JSDOM
ContractOpenAPI / schema compatibility between API producer and consumerDuplicating full business logic tests in contract specs
E2E & QACritical user journeys, rendered layout, accessibility (axe), performanceTesting 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): Runs lib/security-headers.test.ts, components/flow/Flow.test.tsx, and components/diagram/invariants.test.ts (over 500 automated layout/overlap/label checks across every <Flow> and <Diagram> in content/docs/).
  • Lab tests (labs/): Node's built-in node:test runner verifies labs/bridge-pattern-ts and labs/arch-order-service (layered, hexagonal, clean).
  • Browser QA: Playwright scripts (scripts/qa-shots.mjs, scripts/qa-diagrams.mjs, scripts/qa-axe.mjs with @axe-core/playwright) and @lhci/cli check 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 clock Date.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.save was 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-motion in 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, and pnpm test run 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