Testing Strategies
Combine layers, ratios, and policies into a testing strategy that scales with your team.
What you'll learn
- Designing a layered strategy: unit, integration, E2E, and manual
- Choosing test ratios that match your project's risk profile
- Rolling out testing on a legacy codebase incrementally
- Measuring and evolving your strategy over time
Concept
A Layered Strategy
A testing strategy is a deliberate mix of test types. The testing pyramid (many unit, fewer integration, few E2E) is a starting point, but the right shape depends on your project. A UI-heavy app may look more like a testing trophy (many component tests). A distributed system may need more contract and integration tests. The strategy is not the pyramid shape; it is the conscious choice of where to invest, based on where your bugs actually come from.
Ratios and Risk
Allocate tests where the risk and the bug rate are highest. If most production incidents come from API integration, invest there. If UI regressions dominate, add component and visual regression tests. Track which test layer catches the most real bugs and shift investment toward it. A good strategy is evidence-based: measure where bugs escape, then strengthen the layer that should have caught them.
Legacy Rollout
You cannot retrofit tests onto a legacy codebase all at once. Adopt the Boy Scout rule: every time you touch a file, add a test for the behavior you changed. Use characterization tests — tests that capture the current behavior (even if buggy) — to lock in a safety net before refactoring. Prioritize the most-changed, highest-risk modules. Over months, coverage climbs without a big-bang effort that stalls.
Evolve and Measure
A strategy is not set-and-forget. Track metrics: suite runtime, flake rate, coverage, bug escape rate, and time-to-first-test-failure. Review them quarterly. If the suite is slow, push tests down the pyramid. If bugs escape, strengthen the leaking layer. If flake is high, invest in isolation. Communicate the strategy to the team so everyone writes tests that fit the plan, and revisit it as the product and team grow.
In Practice
Document your testing strategy in a short TESTING.md that names the layers, the target ratios, and the tools — this aligns the team and onboards new hires. Track bug escape rate: when a bug reaches production, note which test layer should have caught it and add a regression test there. Over a quarter, the pattern of escapes tells you where to invest. If integration tests dominate the runtime, push logic down to pure functions and unit-test it. If E2E tests are flaky, invest in test-data isolation and auto-waiting. Share the metrics with the team so everyone sees the trend — a suite that is getting slower or flakier is a shared problem. The best strategy evolves with the product: what worked at ten engineers may not work at fifty, so revisit the plan when the team or architecture changes meaningfully.
Example
// A characterization test for legacy code: capture current behavior
// before refactoring, so the safety net exists even if the behavior is odd.
import { describe, it, expect } from "vitest";
import { legacyDiscount } from "./legacyPricing";
describe("legacyDiscount (characterization)", () => {
// These expected values are whatever the code does TODAY.
// They lock in behavior so refactors do not change it silently.
it.each([
[100, "regular", 100],
[100, "vip", 90],
[100, "vip", 90], // captured even if the rule is surprising
])("legacyDiscount(%i, %s) === %i", (price, tier, expected) => {
expect(legacyDiscount(price, tier)).toBe(expected);
});
});
// Next: refactor legacyDiscount with this safety net in place,
// then add new tests for the desired (fixed) behavior.
Characterization tests pin down current behavior — even bugs — so you can refactor with confidence. Once the safety net exists, you add tests for the corrected behavior and fix the code.
Try it
- CI YAML Generator
Wire your layered strategy into CI with sharded, gated jobs.
- Code Diff
Review characterization test diffs as you refactor legacy code.
Common mistakes
The mistake
Copying another project's pyramid without analyzing your bugs
The fix
Measure where your bugs come from and invest there. The right shape depends on your architecture and risk profile, not a textbook diagram.
The mistake
Trying to retrofit full coverage on legacy in one push
The fix
Use the Boy Scout rule: add a test for every file you touch. Coverage climbs steadily without a stalled big-bang effort.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.