Skip to main content

Integration Testing

Integration testing verifies that multiple components of a REST API work together correctly as a complete system.

What Should Be Tested in Integration Testing

AreaDescription
RoutingCorrect endpoint mapping
MiddlewareAuth, validation, logging
DatabaseCRUD operations
Error HandlingEnd-to-end error responses
SerializationJSON ↔ object mapping
HTTP ContractsHeaders, status codes

Example Scenario

REST API Endpint: POST /api/users

Flow Under Test

HTTP Request

Controller

Validation Middleware

Service Layer

Database

HTTP Response

Implementation Example

// user.integration.test.js
const request = require('supertest');
const mongoose = require('mongoose');
const app = require('./app');

beforeAll(async () => {
await mongoose.connect('mongodb://localhost:27017/testdb');
});

afterAll(async () => {
await mongoose.connection.db.dropDatabase();
await mongoose.disconnect();
});

describe("POST /api/users", () => {

it("should create a user in the database", async () => {
const response = await request(app)
.post('/api/users')
.send({ name: "Alice", email: "alice@test.com" });

expect(response.statusCode).toBe(201);
expect(response.body.email).toBe("alice@test.com");
});

});