0
0
Expressframework~5 mins

Integration vs unit test decision in Express

Choose your learning style9 modes available
Introduction

Testing helps find bugs early. Unit tests check small parts alone. Integration tests check parts working together.

When you want to check if a single function or route works correctly by itself.
When you want to verify that multiple parts like routes, database, and middleware work together.
When you fix a bug in one function and want to quickly confirm it works.
When you add a new feature that involves several parts interacting.
When you want to catch errors caused by how parts connect, not just inside one part.
Syntax
Express
Unit test example:
describe('functionName', () => {
  it('does something', () => {
    // test code here
  });
});

Integration test example:
describe('API route', () => {
  it('responds with data', async () => {
    // test code here
  });
});

Unit tests focus on one small piece, like a single function or module.

Integration tests check how multiple parts work together, like routes and database.

Examples
This is an integration test checking the /users route returns a list.
Express
const request = require('supertest');
const app = require('./app');

describe('GET /users', () => {
  it('returns list of users', async () => {
    const response = await request(app).get('/users');
    expect(response.status).toBe(200);
    expect(Array.isArray(response.body)).toBe(true);
  });
});
This is a unit test checking the sum function adds numbers correctly.
Express
const { sum } = require('./math');

describe('sum function', () => {
  it('adds two numbers', () => {
    expect(sum(2, 3)).toBe(5);
  });
});
Sample Program

This example shows both a unit test for a simple function and an integration test for an Express route using that function.

Express
const express = require('express');
const request = require('supertest');

// Simple function to test
function greet(name) {
  return `Hello, ${name}!`;
}

// Express app with one route
const app = express();
app.get('/greet/:name', (req, res) => {
  res.send({ message: greet(req.params.name) });
});

// Unit test for greet function
describe('greet function', () => {
  it('returns greeting message', () => {
    expect(greet('Alice')).toBe('Hello, Alice!');
  });
});

// Integration test for /greet/:name route
describe('GET /greet/:name', () => {
  it('responds with greeting message', async () => {
    const response = await request(app).get('/greet/Bob');
    expect(response.status).toBe(200);
    expect(response.body).toEqual({ message: 'Hello, Bob!' });
  });
});
OutputSuccess
Important Notes

Unit tests run fast and help find bugs in small parts.

Integration tests take longer but catch problems in how parts work together.

Use both types to keep your app reliable and easier to fix.

Summary

Unit tests check one small piece alone.

Integration tests check multiple parts working together.

Choose unit tests for quick checks, integration tests for full flow checks.