What is supertest?
The supertest npm package is a high-level abstraction for testing HTTP, allowing you to test your Node.js HTTP servers using a fluent API. It is built on top of the SuperAgent library and provides a simple and flexible way to assert HTTP responses in your tests.
What are supertest's main functionalities?
HTTP Assertions
This feature allows you to make assertions on the HTTP response, such as the status code, content type, and body content. The code sample demonstrates how to test an Express.js route.
const request = require('supertest');
const express = require('express');
const app = express();
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
describe('GET /user', function() {
it('responds with json', function(done) {
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200, done);
});
});
Integration Testing
Supertest can be used for integration testing of an entire application by sending requests to the routes and asserting the expected responses. The code sample shows how to test for both a successful response and a 404 error.
const request = require('supertest');
const app = require('../app');
describe('Integration Testing', function() {
it('responds to /', function(done) {
request(app)
.get('/')
.expect(200, done);
});
it('404 everything else', function(done) {
request(app)
.get('/foo/bar')
.expect(404, done);
});
});
Asynchronous/Await Support
Supertest works with async/await syntax, allowing for cleaner and more readable asynchronous test code. The code sample demonstrates an asynchronous test using async/await.
const request = require('supertest');
const app = require('../app');
describe('Async/Await Support', function() {
it('responds to /', async () => {
await request(app)
.get('/')
.expect(200);
});
});
Other packages similar to supertest
axios
Axios is a promise-based HTTP client for the browser and Node.js. While it is not specifically designed for testing like supertest, it can be used in combination with testing frameworks to make HTTP requests and assert responses.
nock
Nock is an HTTP server mocking and expectations library for Node.js. Unlike supertest, which performs integration tests against an actual server, nock allows you to test modules that perform HTTP requests by mocking the responses, without having to have an actual server running.
chai-http
Chai-http is a plugin for the Chai assertion library that enables HTTP assertions. Similar to supertest, it allows for testing HTTP servers, but it is designed to be used with the Chai assertion library, providing a different syntax and additional assertion capabilities.
SuperTest
HTTP assertions made easy via super-agent.
About
The motivation with this module is to provide a high-level abstraction for testing
HTTP, while still allowing you to drop down to the lower-level API provided by super-agent.
Example
You may pass an http.Server
, or a Function
to request()
- if the server is not
already listening for connections then it is bound to an ephemeral port for you so
there is no need to keep track of ports.
SuperTest works with any test framework, here is an example without using any
test framework at all:
var request = require('./')
, express = require('express');
var app = express();
app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end(function(err, res){
if (err) throw err;
});
Here's an example with mocha, note how you can pass done
straight to any of the .expect()
calls:
describe('GET /users', function(){
it('respond with json', function(done){
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
})
})
Anything you can do with superagent, you can do with supertest - for example multipart file uploads!
request(app)
.post('/')
.attach('test/fixtures/homeboy.jpg', 'avatar')
...
API
You may use any super-agent methods,
including .write()
, .pipe()
etc and perform assertions in the .end()
callback
for lower-level needs.
.expect(status[, fn])
Assert response status
code.
.expect(status, body[, fn])
Assert response status
code and body
.
.expect(body[, fn])
Assert response body
text with a string, regular expression, or
parsed body object.
.expect(field, value[, fn])
Assert header field
value
with a string or regular expression.
.end(fn)
Perform the request and invoke fn(err, res)
.