Supertest
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
-
Error: expected 200 "OK", got 404 "Not Found"
cause The tested API endpoint returned a 404 status code (or another unexpected non-2xx status) which was not explicitly expected by `.expect()`.fixVerify that the API route is correctly implemented and accessible. If a 404 (or other non-2xx) is expected, add `.expect(404)` to your Supertest chain. -
TypeError: app.address is not a function
cause The `request()` function was called with an argument that is not a valid `http.Server` instance or an Express/Koa application function.fixEnsure you are passing either an instance of an `http.Server` (e.g., `http.createServer(app)`) or a valid application function (e.g., `express()`) directly to `request()`. -
ReferenceError: request is not defined
cause The `supertest` module was not correctly imported or required in the test file.fixAdd `import request from 'supertest';` (for ESM) or `const request = require('supertest');` (for CommonJS) at the top of your test file. -
Error: done() called multiple times
cause The asynchronous `done()` callback for a test was invoked more than once, often due to calling it in both an `.expect()` assertion and the `.end()` callback, or multiple times within a single callback.fixEnsure `done()` is called only once per test. If passing `done` to `.expect()`, omit it from `.end()`. If using `.end()`, only call `done(err)` or `done()` there.
Warnings
- gotcha Supertest (via SuperAgent) treats non-2xx HTTP responses as errors and passes them to the callback's first argument, unless you explicitly assert the expected status code using `.expect(statusCode)`.
- gotcha When using the `.end()` method, failed `.expect()` assertions will not throw errors directly. Instead, they will be returned as the `err` argument to the `.end()` callback.
- breaking Supertest v7.0.0 and later no longer support Node.js versions earlier than 14.16.0.
- gotcha Automatic server closing behavior, which was present in some earlier v7 versions, was reverted in v7.1.3. This means you are responsible for managing the server's lifecycle in your tests.
Install
-
npm install supertest -
yarn add supertest -
pnpm add supertest
Imports
- request
import request from 'supertest';
Quickstart
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);
});