Skip to main content

Production React Testing Strategy

A healthy React test suite is a portfolio. Different layers protect different failure modes; no one layer should carry all confidence.

Risk determines the testing mix
Pure domain logic
Reducers · parsers · validators · selectors
Component behavior
Rendered UI · forms · focus · optimistic states
Integration
Route · providers · data client · real component tree
End to end
Real browser + important product flows

The goal is not a fixed pyramid ratio. The goal is the cheapest test that can reliably catch the failure you care about.

Start from risk, not coverage quotas​

Ask:

  • What failures would hurt users most?
  • Which domain transitions are subtle?
  • Which integrations have failed before?
  • Which journeys affect money, security, or irreversible actions?
  • Which accessibility behaviors are easy to regress?
  • Which server/client boundaries need real platform confidence?

Pure logic tests​

Good targets include reducers, validators, parsers, selectors, permission rules, state-machine transitions, formatters, and cache-key construction.

expect(cartReducer(state, { type: 'itemRemoved', id: '42' })).toEqual(expected);

These tests are cheap because React and the DOM are not required.

Component behavior tests​

Use a DOM environment for:

  • meaningful rendering;
  • user interactions;
  • controlled inputs;
  • loading/error states;
  • focus changes;
  • accessible names/roles;
  • provider integration;
  • optimistic UI.

Integration tests​

Integration test = several real application pieces
Route + providers
Real ownership and context
Feature component tree
Forms · state · effects · Suspense
Controlled external boundary
Network/provider mocked realistically

This catches context wiring, route state, serialization assumptions, cache interactions, mutation/revalidation bugs, and focus regressions. Do not mock every layer inside an integration test.

End-to-end tests​

Use a real browser for high-risk workflows such as sign-in, checkout, account creation, destructive confirmation, file upload, payments, role changes, complex keyboard interactions, and hydration regressions that depend on browser behavior.

Which layer should own this test?
Pure deterministic business ruleUnit test
Rendered interaction/focus/state contractComponent test
Several real app layers must cooperateIntegration test
Browser/network/navigation/platform behavior is essentialEnd-to-end test

Test network states, not only 200 success​

A useful integration suite can model:

Expected client failures
400 · 401 · 403 · 404 · 409
Infrastructure failures
500 · network error · aborted request
Timing
slow response · retry · cancellation

React UI often breaks during transitions between states rather than on the happy path itself.

Test recovery, not only failure​

If the product supports retry, protect the complete state machine:

1Request fails
2Useful error UI
3User chooses Retry
4Next request succeeds
5UI recovers

Cancellation tests should protect user-visible invariants​

For search/navigation/streaming/subscriptions, assert that stale results cannot overwrite newer state, aborted work does not become a false error, and leaving a screen cleans up ongoing work.

Avoid testing private AbortController details unless cancellation is itself part of a public library API.

SSR and hydration tests​

Hydration bugs are consistency bugs.

Hydration confidence spans layers
Server render
Deterministic HTML/data snapshot
Client hydration
IDs · locale · data agree
Interactive UI
No mismatch/regression

Important scenarios include consistent useId, no browser-only reads during server render, deliberate time/locale serialization, and no blind suppressHydrationWarning use.

Server Components and Server Functions​

RSC applications should test:

  • server data/domain services;
  • route rendering through framework integration;
  • serialization-safe client-boundary props;
  • Server Function authentication, authorization, runtime validation, idempotency, transactions, and error mapping;
  • client behavior after the server/client boundary.

A button-click test alone does not secure a server mutation boundary.

Accessibility is a testing layer, not a single tool​

Accessibility confidence is layered
Semantic tests
roles · names · labels · focus
Automated checks
structural violations
Human/platform review
keyboard · screen reader · contrast · real browser

Automated tools cannot judge the entire interaction experience.

Visual regression testing​

Useful for design-system primitives, layout-heavy pages, themes, responsive breakpoints, charts, and high-risk CSS refactors.

Visual tests answer “did appearance change?” They do not replace behavior or accessibility assertions.

Type tests and runtime tests are complementary​

A compile-time test can prove:

<Button tone="primary" />; // valid
// @ts-expect-error
<Button tone="not-real" />;

It cannot prove that the button renders, responds to clicks, exposes the right role/name, manages focus, or has correct CSS.

Coverage is a signal, not confidence itself​

render(<Widget />);

may execute many lines while asserting almost nothing.

Use coverage to find untested areas; use risk and contracts to decide what deserves tests.

Flaky tests are defects​

Common causes include arbitrary sleeps, real time when deterministic control is possible, leaked global state, shared mutable fixtures, unawaited interactions, order-dependent mocks, unstable selectors, scheduler-count assertions, timezone dependence, and missing cleanup.

Fix the race instead of adding blind retries.

Deterministic test data​

function makeUser(overrides: Partial<User> = {}): User {
return {
id: 'user-1',
name: 'Aisha',
email: 'aisha@example.com',
...overrides,
};
}

Factories are usually easier to isolate than one giant shared fixture object.

Match the test layer to state ownership​

URL owns state
Test navigation/location semantics
Server owns data
Test request/cache/revalidation behavior
Local UI owns state
Test the component interaction directly

The test architecture should reflect the application architecture.

Do not duplicate every assertion at every layer​

A reducer unit test can prove a formula. A browser checkout test proves the journey. Neither needs to re-prove the other's entire responsibility.

Production incidents should create regression contracts​

1Production failure
2Smallest meaningful failing test
3Fix implementation
4Keep regression test
5Review wider missing test class

This steadily aligns the suite with real-world risk.

CI strategy​

Run high-signal cheap checks early
Fast feedback
typecheck · lint · pure unit tests · component tests
Broader confidence
integration · visual · end-to-end · cross-browser
Production confidence
critical journeys + deployment-specific checks

Exact stages depend on repository size and deployment risk.

What usually does not need testing​

Usually do not test React itself, TypeScript itself, that useState can update state, trivial getters/setters, or third-party internals unless your own integration contract depends on them.

Spend test budget where your code and architecture can be wrong.

Feature review checklist​

  1. Are important domain transitions covered?
  2. Are meaningful user behaviors covered?
  3. Are failure, retry, and cancellation paths represented?
  4. Are async boundaries deterministic?
  5. Are permissions verified server-side?
  6. Is keyboard/focus behavior protected?
  7. Are reusable TypeScript contracts tested where useful?
  8. Is at least one high-risk journey covered in a real browser when appropriate?
  9. Would a regression test catch the failure you fear most?

Interview questions​

  1. How do unit, component, integration, and end-to-end React tests differ?
  2. What should determine the testing mix?
  3. Why do Server Functions need backend tests?
  4. Which hydration problems need browser-level confidence?
  5. Why are type tests and runtime tests complementary?
  6. Why is coverage a signal rather than a quality target?
  7. How should production incidents influence the suite?

References​