Author: Rakib Hasan
Senior Software Engineer in Test II
Software Quality / Test Automation
I’ve been playing around with some API automation tools and found an easy solution to automate the rest APIs. Jest with Supertest is an exciting and easy tool for API automation testing using JavaScript. I tried to integrate SuperTest, a library for testing HTTP servers. It seems to be a standard choice for testing our backend API.
In the following discussion, I’ll walk you through the process of seamlessly integrating Supertest with Jest to streamline the testing of your APIs.
Installing Dependencies
As always, the first step we need to take is to install the dependencies we need. You must add the following to your project’s dev dependencies: If you’re working with TypeScript, don’t forget to grab the type definitions too.
I am going to use JavaScript and two libraries that I love: one is a testing framework called Jest, and the other is a library for doing HTTP testing called SuperTest.
Getting Started
Ensure that Node.js is installed on your machine. If you don’t have it yet, download Node.js from here and complete the installation.
Utilize the following command to install Jest, Supertest, and Chai in one go: Chai for assertions.
npm i -D jest supertest chai
That’s it! Once the installation of the required libraries is complete, you are all set to start using them, write the API tests, and perform the assertions.
Create a “tests” folder and create a spec file as ‘sampleAPI.spec.js’ under the tests folder.

First, we must import the supertest library and the expect library from Chai to set up and initiate their respective functions in our tests.
Jest includes describe and it for you in every test file. You don’t have to require them.
- describe lets you wrap many tests together under one umbrella. (It is used for organizing your tests.)
- it lets you run a test.
- expect lets you perform assertions. The test will pass if all assertions pass.
This can be achieved by adding the following statements at the beginning of your test scripts:
GET request
As a sample, let’s try a GET API. My base URL is https://reqres.in, which I’ve hardcoded for simplicity.
SuperTest has a function named `request`, designed for initializing API calls based on the provided parameters. Following this, a straightforward call to the get function simulates a .get() request to the ‘/api/users?page=2’ endpoint.

The async/await keywords ensure the request is asynchronous, allowing us to wait for its completion before asserting that the status code should be 200.
If you do want to set header/headers, you need to use the .set function. You should be able to pass a single JSON object or two parameters for the key and value.
.set('auth-key', 'api_1971') or.set({ ‘API-Key’: ‘foobar’, ‘access_token’: ‘key_login3871’})

POST request
Now, let’s try to create a user and see how we can achieve that with SuperTest. For this test, we want to send the request body {“name”: “morpheus”, “job”: “leader”}. We need to use the .send() function of SuperTest with a post() request to the ‘/api/users’ endpoint and verify its success by asserting the status code to be 201. Finally, we’ll check that the response body matches our expectations.

PUT request
In the same vein, to modify a user, we should send the request body {“name”: “morpheus”, “job”: “leader2”} and employ the .send() function from SuperTest with a .put() request to the ‘/api/users/2’ endpoint. Following this, we verify its success by asserting the status code to be 200. Naturally, we will confirm that the response body aligns with our expectations.

Wrapping up:
That’s pretty much it!
Similarly, you can test the following:
.patch()
.delete()
- And other APIs
SuperTest is a very easy-to-use library for testing REST-based or GraphQL APIs. These are elementary examples, but should help you to begin with. In addition to performing .get() or .put() requests, you can execute other HTTP actions and set authorization tokens, cookies, or custom headers.
For more examples of using the library, check out the SuperTest documentation.
All the code snippets above can be found on my demo for Jest with the SuperTest GitHub repository, so feel free to clone or download it.
In conclusion, I emphasize the significance of dedicating attention to API testing. As quality assurance professionals, let’s adhere to the principles of the testing pyramid, focusing on addressing issues at lower levels to prevent chaos at higher levels, such as the UI.

I trust you found this information valuable. Feel free to share this knowledge. Happy coding.
In a future article, I’ll show you how to make GET and POST requests using Express for testing APIs.

