🧪
Testing6M+/wkMIT

supertest

SuperTest is a high-level HTTP assertion library built on SuperAgent that makes it easy to test Node.js HTTP servers. It provides a fluent API for making HTTP r

Installation

npm
npm install -D supertest
yarn
yarn add -D supertest
pnpm
pnpm add -D supertest

Import

ESM
import request from 'supertest';

Quick Example

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

test('GET /users returns 200', async () => {
  const res = await request(app)
    .get('/users')
    .expect(200)
    .expect('Content-Type', /json/);

  expect(res.body).toHaveLength(3);
});

About supertest

SuperTest is a high-level HTTP assertion library built on SuperAgent that makes it easy to test Node.js HTTP servers. It provides a fluent API for making HTTP requests to your Express, Fastify, Koa, or any Node.js HTTP server and asserting on the response status code, headers, and body without actually starting the server on a network port. SuperTest accepts an http.Server instance or Express app directly and manages the server lifecycle internally, starting and stopping it for each test. This eliminates the need for manual server management and port allocation in tests. The chaining API allows expressing assertions naturally: request(app).get('/users').expect(200).expect('Content-Type', /json/).expect(body => assert(body.length > 0)). SuperTest supports all HTTP methods, custom headers, request body sending, cookie handling, authentication, file uploads, and redirect following. It integrates seamlessly with any test runner including Jest, Mocha, and Vitest. SuperTest is the standard tool for integration testing Express APIs and is frequently used alongside unit testing frameworks to verify route handlers, middleware behavior, and error handling.

Quick Facts

Packagesupertest
CategoryTesting
Weekly Downloads6M+
LicenseMIT
Installnpm install -D supertest

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.