API Reference

Supertest

HTTP assertion library that drives Express, Koa, or plain Node servers in tests without keeping a port open between cases.

Official Documentation ↗
CompatibilityThis documentation covers version 7.3.1. Check the official docs for the latest API surface.

Installation

npmnpm install -D supertest
pnpmpnpm add -D supertest
yarnyarn add -D supertest

Methods

Supertest exercises HTTP handlers from the outside. It sends real requests through the framework, asserts on status, headers, and body, and closes the listener when the request resolves, so route, middleware, and serialization behavior are all covered by the same test.

Overview

The library builds on superagent and adds server bootstrapping plus fluent assertions. It works with Express, Koa, Connect-style handlers, and raw http.Server instances. Because requests travel through the real middleware stack, tests catch missing body parsers, wrong status codes, and malformed JSON that a direct function call would miss.

Installation

Install Supertest as a development dependency and pair it with your test runner, such as Vitest, Jest, or node:test. TypeScript projects should also add @types/supertest. Keep the application factory separate from the listen call so tests can mount the app without occupying a fixed port.

Core API walkthrough

request(app) creates the client and an ephemeral listener. .send writes a JSON or string body. .expect asserts status codes, response headers, and parsed bodies, and reports the whole response when an assertion fails. .set and .query add headers or query parameters for auth and pagination scenarios. request.agent(app) preserves cookies across requests, which is how session login flows are verified.

Pitfalls

Forgetting to return or await the request leaves assertions running after the test ends. Reusing one server across suites can leak state, so create the app per test file. Fastify apps must be ready before the request is issued. Cookie assertions need an agent; a plain request drops the Set-Cookie header between calls.

Verification

Start with a failing assertion to confirm the suite actually awaits the response, then assert status, content type, and one body field per endpoint. Run the suite in watch mode while editing a handler to see failures immediately, and keep a 404 and a validation-error case so error paths are covered too.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/ladjs/supertest@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:43.489Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/ladjs/supertest@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:43.489Z.

FAQ

Does Supertest start a real HTTP server?
When you pass an app, Supertest binds an ephemeral port for each request and closes it afterward. Passing an already-listening http.Server reuses that server instead.
How do I test multipart file uploads?
Chain .attach('avatar', 'fixtures/avatar.png') on the request. The field name must match the upload middleware configuration, and the file path is resolved relative to the test file.
Can I use Supertest with Fastify or Koa?
Yes, but the app must be ready first. For Fastify call await app.ready() and pass app.server; for Koa pass the app instance or its callback.