Github url


by visionmedia

visionmedia /supertest

🕷Super-agent driven library for testing node.js HTTP servers using a fluent API.

10.1K Stars 647 Forks Last release: over 1 year ago (v4.0.2) MIT License 351 Commits 45 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


CoverallsBuild StatusDependenciesPRs WelcomeMIT License

HTTP assertions made easy via superagent.


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 superagent.

Getting Started

Install SuperTest as an npm module and save it to your package.json file as a development dependency:

npm install supertest --save-dev

Once installed it can now be referenced by simply calling



You may pass an


, or a



  • 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:

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; });

Here's an example with mocha, note how you can pass


straight to any of the



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); }); });

You can use


method to pass HTTP username and password in the same way as in the superagent:

describe('GET /user', function() { it('responds with json', function(done) { request(app) .get('/user') .auth('username', 'password') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); });

One thing to note with the above statement is that superagent now sends any HTTP error (anything other than a 2XX response code) to the callback as the first argument if you do not add a status code expect (i.e.



If you are using the




assertions that fail will not throw - they will return the assertion as an error to the


callback. In order to fail the test case, you will need to rethrow or pass




, as follows:

describe('POST /users', function() { it('responds with json', function(done) { request(app) .post('/users') .send({name: 'john'}) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) { if (err) return done(err); done(); }); }); });

You can also use promises:

describe('GET /users', function() { it('responds with json', function() { return request(app) .get('/users') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) .then(response =\> { assert(, '[email protected]') }) }); });

Expectations are run in the order of definition. This characteristic can be used to modify the response body or headers before executing an assertion.

describe('POST /user', function() { it(' should be an case-insensitive match for "john"', function(done) { request(app) .post('/user') .send('name=john') // x-www-form-urlencoded upload .set('Accept', 'application/json') .expect(function(res) { = 'some fixed id'; =; }) .expect(200, { id: 'some fixed id', name: 'john' }, done); }); });

Anything you can do with superagent, you can do with supertest - for example multipart file uploads!

request(app) .post('/') .field('name', 'my awesome avatar') .attach('avatar', 'test/fixtures/avatar.jpg') ...

Passing the app or url each time is not necessary, if you're testing the same host you may simply re-assign the request variable with the initialization app or url, a new


is created per



request = request('http://localhost:5555'); request.get('/').expect(200, function(err){ console.log(err); }); request.get('/').expect('heya', function(err){ console.log(err); });

Here's an example with mocha that shows how to persist a request and its cookies:

const request = require('supertest'); const should = require('should'); const express = require('express'); const cookieParser = require('cookie-parser'); describe('request.agent(app)', function() { const app = express(); app.use(cookieParser()); app.get('/', function(req, res) { res.cookie('cookie', 'hey'); res.send(); }); app.get('/return', function(req, res) { if (req.cookies.cookie) res.send(req.cookies.cookie); else res.send(':(') }); const agent = request.agent(app); it('should save cookies', function(done) { agent .get('/') .expect('set-cookie', 'cookie=hey; Path=/', done); }); it('should send cookies', function(done) { agent .get('/return') .expect('hey', done); }); });

There is another example that is introduced by the file agency.js

Here is an example where 2 cookies are set on the request.

agent(app) .get('/api/content') .set('Cookie', ['nameOne=valueOne;nameTwo=valueTwo']) .send() .expect(200) .end((err, res) =\> { if (err) { return done(err); } expect(res.text)'hey'); return done(); });


You may use any superagent methods, including




etc and perform assertions in the


callback for lower-level needs.

.expect(status[, fn])

Assert response



.expect(status, body[, fn])

Assert response


code and



.expect(body[, fn])

Assert response


text with a string, regular expression, or parsed body object.

.expect(field, value[, fn])

Assert header


with a string or regular expression.

.expect(function(res) {})

Pass a custom assertion function. It'll be given the response object to check. If the check fails, throw an error.

request(app) .get('/') .expect(hasPreviousAndNextKeys) .end(done); function hasPreviousAndNextKeys(res) { if (!('next' in res.body)) throw new Error("missing next key"); if (!('prev' in res.body)) throw new Error("missing prev key"); }


Perform the request and invoke

fn(err, res)



Inspired by api-easy minus vows coupling.



We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.