🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

supertest

Package Overview
Dependencies
Maintainers
6
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

supertest

SuperAgent driven library for testing HTTP servers

7.1.1
latest
Source
npm
Version published
Weekly downloads
7.9M
9.63%
Maintainers
6
Weekly downloads
 
Created

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

Keywords

bdd

FAQs

Package last updated on 12 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts