Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

supertest

Package Overview
Dependencies
Maintainers
6
Versions
69
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.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7M
increased by0.06%
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

FAQs

Package last updated on 24 Apr 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc