0
0
Node.jsframework~5 mins

Integration testing patterns in Node.js

Choose your learning style9 modes available
Introduction

Integration testing patterns help check if different parts of your app work well together. They find problems that unit tests might miss.

When you want to test how your database and server code work together.
When you need to verify that API endpoints return correct data.
When you want to check if multiple modules communicate properly.
When you want to catch bugs caused by interaction between components.
When you want to test real workflows instead of isolated functions.
Syntax
Node.js
describe('Feature or module', () => {
  beforeAll(async () => {
    // setup code like connecting to DB
  });

  afterAll(async () => {
    // cleanup code like closing DB connection
  });

  test('should do something', async () => {
    const result = await someIntegrationFunction();
    expect(result).toBe(expectedValue);
  });
});

describe groups related tests.

beforeAll and afterAll help prepare and clean up resources.

Examples
Tests an API endpoint to check if it returns a list of users with status 200.
Node.js
describe('User API', () => {
  test('GET /users returns list', async () => {
    const response = await request(app).get('/users');
    expect(response.status).toBe(200);
    expect(Array.isArray(response.body)).toBe(true);
  });
});
Sets up and tears down a database connection, then tests saving and retrieving data.
Node.js
describe('Database connection', () => {
  beforeAll(async () => {
    await db.connect();
  });

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

  test('should save and retrieve data', async () => {
    await db.save({ id: 1, name: 'Test' });
    const data = await db.find(1);
    expect(data.name).toBe('Test');
  });
});
Sample Program

This example creates a simple Express app with one endpoint. The integration test checks if the endpoint returns the expected user list.

Node.js
import request from 'supertest';
import express from 'express';

const app = express();
app.use(express.json());

const users = [{ id: 1, name: 'Alice' }];

app.get('/users', (req, res) => {
  res.json(users);
});

// Integration test
import { describe, test, expect } from '@jest/globals';

describe('User API integration test', () => {
  test('GET /users returns users list', async () => {
    const response = await request(app).get('/users');
    expect(response.status).toBe(200);
    expect(Array.isArray(response.body)).toBe(true);
    expect(response.body.length).toBe(1);
    expect(response.body[0].name).toBe('Alice');
  });
});
OutputSuccess
Important Notes

Use real or test databases to avoid messing with production data.

Keep tests isolated to avoid side effects between tests.

Run integration tests after unit tests for faster feedback.

Summary

Integration testing patterns help check if parts of your app work together correctly.

Use setup and cleanup hooks to manage resources like databases.

Test real workflows like API calls or database operations.