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.xmlGotchas
- 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
How to Mock External APIs in CIStub third-party APIs during API tests in GitHub Actions with WireMock, Prism, MSW, or nock, so tests stay de…
How to Give API Tests a Real Database With Service Containers in CIBack API tests with a real Postgres in GitHub Actions using a service container with health checks, so integr…
How to Publish an API Test JUnit Report in CIEmit a JUnit XML report from any API test runner in GitHub Actions and publish it, so passed and failed reque…