Q45: How do you perform integration tests on an Express application using Supertest and Jest?

Difficulty:MediumTopic:Testing

supertest allows testing HTTP endpoints programmatically without binding the application to a real network port.

Supertest Endpoint Integration Test
const request = require('supertest');
const app = require('./app'); // Decoupled express app

describe('GET /api/v1/health', () => {
  it('should return 200 OK with json status', async () => {
    const response = await request(app)
      .get('/api/v1/health')
      .expect('Content-Type', /json/)
      .expect(200);
      
    expect(response.body.status).toBe('UP');
  });
});

Supertest invokes the app's request pipeline directly in memory without taking up local network ports.

Key Takeaways
  • Pass the un-listened app instance directly to request(app).

  • Fast, reliable integration testing without network port conflicts.