Choosing a Test Strategy
Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.
What you will be able to do
#- Compare the test pyramid, the trophy and the ice-cream cone.
- Choose test types from risk rather than from a template.
- Explain the trade-offs of each layer to a team.
Overview
#Strategy is about where to spend effort
A test strategy decides how much of each kind of test to write. There is no universally correct ratio, only trade-offs between feedback speed, confidence and maintenance cost.
The pyramid
The classic pyramid puts many unit tests at the base, fewer integration tests in the middle and a few end-to-end tests at the top. It optimizes for fast feedback and low maintenance, at the cost of less confidence that the whole system works.
The trophy
The testing trophy widens the integration layer, keeping a solid unit base and a thin end-to-end top. It suits frontend applications where the value is in components and their wiring, and where integration tests catch the defects that matter most while staying reasonably fast. This course follows the trophy shape.
The ice-cream cone
The ice-cream cone inverts the pyramid: many slow end-to-end tests and few unit tests. It produces slow, flaky suites that are expensive to maintain and give late feedback. It is the anti-pattern to recognize and avoid.
Choose from risk
Let the cost of a defect drive the choice. A pricing calculation deserves unit tests, boundary tests and mutation testing. A permission check deserves tests for every role and denial path. A marketing page deserves a smoke test that it renders. A critical user journey deserves one end-to-end test. Match the investment to the consequence.
Confidence versus speed
Every test you add buys confidence and costs time. A suite that takes an hour to run will be skipped locally and will slow every deploy. A suite that runs in seconds encourages frequent runs. Balance the two by keeping the fast layers large and the slow layers small and critical.
Maintenance is a real cost
A test that breaks on every refactor is a liability. Prefer tests that assert on contracts and observable behavior so safe refactors do not touch them. Treat test code with the same care as production code: readable, isolated and deterministic. A test suite is a product you maintain, not a byproduct you tolerate.
Revisit the strategy
As the application changes, the right balance changes. A feature that becomes critical may deserve deeper tests; a stable area may need fewer. Review coverage, mutation results and flake rate periodically and adjust. A strategy is a living decision, not a one-time setup.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which shape has many slow end-to-end tests and few unit tests?
Exercise 2Explain
Explain why the testing trophy suits a frontend application better than a pure pyramid.
Reveal the reference answer
reference.javascriptFrontend value lives in components and how they wire together, so integration tests catch the defects that matter most: a store not updating the view, an event not wired, a form not validating. A pure pyramid would push those into many small unit tests and a few end-to-end tests, leaving the wiring under-tested. The trophy widens the integration layer to cover that wiring while keeping a unit base and a thin end-to-end top for the critical journeys.The answer should connect the trophy's wide integration layer to the wiring defects that dominate frontend applications.
Exercise 3Implement
Implement `recommendTests(risk)` that returns an array of test types based on risk level: 'high' returns ['unit','integration','e2e'], 'medium' returns ['unit','integration'], and anything else returns ['unit']. `console.log(recommendTests('high').join(','))` should print `unit,integration,e2e`.
starter.javascriptfunction recommendTests(risk) { // your code here }Reveal the reference answer
reference.javascriptfunction recommendTests(risk) { if (risk === 'high') return ['unit', 'integration', 'e2e']; if (risk === 'medium') return ['unit', 'integration']; return ['unit']; }Authored expected output: unit,integration,e2e
Higher risk justifies more layers of testing. The function maps a risk level to a proportionate set of test types, which is the core idea of risk-based strategy.
Exercise 4Debug
This strategy puts a slow end-to-end test on every pure function, which is wasteful. Fix it to use a unit test for the pure function.
starter.javascriptfunction strategy(kind) { return 'e2e'; } console.log(strategy('pure-function'));Reveal the reference answer
reference.javascriptfunction strategy(kind) { if (kind === 'pure-function') return 'unit'; if (kind === 'user-journey') return 'e2e'; return 'integration'; } console.log(strategy('pure-function'));Authored expected output: unit
A pure function has no wiring or browser behavior, so a fast unit test covers it fully. Reserving end-to-end tests for user journeys keeps the slow layer small and the feedback fast.
Exercise 5Test
Write a test that proves `recommendTests` returns proportionate layers for high, medium and low risk.
starter.javascriptfunction recommendTests(risk) { if (risk === 'high') return ['unit', 'integration', 'e2e']; if (risk === 'medium') return ['unit', 'integration']; return ['unit']; }Reveal the reference answer
reference.javascriptfunction assertEqual(actual, expected, label) { if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual); } assertEqual(recommendTests('high').length, 3, 'high risk gets three layers'); assertEqual(recommendTests('medium').length, 2, 'medium risk gets two layers'); assertEqual(recommendTests('low').length, 1, 'low risk gets one layer'); console.log('strategy verified');Authored expected output: strategy verified
The three assertions prove the mapping scales the number of layers with risk. This encodes the strategy decision so a future change to the mapping is caught.
Exercise 6Interview
In an interview, explain how you would choose a test strategy for a new frontend feature.
Reveal the reference answer
reference.javascriptI start from risk: what happens if this feature breaks? A pricing or permission feature gets a solid unit and integration base plus mutation testing on the critical functions. A critical user journey gets one end-to-end test. I follow the trophy shape, keeping the fast layers large and the slow layers small. I write tests against contracts so refactors do not break them, and I review flake rate and coverage periodically to adjust. I do not apply a fixed ratio; I match the investment to the consequence.The answer should be risk-based, follow the trophy shape, and treat the strategy as adjustable rather than fixed.
Common misconceptions
#- There is one correct ratio of unit to integration to end-to-end tests.
- More tests always mean better quality.
- Test code does not need the same care as production code.
Follow-up practice
#- testing-capstone-quality-task-board
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.