Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Testing Pyramid: Where to Start with Front End Testing

· 6 min read · Home Tech

In this article
  1. Background: Why the Pyramid Shape?
  2. How the Pyramid Works in Front-End Testing
  3. Where to Start: A Practical Path
  4. How to Choose What to Test First
  5. Common Problems and Misconceptions
  6. Costs and Benefits
  7. Alternatives to the Pyramid
  8. Frequently Asked Questions

To start with front-end testing using the testing pyramid, begin with a solid base of unit tests for your individual functions and components, then add a smaller layer of integration tests to verify how those pieces work together, and finish with a few end-to-end (E2E) tests that cover critical user journeys. This approach gives you the fastest, most reliable feedback and the lowest maintenance burden, because unit tests are quick and cheap to run, while E2E tests are slow and brittle.

Background: Why the Pyramid Shape?

The testing pyramid, popularized by Mike Cohn in his book Succeeding with Agile, is a visual metaphor for balancing test speed, cost, and confidence. The base is wide—many unit tests—because they run in milliseconds and pinpoint failures precisely. The middle is narrower—fewer integration tests—because they verify interactions between modules and may require more setup. The top is the smallest—just a handful of E2E tests—because they simulate real user flows through the entire stack, which is slow and often flaky. The pyramid’s shape reminds you not to invert it: if your suite is mostly E2E tests, you’ll suffer from long runtimes, random failures, and high maintenance costs.

How the Pyramid Works in Front-End Testing

In a typical React, Vue, or Angular application, the layers map to concrete tools and practices:

  • Unit tests (e.g., Jest, Vitest, Mocha) test a single function, component, or utility in isolation. For example, you might test a formatCurrency helper or a button component’s rendering logic.
  • Integration tests (e.g., React Testing Library, Vue Test Utils) test how multiple units work together—like a form component that validates input and calls a submit handler, or a list that renders items from a mocked API.
  • E2E tests (e.g., Cypress, Playwright) run the full application in a real or simulated browser, clicking through actual user flows, such as logging in, adding an item to a cart, and checking out.

Each layer has a different purpose. Unit tests catch logic errors early; integration tests catch mismatches between components; E2E tests catch systemic issues like broken navigation or server communication. The pyramid guides you to invest most effort in the lowest layer because that’s where you get the most value per test.

Where to Start: A Practical Path

If you’re new to front-end testing, don’t try to build all three layers at once. Start with unit tests for the parts of your code that are most likely to break and most expensive to fix manually—typically pure functions, utility modules, and small presentational components. Then, as your confidence grows, add integration tests for critical user interactions. Finally, add a few E2E tests for the most important user journeys (like sign-up or checkout) once your app is stable enough that they won’t be flaky.

For a typical component, a good first unit test might check that it renders the correct text, handles an empty state, or calls a callback when clicked. For a utility, test edge cases: empty strings, negative numbers, or invalid inputs. These tests are cheap to write and run, and they give you immediate feedback when you refactor.

How to Choose What to Test First

Prioritize based on risk and business value. Ask: “What would break the user experience if it failed?” Common candidates include:

  • Authentication and authorization flows
  • Checkout and payment processing
  • Data fetching and error handling
  • Forms with validation
  • State management logic (e.g., Redux reducers, Vuex mutations)

Start with the simplest unit tests for these areas, then add integration tests that simulate user interactions. For example, test that a login form shows an error message when the API returns 401, or that a product list displays a loading spinner before data arrives. These tests catch real-world issues without needing a full browser.

Common Problems and Misconceptions

One common mistake is writing too many E2E tests too early. They’re slow and often fail due to timing issues, network delays, or third-party scripts—not because your code is wrong. Another misconception is that you must test every component in isolation. That leads to excessive mocking and tests that don’t reflect real usage. Instead, focus on behavior: what does the user see and do?

Another pitfall is over-mocking. If you mock everything, your tests only verify that your mocks are correct, not that your code works. Use lightweight mocks for external dependencies (like API calls) but let internal components interact naturally. Also, avoid testing implementation details—like CSS classes or internal state—because those change frequently and make tests brittle.

Costs and Benefits

The main cost of testing is time: writing tests, maintaining them, and debugging failures. Unit tests are the cheapest to maintain because they change only when the code they test changes. Integration tests are moderate—they need more setup and may require updating mocks when APIs change. E2E tests are the most expensive, requiring browser automation, waiting for selectors, and handling flakiness. The benefit is confidence: a good pyramid lets you refactor with safety, catch regressions quickly, and ship faster because you trust the suite.

There’s also a learning curve. If your team is new to testing, start with a simple tool like Vitest or Jest and write a few tests for a small feature. As you gain experience, you’ll develop patterns for mocking, async code, and accessibility queries (using roles and labels instead of CSS selectors).

Alternatives to the Pyramid

Some teams adopt a “testing trophy” model, which emphasizes integration tests over unit tests, arguing that they give more confidence per test. Others use a “honeycomb” approach for microservices. For front-end, the trophy model (from Kent C. Dodds) suggests writing a few unit tests, many integration tests, and a few E2E tests. This can work well if your app is heavily integrated—like a single-page app with complex state. However, the pyramid remains a solid default because it’s simpler to reason about and scales well.

Another alternative is visual regression testing (e.g., Percy, Chromatic) which captures screenshots and compares them to baselines. This is great for catching unintended UI changes but complements, not replaces, functional tests.

Frequently Asked Questions

How many unit tests should I write compared to E2E tests?

There’s no fixed ratio, but a common guideline is 70% unit, 20% integration, and 10% E2E. For a small app, you might have 100 unit tests, 20 integration tests, and 5 E2E tests. The key is that the number of tests decreases as you go up the pyramid, because each level covers more surface area per test.

Should I test CSS or visual styles?

No. Unit and integration tests are for logic and behavior. Visual styles are better covered by visual regression tools or manual QA. Testing CSS classes in unit tests only makes your tests brittle and doesn’t verify that the styles actually look correct.

What if my app is just a static landing page?

You may not need many tests. A few unit tests for any interactive elements (like a contact form’s validation) and one E2E test that the page loads and shows the main content could be enough. Don’t force tests where there’s no logic to break.

How do I handle tests for components that depend on browser APIs like localStorage or fetch?

Mock them. In unit tests, use a library like jest-localstorage-mock or mock fetch with a tool like msw (Mock Service Worker). In integration tests, you can wrap your component in a test environment that provides these APIs. For E2E tests, you can use real APIs or intercept network requests with Cypress or Playwright.

Is it okay to skip integration tests and go straight to E2E?

You can, but you’ll likely end up with a slow, flaky suite. Integration tests catch issues that unit tests miss (like a component passing the wrong prop) and are much faster than E2E. It’s worth investing a little time in them, even if you start with just a few.