Testing & Debugging

Integration Testing

Integration tests verify that multiple parts of your system work together correctly. React Testing Library tests components as users interact with them; Supertest tests your APIs end-to-end; MSW intercepts network calls for realistic test environments.

What is integration testing?

Integration tests check that several units work correctly together: a component with its data hooks, or an API route with the database. In the front end, React Testing Library renders components and interacts with them like a user (getByRole, userEvent.click). Network calls are mocked at the HTTP level with MSW.

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

test("adds a todo", async () => {
  render(<TodoApp />);
  await userEvent.type(screen.getByRole("textbox"), "Learn tests");
  await userEvent.click(screen.getByRole("button", { name: /add/i }));
  expect(screen.getByText("Learn tests")).toBeInTheDocument();
});

The Testing Pyramid

E2E Tests

Few, slow, expensive — full browser flows

Integration Tests

Medium — components + API + DB

Unit Tests

Many, fast, cheap — pure functions

Aim for mostly unit tests, a solid layer of integration tests, and a few critical E2E tests.

RTL Query Priority

Use higher-priority queries first — they better reflect how users interact with your UI.

#1getByRoleQueries by ARIA role + accessible name — closest to screen reader / user experience
#2getByLabelTextForm inputs associated with a <label>
#3getByTextVisible text content (non-interactive elements)
#4getByPlaceholderTextPlaceholder attribute (prefer label)
#5getByAltTextImage alt text
#6getByTestIddata-testid fallback — avoid when possible, not user-facing

Common Pitfalls

Using getByTestId everywhere — tests break if you rename a className or data-testid without changing behavior
Prefer getByRole, getByLabelText, getByText — these survive styling/class refactors.
Using act() warnings as a sign to wrap everything in act() — can mask real async bugs
Fix the underlying async issue (missing await, missing waitFor). act() is usually handled by RTL automatically.
Not resetting MSW handlers between tests — one test's override bleeds into next test
Add afterEach(() => server.resetHandlers()) to your test setup file.
Testing snapshot everything — snapshots grow stale and developers auto-update them without reading
Use snapshots sparingly (e.g. for serialized JSON). Prefer explicit assertions about specific UI elements.

Interactive Demo

Code Editor

<form id="todo"><input name="title" aria-label="New todo"><button>Add</button></form>
<ul id="list"></ul>
<pre id="out"></pre>

Preview

Integration Testing: summary

Key points

  • Query by role and label text as users do; avoid test IDs and CSS selectors when you can.
  • Integration tests usually give the most confidence for the effort.
  • Keep them deterministic: control time, randomness and network responses.

Frequently asked questions

›How many unit vs integration tests should I write?

Many teams follow the "testing trophy": mostly integration tests, some unit tests for tricky logic, and a few end-to-end tests for critical flows.