Introduction:
Supertest is a Node.js library that you can use to test your API. It extends another library called Superagent, which is a JavaScript HTTP client for Node.js and browsers. Testers and developers can use SuperTest as a standalone library or with JavaScript testing frameworks like Mocha, Jest, and Jasmine.
SuperTest, SuperAgent driven library for testing HTTP servers. It was authored by TJ Holowaychuk. It currently has around 11,992 stars, 768 issues, 73 maintainers and forked by 746 projects.
SuperTest’s Features:
- HTTP Assertions
- Asynchronous
- Promise support
Pre-requisite:
- The latest version of Node.js
- The latest version of NPM
Installation
Firstly, create a new project inside an empty directory and initialize it by running this command to create a default package.json file.
npm init -y
Once your project is initialized, you can use the following commands to set up the latest versions of the SuperTest, Mocha, and Chai libraries.
npm install –save-dev supertest mocha chai
Here I’m using mocha and chai but if you don’t like Mocha, there are similar frameworks like Jest or Jasmine. If Chai isn’t your cup of tea, other assertion libraries like should.js also work equally well.
That’s all you need to start creating automated tests for your API.
Syntax
A request can be initiated by invoking the appropriate method on the request object, then calling .then() (or.end()) to send the request and in .set() we set the HTTP header attributes. For example, a simple GET request syntax looks like this:

Create [POST]:
First of all, we need to import the Supertest library and expect the library from chai to set up and start its respective functions in our tests. Headers for the request are set by using the set function.
It’s very simple to send a Post request with the test data by using the request function and then calling the post function and after that send function sends the request body. Then the function sends the request in response, body. Once the post request is sent successfully, all the data received in the response is validated using the expect function.
The data which we create asserted against the actual data coming in response, which assures that what we sent in post request is actually working or not from chai library.

Read [GET]
Similarly, you can send a Get request by calling the Get function with the request function.
Once the get request is sent successfully, data is received in the response and status code 200 for validation using the expect function from the chai library.

Update [PUT]
For update, we use put request while this request we need to send the full body in the request like “name”, “gender”, ”email”, ”status”.
Here we are updating user/100 which we created previously. Once the Put request is sent successfully, assertions are done using the expect function from chai library, and values are compared using the expected property from data with actual values coming in response and status code 200.

Delete [DELETE]
Delete testing step is the easiest test compared to other tests, where we check by deleting users by delete request. We put assertions to check the status code as the data would be deleted once this request is sent. For a successful request, we expect status code 204.

Pros
- It can integrate nicely with other testing frameworks
- One of the nice things about SuperTest is that it can run tests without any additional tools.
- Supertest is great for implementing test assertions for HTTP API systems for both REST and GraphQL based backend middleware.
- It has a fluent API
- It makes HTTP assertions easy
- Can be mixed with different testing suites like Chai.js and Mocha.
- Old style callbacks are also supported.
Cons
- It doesn’t work on browsers.
- Poor Documentation.