Integration Testing
Verify that modules work correctly together, catching wiring bugs unit tests miss.
What you'll learn
- What integration tests are and the bugs they catch
- How to choose boundaries for integration tests
- Using real vs fake dependencies intentionally
- Keeping integration tests fast and deterministic
Concept
Beyond Unit Tests
Unit tests prove that each function works in isolation, but they cannot prove that the functions fit together. An integration test lets two or more real modules interact and asserts that the collaboration produces the right result. This is where you catch mismatched interfaces, wrong column names, broken serializers, and transaction boundaries — the bugs that appear only when the pieces meet.
Choosing Boundaries
The art of integration testing is choosing where to draw the boundary. A good boundary is a seam where one side is fast and reliable to run for real, and the other side is expensive or non-deterministic. For example, you might let your repository and an in-memory database run for real, but stub the external payment gateway. Test the seams that are most likely to break and cheapest to run.
Real vs Fake Dependencies
Use real dependencies for the parts you want to verify (your own modules, an in-memory or test database) and fakes for the parts you cannot control (third-party APIs, the clock, email delivery). The goal is maximum confidence for minimum cost. A test database that resets between tests gives you real SQL behavior without the slowness of a shared remote database.
Speed and Determinism
Integration tests are slower than unit tests, so keep the suite lean. Reset shared state before each test, run them in parallel when safe, and tag them so developers can run a fast subset locally and the full suite in CI. Never let an integration test depend on leftover data from a previous run — that creates order-dependent flakiness that is miserable to debug.
In Practice
A common pattern is to use an in-memory or test database that resets in a beforeEach hook, giving you real SQL behavior without the latency of a remote server. For external services you cannot run locally — a payment gateway, an email provider — stub the thin adapter that wraps them, and let your own modules interact for real beneath. Watch out for tests that grow slow because they wait on real I/O; if an integration test takes more than a few hundred milliseconds, investigate whether part of it can be pushed down to a unit test. Keep integration tests tagged separately so a developer can run only the fast unit suite during tight inner-loop coding and defer the integration suite to pre-commit or CI. This keeps the local feedback loop fast while preserving the wiring confidence integration tests provide.
Example
// Integration test: UserService + real InMemoryUserRepository
import { describe, it, expect, beforeEach } from "vitest";
import { UserService } from "./UserService";
import { InMemoryUserRepository } from "./UserRepository";
describe("UserService with repository", () => {
let repo: InMemoryUserRepository;
let service: UserService;
beforeEach(() => {
repo = new InMemoryUserRepository(); // real module, fresh state
service = new UserService(repo);
});
it("persists a created user and can look it up", async () => {
const created = await service.create({ name: "Grace", email: "[email protected]" });
const found = await service.findById(created.id);
expect(found?.email).toBe("[email protected]");
});
});
The repository is a real in-memory implementation, so the test exercises the real collaboration between service and storage without touching a slow external database.
Try it
- HTTP Request Tester
Probe the real endpoints your integration tests call.
- JSON Diff
Spot the wiring mismatches your integration tests reveal.
Common mistakes
The mistake
Sharing a database across tests without resetting it
The fix
Truncate or recreate the test database in a beforeEach hook so every test starts from a known, empty state.
The mistake
Stubbing every dependency until the test tests nothing real
The fix
If you replace all collaborators, you have a unit test. Let at least the modules you want to verify interact for real.
Practice Integration Testing
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.