Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 35 ยท Getting Started

Testing Strategy and the Test Pyramid

Decide what to test end to end and what to leave to unit and component tests, using the test pyramid, critical user journeys and a cost-versus-confidence score.

The most common mistake with browser testing is not a bad selector or a slow test. It is trying to test everything in the browser. A suite like that takes an hour to run, fails for mysterious reasons and ends up ignored. A good strategy decides in advance which checks belong in which layer, so that each one is as cheap as it can be while still giving real confidence.

This lesson gives you a way to think about that choice: the test pyramid, the idea of a critical user journey, and a simple score that compares the value of a test with its cost.

The test pyramid

Picture three layers stacked like a pyramid. The wide base holds many small, fast tests. The narrow tip holds a few big, slow ones.

flowchart TB
  E["End-to-end<br/>few, slow, high realism"]
  C["Component and integration<br/>some, medium speed"]
  U["Unit<br/>many, very fast, low realism"]
  E --- C --- U

The pyramid: more tests at the fast, cheap bottom; fewer at the slow, realistic top.

  • Unit tests cover pure logic: price calculations, validators, formatters, reducers. Thousands can run in seconds.
  • Component and integration tests render one screen or widget with fake data and check behavior. See Testing React Components and Component Testing.
  • End-to-end tests run the whole product. They are the only layer that proves the pieces work together.

The shape matters because of a trade-off you cannot escape: the more realistic a test is, the slower and more fragile it tends to be. Testing a discount rule through the browser takes five seconds and can fail because of a slow network. Testing it as a function takes one millisecond and can only fail if the rule is wrong.

A handy question: "What is the lowest layer where this bug could be caught?" Test it there. Move up only when the bug lives in the connection between layers.

If you are new to the lower layers, read Testing in JavaScript and Testing Hooks, Reducers and Custom Logic alongside this tutorial.

What belongs in an end-to-end test

End-to-end tests are best at proving that critical user journeys work. A journey is a path through the product that a user takes to get something valuable done, and whose failure would hurt the business or the user.

Good candidates:

  • Sign up and log in. If nobody can get in, nothing else matters.
  • The core action of your product. Searching and opening a result, writing and publishing a post, uploading a file.
  • Money paths. Adding to a cart, applying a coupon, paying, receiving a confirmation.
  • Cross-cutting wiring. That the front end really talks to the real back end with the right request shape, that authentication cookies are accepted, that a deployed build boots at all (a "smoke test").

Poor candidates:

  • Every combination of a form's validation rules. Test the rules as unit tests, then add one e2e test that proves an error message appears at all.
  • Pixel details and styling. Use dedicated visual checks (Visual and Accessibility Testing).
  • Third-party widgets you do not control. Stub them (Intercepting and Mocking Network Requests).
  • Rare error branches that are hard to trigger in a real environment. Unit-test the handler or stub the response.

Cost versus confidence

You can make this decision concrete by giving every candidate test two numbers:

  • Value: how bad is it if this breaks without anyone noticing, and how likely is it to break? A checkout that earns the company money and changes often scores high.
  • Cost: how long does it take to run, and how much effort does it take to keep stable?

Dividing value by cost gives a rough return per test. You then fill your time budget (say, a ten-minute pipeline) from the best return downward. The stand-in below does exactly that for a small test plan.

JS Scoring a test plan

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This is a stand-in for a planning conversation, not a real tool, and the numbers are invented. Its value is the habit: write down what each test buys and what it costs. Notice what it surfaces. The "every password-rule combination" test is slow and low value as an e2e test, which is a signal to move those cases down to unit tests.

Splitting a feature across layers

Take a coupon feature on a shopping cart. A sensible split looks like this:

Check Layer
Percent and fixed discounts compute the right total; expired coupons are refused Unit
The coupon box shows an error message for a rejected code and the total line updates Component
A shopper applies a coupon and the order confirmation shows the discounted price One end-to-end test

The unit layer holds many cases; the e2e layer holds one. If the e2e test fails, you know a connection broke, because the unit tests prove the arithmetic.

The stand-in below makes the idea visible. It counts tests and time per layer for a made-up suite and compares two shapes: a healthy pyramid and an "ice cream cone", where most checks are slow browser tests.

JS Pyramid versus ice cream cone

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The pyramid has far more tests yet finishes in a fraction of the time, because most of its tests are cheap. The cone is slow, hard to debug and usually flaky, which is the situation you are trying to avoid.

Practical rules of thumb

  • One journey per test. Keep each test focused on a single outcome so a failure tells you what broke.
  • Independent tests. A test should set up its own data and not rely on another test running first. Test Data and Logging In Fast shows how to do it cheaply.
  • Skip the UI for setup. Create users and records through an API, then use the browser only for the part you want to prove.
  • Stub what you cannot control. Payment providers, email and maps are usually better faked at the network edge in most tests, with perhaps one real run before release.
  • Delete low-value tests. A test that never catches anything but regularly breaks is a net cost.
  • Treat flakiness as a bug. A test you cannot trust is worse than no test.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the test pyramid, which layer should have the most tests?

    Unit tests are the cheapest and fastest, so the base of the pyramid holds the most of them.
  2. 2. Which of these is the best candidate for an end-to-end test?

    Payment is a critical user journey that crosses front end, back end and data. Rules, colors and helper functions are better covered at lower layers or by visual checks.
  3. 3. A discount calculation has twenty edge cases. Where should most of them be tested?

    Logic is cheapest to verify as unit tests. A single browser test then confirms the wiring.
  4. 4. What does an 'ice cream cone' test suite look like?

    The cone inverts the pyramid. It is slow, expensive to debug and prone to flaky failures.

Summary

  • Use a pyramid: many fast unit tests, some component tests, a few end-to-end tests.
  • Reserve browser tests for critical user journeys and for proving that the layers connect.
  • Test each rule at the lowest layer where a bug could be caught.
  • Compare each test's value with its cost, and fit the suite into a time budget.
  • Keep tests focused and independent, set up data without the UI, and stub services you do not control.
  • Delete or fix tests you cannot trust.

Next: Cypress vs Playwright