Lesson 24 / 25

Testing Routes

Test Express routes in-process with supertest by exporting the app without calling listen.

Exercise the HTTP layer

supertest wraps your Express app and lets tests fire real HTTP requests at it — no need to actually bind a port.

A supertest example

Export app without calling listen() in it, so tests import it directly.

import request from 'supertest';
import app from '../app.js';

test('GET /health returns ok', async () => {
  const res = await request(app).get('/health');
  expect(res.status).toBe(200);
  expect(res.body.status).toBe('ok');
});