Supertest

7.2.2 · active · verified Sat Apr 18

Supertest is an actively maintained, SuperAgent-driven library for making HTTP assertions easy when testing HTTP servers. The current stable version is 7.2.2. The project receives regular updates and bug fixes, with new versions released periodically to incorporate dependency bumps and address issues.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic GET request to an Express application, asserting response headers, content length, and status code without a dedicated test framework.

import request from 'supertest';
import express from 'express';

const app = express();

app.get('/user', function(req, res) {
  res.status(200).json({ name: 'john' });
});

request(app)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  .end(function(err, res) {
    if (err) {
      console.error(err);
      // In a test environment, you would typically use a test runner's fail method here
      // e.g., done(err) for Mocha
    }
    console.log('Test successful:', res.body);
  });

view raw JSON →