0
0
NestJSframework~5 mins

Integration testing in NestJS

Choose your learning style9 modes available
Introduction

Integration testing checks if different parts of your NestJS app work well together. It helps find problems between modules or services.

When you want to test how controllers and services work together.
To verify database operations with real or test databases.
When you need to check if middleware and pipes interact correctly.
To ensure external APIs or modules integrate properly.
Before deploying to catch issues between components.
Syntax
NestJS
describe('YourModule Integration Test', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [YourModule],
    }).compile();

    app = moduleRef.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it('should test something', () => {
    // your test code here
  });
});

Use Test.createTestingModule to build a test module with real imports.

Call app.init() to start the NestJS app before tests.

Examples
This tests the /cats route to ensure it returns status 200.
NestJS
describe('CatsController Integration', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [CatsModule],
    }).compile();

    app = moduleRef.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it('GET /cats returns 200', () => {
    return request(app.getHttpServer())
      .get('/cats')
      .expect(200);
  });
});
This tests the UserService to check if it creates a user correctly.
NestJS
describe('UserService Integration', () => {
  let service: UserService;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [UserModule],
    }).compile();

    service = moduleRef.get<UserService>(UserService);
  });

  it('should create a user', async () => {
    const user = await service.create({ name: 'Anna' });
    expect(user.name).toBe('Anna');
  });
});
Sample Program

This integration test starts the full NestJS app from AppModule. It sends a GET request to the root URL and checks if the response is 'Hello World!'. This shows how to test the app's real HTTP behavior.

NestJS
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';

describe('AppController (Integration)', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it('/ (GET) should return Hello World', () => {
    return request(app.getHttpServer())
      .get('/')
      .expect(200)
      .expect('Hello World!');
  });
});
OutputSuccess
Important Notes

Use supertest to simulate HTTP requests in integration tests.

Integration tests are slower than unit tests but catch more real issues.

Close the app with app.close() to free resources after tests.

Summary

Integration testing checks how NestJS parts work together.

Use Test.createTestingModule with real modules and app.init().

Test HTTP routes with supertest for realistic behavior.