Integration Testing Explained: When Unit Tests Aren’t Enough

Integration testing explained in plain terms starts with a simple observation: a unit test checks one piece of code in isolation, but real software breaks where pieces meet. A function can pass every unit test and still fail when it talks to a database, calls another service, or reads a file. Integration tests cover that ground. They run two or more parts of the system together and check that the combined behavior is correct.

Take a checkout flow. The price calculator works, the payment client works, and the order saver works. Put them together and the total might be counted in cents while the payment client expects dollars, or the order might save before the payment confirms. No unit test catches that. An integration test that runs the whole flow through real wiring does.

Integration Testing Explained: What It Covers and What It Does Not

An integration test sits between unit tests and end-to-end tests. Unit tests check a function with its dependencies replaced. End-to-end tests drive the whole product the way a user would, through the browser or the public API. Integration tests sit in the middle: they run real modules together, with real databases or real HTTP calls to a local service, but they skip the browser and the full deployment.

That middle position is the point. Integration tests are slower than unit tests because they touch disks and networks, but faster and less fragile than end-to-end tests because they skip the UI layer. A healthy suite has many unit tests, a smaller set of integration tests, and a handful of end-to-end tests. That shape is often called the integration testing pyramid, and it works because each layer catches a different kind of failure.

Integration Test vs Unit Test: Where the Line Falls

The difference is not the size of the test file. It is what is real and what is faked. A unit test replaces the database with a fake, while an integration test uses a real database, even if that database runs in a throwaway container. A unit test for a route handler might pass a fake request object, while an integration test starts the server and sends a real HTTP request.

Here is a unit test shape, then the integration version of the same feature, so the contrast is concrete:

// Unit test: the repository is replaced with a fake
test("createUser rejects a duplicate email", () => {
  const fakeRepo = { findByEmail: () => ({ id: 1 }) };
  const service = new UserService(fakeRepo);
  expect(() => service.createUser("[email protected]")).toThrow("Email taken");
});
// Integration test: real HTTP request, real database
test("POST /users rejects a duplicate email", async () => {
  await db.query("INSERT INTO users (email) VALUES ('[email protected]')");
  const res = await request(app)
    .post("/users")
    .send({ email: "[email protected]", name: "Ana" });
  expect(res.status).toBe(409);
  expect(res.body.error).toMatch(/email/i);
});

The first test proves the service logic. The second proves the route, the validation, the database constraint, and the error format all agree. Only the second fails when the route forgets to map the error to a 409 status. When you build a REST API with Node.js and Express, integration tests like the second one are the fastest way to catch wiring mistakes before the API ships.

The Main Types of Integration Tests

Not every integration test looks the same. The most common shapes cover different seams of a system, and a project usually needs more than one.

Module integration

Two or more modules inside one process run together: a service and its repository, a parser and its validator. The database may still be faked at this level, but the modules under test are real and their interaction is what gets checked.

Database integration

Code runs against a real database. These tests catch wrong column names, missing migrations, and bad queries. They need a test database that can be reset between runs, usually by wiping tables or rolling back a transaction after each test.

API integration

Tests send real HTTP requests to a running server and check status codes, headers, and response bodies. They catch routing errors, auth mistakes, and serialization bugs, and they are the most common integration tests in web projects because they test the exact surface clients use.

Service-to-service integration

One service calls another over the network, often with a test double standing in for the other side. These tests check request formats, timeouts, retries, and error handling when the other service is slow or returns an error.

How to Write an Integration Test That Stays Fast and Stable

Integration tests get a bad name when they are slow and flaky. Most of that pain comes from a few fixable habits.

  • Reset state between tests. Each test should start from a known database state. Truncate tables or wrap each test in a transaction that rolls back. A test that depends on data left behind by another test fails when run alone.
  • Seed only what the test needs. A giant shared seed file makes tests slow and hard to read. Create the rows the test needs inside the test itself, with a small helper.
  • Keep external calls under control. A test that calls a live payment provider fails when that provider is down. Point those calls at a local stub server or a recorded response.
  • Use one database per test run. Tests that share a database with the development environment corrupt real data sooner or later. Give tests their own database name, created fresh by the test setup.
  • Make failures point at the cause. A test named “user flow works” tells you nothing when it goes red. Name it after the behavior: “POST /users returns 409 for a duplicate email”.

A practical structure for a Node.js project is a tests/integration folder, a setup file that starts a test database and the app, and a helper that creates test data. The best API testing tools for 2026 are useful for exploring endpoints by hand, but automated integration tests keep those endpoints correct after every change.

Test Doubles, Mocks, and Stubs: What to Fake and What to Keep Real

Test doubles are stand-ins for real dependencies: stubs return canned answers, mocks record how they were called, and fakes are small working copies of the real thing. In integration tests the rule is simple: keep real everything your team owns, fake everything you do not control.

Your database, your queue, and your own services stay real. A payment gateway, an email provider, or a weather API gets a stub. That split keeps the test honest about your code while removing the parts that are slow, costly, or out of your hands. The mistake to avoid is faking your own code: if the repository is stubbed, the test no longer checks whether the SQL is right.

Where Integration Tests Fit in CI

Integration tests belong in continuous integration, but they need their own stage. Unit tests run first, in seconds, on every push. Integration tests run next, with services started by the pipeline: a Postgres container, a Redis container, whatever the app needs. The guide to building a GitHub Actions pipeline for Node.js shows how to wire that stage.

Common Mistakes That Make Integration Tests Painful

Mistake What goes wrong Fix
Testing through the UI Slow, brittle, hard to read Test the API layer directly
Shared test data Tests pass in order, fail alone Seed per test, reset between tests
Live third-party calls Random failures, real charges Stub external services locally
One giant test per flow Failure hides the real cause One behavior per test, clear names
No timeout on tests A hung test blocks the pipeline Set per-test and per-suite timeouts

Shared test data is the most common source of flakes, and it is also the easiest to fix once the team agrees that each test owns its setup. Timeouts matter because a test that hangs on a network call can block a whole pipeline; if the suite hangs in CI, a guide to debugging a failing Docker build often helps too, since many integration setups run their test database in a container.

How Many Integration Tests Does a Project Need

There is no fixed number, but a useful rule is to cover every seam once. Every endpoint gets at least one happy-path test and one error-path test. Every database query that is more than a simple lookup gets a test against real data. Every call to another service gets a test for success and a test for failure. That coverage usually lands at one integration test for every five to ten unit tests.

Start with the paths that move money or data: signup, checkout, payment, file upload, password reset. Then add the paths that have broken before. A bug that escaped to production is a gift: write the integration test that would have caught it, and the same bug rarely comes back.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.