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
Few, slow, expensive — full browser flows
Medium — components + API + DB
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.
getByRoleQueries by ARIA role + accessible name — closest to screen reader / user experiencegetByLabelTextForm inputs associated with a <label>getByTextVisible text content (non-interactive elements)getByPlaceholderTextPlaceholder attribute (prefer label)getByAltTextImage alt textgetByTestIddata-testid fallback — avoid when possible, not user-facingCommon Pitfalls
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.