Skip to content
Latchkey

How to Run REST API Tests With supertest in CI

supertest wraps your Express app and sends requests in-process, so no separate server needs to be running.

Import the app and pass it to request(app), then chain .expect() on status and body. Run under Jest so a JUnit reporter can emit results.

Test

test/users.test.js
const request = require('supertest');
const app = require('../src/app');

describe('users API', () => {
  it('creates a user', async () => {
    const res = await request(app)
      .post('/users')
      .send({ name: 'ci' })
      .expect(201);
    expect(res.body.id).toBeDefined();
  });
});

Workflow

.github/workflows/ci.yml
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm test -- --reporters=default --reporters=jest-junit
        env:
          JEST_JUNIT_OUTPUT: results/jest.xml

Gotchas

  • Export the app without calling listen() so supertest can bind an ephemeral port itself.
  • Reset any in-memory or test database state between tests to keep them independent.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →