← Open Source
forwardemail

supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

ApplicationsCodingJavaScript
Open on GitHub
Momentum
+1stars in 24 hours+0.0%
14.4k
Stars
786
Forks
+0
This week
92
Contributors
Created 2012-06-27 · Updated 2026-10-15 · #4378 today
Top developers

forwardemail/supertest is ranked 4,378 with 14,402 stars.

Same-category comparison

forwardemail/supertest is ranked 4,378 among these 8 peers; ai-shifu/ChatALL is ranked 4,369.

Same-category comparison
NameRankMetrics
rowboatlabs/rowboat4,359
Language:
TypeScript
Stars:
18,024
Forks:
1,805
Description:
AI coworker with memory and collaboration.
influxdata/telegraf4,361
Language:
Go
Stars:
17,864
Forks:
5,844
Description:
Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.
microsoft/data-formulator4,365
Language:
Python
Stars:
17,567
Forks:
1,728
Description:
🪄 Data Formulator is an interactive AI-powered data analysis system makes it easy to connect, explore and visualize data.
ai-shifu/ChatALL4,369
Language:
JavaScript
Stars:
16,510
Forks:
1,723
nearai/ironclaw4,387
Language:
Rust
Stars:
12,658
Forks:
1,482
Description:
IronClaw is an Agent OS focused on privacy, security and extensibility.
infracost/infracost4,389
Language:
Go
Stars:
12,565
Forks:
698
Description:
Cloud cost intelligence for engineers, AI coding agents, and CI/CD 💰📉 Shift FinOps Left.
datalab-to/chandra4,390
Language:
Python
Stars:
12,502
Forks:
1,258
Description:
OCR model that handles complex tables, forms, handwriting with full layout.
TheR1D/shell_gpt4,393
Language:
Python
Stars:
12,291
Forks:
980
Description:
A command-line productivity tool powered by AI large language models like GPT-5, will help you accomplish your tasks faster and more efficiently.
Key parameters and specifications
Key parameters and specifications
ParameterValue
LanguageJavaScript
CategoryApplications
SubcategoryCoding
KeywordsAutomated testing
Stars14,402
Forks786
Contributors92
Created date2012-06-27
Updated date2026-10-15
Stars in 1 day1
Stars in 7 days0
Rank4,378
Previous rank7,164
Rank change2,786
7-day stars rank11,642
Previous 7-day stars rank11,701
7-day stars rank change59
Total stars rank1,018
Previous total stars rank1,017
Total stars rank change-1
README summary

supertest HTTP assertions made easy via superagent. 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 superagent. Getting Started Install supertest as an npm module and save it to your package. json file as a development dependency: Once installed it can now be referenced by simply calling Example You may pass an , or a to - 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: To enable http2 protocol, simply append an options to or : Here's an example with mocha, note how you can pass straight to any of the calls: You can use method to pass HTTP username and password in the

same way as in the superagent: 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. If you are using the method 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 to , as follows: You can also use promises: Or async/await syntax: Expectations are run in the order of definition. This characteristic can be used to modify the response body or headers before executing an assertion. Anything you can do with superagent, you can do with supertest - for example multipart file uploads! 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 call. Here's an example with mocha that shows how to persist a request and its cookies: There is another example that is introduced by the file agency. js Here is an example where 2 cookies are set on the request. API You may use any superagent methods, including , etc and perform assertions in the callback for lower-level needs. expect(status[, fn]) Assert response code. 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. end(fn) Perform the request and invoke . Cookies Here is an example of using the and cookie assertions: It is also possible to chain assertions: Cookie assertions Functions and methods are chainable. cookies([secret

], [asserts]) Get assertion function for super-test method. *Arguments* - String or array of strings. set(expects, [assert]) Assert that cookie

Frequently asked questions

How many stars and forks are recorded?

forwardemail/supertest has 14,402 stars and 786 forks.

What is the repository rank?

forwardemail/supertest is ranked 4,378.

What language and category are recorded?

forwardemail/supertest has 14,402 stars, uses JavaScript and belongs to Applications.

Which repository is closest in rank?

forwardemail/supertest is compared with ai-shifu/ChatALL, ranked 4,369 with 16,510 stars.

README

supertest

HTTP assertions made easy via superagent. Maintained for Forward Email and Lad.

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 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 require('supertest');

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:

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

To enable http2 protocol, simply append an options to request or request.agent:

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

Here's an example with mocha, note how you can pass done straight to any of the .expect() calls:

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 auth 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. .expect(302)).

If you are using the .end() method .expect() assertions that fail will not throw - they will return the assertion as an error to the .end() callback. In order to fail the test case, you will need to rethrow or pass err to done(), 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);
        return 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 => {
         expect(response.body.email).toEqual('foo@bar.com');
      })
  });
});

Or async/await syntax:

describe('GET /users', function() {
  it('responds with json', async function() {
    const response = await request(app)
      .get('/users')
      .set('Accept', 'application/json')
    expect(response.headers["content-type"]).toMatch(/json/);
    expect(response.status).toEqual(200);
    expect(response.body.email).toEqual('foo@bar.com');
  });
});

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('user.name 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) {
        res.body.id = 'some fixed id';
        res.body.name = res.body.name.toLowerCase();
      })
      .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')
  .field('complex_object', '{"attribute": "value"}', {contentType: 'application/json'})
  .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 Test is created per request.VERB() call.

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).to.be.equal('hey');
    return done();
  });

API

You may use any superagent 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.

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

.end(fn)

Perform the request and invoke fn(err, res).

Cookies

Here is an example of using the set and not cookie assertions:

// setup super-test
const request = require('supertest');
const express = require('express');
const cookies = request.cookies;

// setup express test service
const app = express();

app.get('/users', function(req, res) {
  res.cookie('alpha', 'one', { domain: 'domain.com', path: '/', httpOnly: true });
  res.send(200, { name: 'tobi' });
});

// test request to service
request(app)
  .get('/users')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  // assert 'alpha' cookie is set with domain, path, and httpOnly options
  .expect(cookies.set({ name: 'alpha', options: ['domain', 'path', 'httponly'] }))
  // assert 'bravo' cookie is NOT set
  .expect(cookies.not('set', { name: 'bravo' }))
  .end(function(err, res) {
    if (err) {
      throw err;
    }
  });

It is also possible to chain assertions:

cookies.set({/* ... */}).not('set', {/* ... */})

Cookie assertions

Functions and methods are chainable.

cookies([secret], [asserts])

Get assertion function for super-test .expect() method.

Arguments

  • secret - String or array of strings. Cookie signature secrets.
  • asserts(req, res) - Function or array of functions. Failed custom assertions should throw.

.set(expects, [assert])

Assert that cookie and options are set.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • options - Optional array of options.
  • assert - Optional boolean "assert true" modifier. Default: true.

.reset(expects, [assert])

Assert that cookie is set and was already set (in request headers).

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
  • assert - Optional boolean "assert true" modifier. Default: true.

.new(expects, [assert])

Assert that cookie is set and was NOT already set (NOT in request headers).

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
  • assert - Optional boolean "assert true" modifier. Default: true.

.renew(expects, [assert])

Assert that cookie is set with a strictly greater expires or max-age than the given value.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • options - Object of options. use one of two options below
    • options.expires - String UTC expiration for original cookie (in request headers).
    • options.max-age - Integer ttl in seconds for original cookie (in request headers).
  • assert - Optional boolean "assert true" modifier. Default: true.

.contain(expects, [assert])

Assert that cookie is set with value and contains options.

Requires cookies(secret) initialization if cookie is signed.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • value - Optional string unsigned value of cookie.
    • options - Optional object of options.
    • options.domain - Optional string domain.
    • options.path - Optional string path.
    • options.expires - Optional string UTC expiration.
    • options.max-age - Optional integer ttl, in seconds.
    • options.secure - Optional boolean secure flag.
    • options.httponly - Optional boolean httpOnly flag.
  • assert - Optional boolean "assert true" modifier. Default: true.

.not(method, expects)

Call any cookies assertion method with "assert true" modifier set to false.

Syntactic sugar.

Arguments

  • method - String method name. Arguments of method name apply in expects.
  • expects - Object or array of objects.
    • name - String name of cookie.
    • value - Optional string unsigned value of cookie.
    • options - Optional object of options.

Notes

Inspired by api-easy minus vows coupling.

License

MIT

Nearby in the ranking
Similar items