What is Integration Testing?

Integration testing checks that several units work correctly together, such as a component with its hooks, state and API layer, or an API route with its database, catching problems that isolated unit tests miss.

Where it sits

  • Unit tests: one piece in isolation, dependencies mocked.
  • Integration tests: several real pieces together; only the outer edges (network, third-party services) are faked.
  • End-to-end tests: the whole deployed system through a real browser.

Front-end integration tests

Render a feature (a form, a page section) with React Testing Library, use real components, state and hooks, mock the network with MSW, and interact like a user (userEvent.type, click). Then assert on what the user sees. These tests give high confidence and survive refactors because they do not depend on internals. This idea is behind the "testing trophy", which puts most effort here.

Back-end integration tests

Call an API endpoint (supertest, fetch against a test server) with a real test database (often in Docker or Testcontainers), and check the response and the stored data.

Making them reliable

  • Reset state between tests (fresh database transaction, cleared stores).
  • Use realistic test data factories.
  • Avoid arbitrary sleeps; wait for UI states (findBy..., waitFor).
  • Keep them reasonably fast so they run on every pull request.

What they catch

Wrong data shapes passed between layers, broken wiring, incorrect API contracts, state that does not update the UI, and error paths across boundaries.

Code example

js
1// SignupForm.test.jsx - React Testing Library + MSW: real component, real hooks, fake network
2import { render, screen } from "@testing-library/react";
3import userEvent from "@testing-library/user-event";
4import { http, HttpResponse } from "msw";
5import { setupServer } from "msw/node";
6import { test, expect, beforeAll, afterAll, afterEach } from "vitest";
7import { SignupForm } from "./SignupForm";
8
9const server = setupServer(
10 http.post("/api/signup", async ({ request }) => {
11 const { email } = await request.json();
12 if (email === "taken@example.com") return HttpResponse.json({ error: "Email already in use" }, { status: 409 });
13 return HttpResponse.json({ id: 1, email }, { status: 201 });
14 })
15);
16beforeAll(() => server.listen());
17afterEach(() => server.resetHandlers());
18afterAll(() => server.close());
19
20test("signs the user up and shows a welcome message", async () => {
21 const user = userEvent.setup();
22 render(<SignupForm />);
23
24 await user.type(screen.getByLabelText(/email/i), "ada@example.com");
25 await user.type(screen.getByLabelText(/password/i), "correct horse battery");
26 await user.click(screen.getByRole("button", { name: /sign up/i }));
27
28 expect(await screen.findByText(/welcome, ada@example.com/i)).toBeInTheDocument();
29});
30
31test("shows the server error when the email is taken", async () => {
32 const user = userEvent.setup();
33 render(<SignupForm />);
34 await user.type(screen.getByLabelText(/email/i), "taken@example.com");
35 await user.type(screen.getByLabelText(/password/i), "whatever123");
36 await user.click(screen.getByRole("button", { name: /sign up/i }));
37 expect(await screen.findByRole("alert")).toHaveTextContent("Email already in use");
38});

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app