← Back to Frontend, page 3

frontend-0047 · Easy fresher

What are unit, integration and end-to-end tests in a front-end project, and how do you decide what to test at each level?

Unit tests check one function or component in isolation and are fast. Integration tests check several parts working together, such as a form with its validation and API calls. End-to-end tests drive a real browser through a user flow and are the slowest and most realistic. Use many cheap tests for logic and few expensive ones for critical flows.

Full answer

The three levels trade speed and precision against realism.

  • Unit tests. One unit: a pure function, a hook, a small component. They run in milliseconds, point to the exact line that broke, and are easy to write for logic such as price formatting, validation and reducers. They cannot tell you that the parts work together.
  • Integration tests. Several units with real wiring and fake edges: a component tree rendered with its state and a mocked network. They catch broken contracts between parts, such as a form that sends the wrong field. They run in a test runner with a simulated DOM, in seconds.
  • End-to-end (E2E) tests. A real browser against a running app, usually with a test backend. They prove that a user can log in and buy a course. They are slow, need data setup, and can be flaky, so keep the set small and aimed at critical paths.

The "test pyramid" suggests many unit tests, fewer integration tests and few E2E tests. Front-end teams also use the "testing trophy" idea, which puts most effort into integration tests, because the user-visible behaviour of a component depends on how its parts work together. Both models say the same practical thing: do not test everything at the top level.

Choosing the level:

  1. Pure logic with many cases: unit test with a table of inputs and outputs.
  2. A component or screen with state, forms or data loading: integration test through the rendered output.
  3. A flow that earns money or must not break (sign up, pay, enrol): E2E, one per flow.
  4. Visual details: a visual comparison tool or a manual check, because assertions on pixels are fragile.

Other checks that help: static types and lint (they catch a class of errors with no test), and accessibility checks in tests.

The snippet uses the built-in Node test runner. Two unit tests check small functions, and one test combines them, which is the smallest kind of integration test.

javascript
import test from 'node:test';
import assert from 'node:assert/strict';

const formatPrice = (cents) =>
  new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(cents / 100);
const cartTotal = (items) => items.reduce((sum, i) => sum + i.cents * i.qty, 0);

test('unit: formatPrice shows dollars and cents', () => {
  assert.equal(formatPrice(1999), '$19.99');
});
test('unit: cartTotal multiplies price by quantity', () => {
  assert.equal(cartTotal([{ cents: 500, qty: 2 }, { cents: 250, qty: 1 }]), 1250);
});
test('integration: the cart total is shown as a price', () => {
  const items = [{ cents: 500, qty: 2 }, { cents: 250, qty: 1 }];
  assert.equal(formatPrice(cartTotal(items)), '$12.50');
});

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. Run with the Node test runner (node --test). Three tests passed in two runs. No browser test runner was used.

Key points

  • Unit: fast, isolated, precise. Integration: parts together. E2E: real browser, real flow.
  • Pyramid: many unit tests. Trophy: most effort in integration tests.
  • Choose the lowest level that can catch the bug.
  • Keep E2E tests few and focused on critical flows.
  • Types and lint are cheap tests too.

Common mistakes

  • Testing everything through the browser because it feels realistic.
  • Writing unit tests for implementation details that change often.
  • Counting coverage percent as the goal.
  • Leaving critical flows with no end-to-end test.

Follow-ups

  • Why are E2E tests slower and more fragile?
  • How do you decide whether to mock the network in an integration test?
  • What does a test of an implementation detail look like?

Tips

  • Describe each level by speed, cost and what it can catch.
  • Give one example of what you would test at each level.