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 install -D supertest
yarn add -D supertest
pnpm add -D supertest
Import
import request from 'supertest';
Quick Example
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
| Package | supertest |
| Category | Testing |
| Weekly Downloads | 6M+ |
| License | MIT |
| Install | npm install -D supertest |
Related Packages
SuperAgent is a progressive HTTP request library for Node.js and the browser that provides a flexibl…
Jest is a delightful JavaScript testing framework with a focus on simplicity, created by Facebook. I…
Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing…
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
Nock is an HTTP server mocking and expectations library for Node.js that intercepts outgoing HTTP re…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.