Lesson 22 / 23

Testing Routes

supertest drives real requests against your app in-process.

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');
});