supertest
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
methods | 1.1.2 | 2.42 kB | MIT | prod | |
superagent | 8.1.2 | 155.11 kB | MIT | prod |
SuperTest is a high-level abstraction module for testing HTTP. It's driven by SuperAgent and provides various assertions to be used when testing your HTTP server. It adds simplicity to your tests while still providing the ability to drop down to the lower-level API provided by SuperAgent if needed.
To integrate and use SuperTest, you’ll first need to install it as an npm module and then save it as a development dependency using the command npm install supertest --save-dev
. Once installed, it can be called within your code using require('supertest')
.
Here’s an example of SuperTest in action without using any test framework:
const request = require('supertest');
const express = require('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) throw err;
});
For the case of enabling http2 protocol, you simply append an options to request
or request.agent
as shown below:
request(app, { http2: true })
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200)
.end(function(err, res) {
if (err) throw err;
});
request.agent(app, { http2: true })
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200)
.end(function(err, res) {
if (err) throw err;
});
SuperTest is also compatible with any test frameworks. An example usage with Mocha is shown below:
describe('GET /user', function() {
it('responds with json', function(done) {
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
For your reference, you can always resort to the official SuperTest documentation hosted on Github. Make use of these extensive docs to further explore code examples and method descriptions to benefit your project's HTTP testing.