Supertest offers a fluent API for asserting HTTP behavior against Node.js servers during automated tests. It builds on superagent so you can express status, headers, and body expectations in code.
Overview
Supertest is a Node.js library for testing HTTP servers with high-level request helpers while still allowing lower-level superagent calls when needed. You install it as a development dependency and attach it to an Express-style app, generic HTTP server, or URL.
The request helper binds ephemeral ports when the server is not already listening, shares one server across requests from the same app, and works with common test runners or plain callbacks, promises, and async functions. Options exist to exercise HTTP/2.
Chained expect methods assert status codes, response bodies, and headers, including custom functions. Cookie helpers verify set, reset, and renewal semantics. Multipart uploads and other superagent features remain available.
The design goal is concise HTTP assertions for integration tests without manually tracking ports or reimplementing superagent behavior.
Key features
- Fluent HTTP expectations
- Ephemeral port binding
- Works with any test runner
- Promise and async support
- Header and body matchers
- Cookie assertion helpers
- HTTP/2 request option
- Superagent compatibility
Use cases
- API integration tests
- Express route testing
- HTTP header assertions
- Cookie behavior checks
- Multipart upload tests