Skip to main content
Posted On: 26 September 2022

Does Supertest mean superlative test? No, it does not. Let’s explore what it means.

We live in a connected age where applications are interconnected to allow for an easy flow of customer data. For example, a person using a cab-hailing app can pay directly from their bank account courtesy of integration between the two apps. This integration happens through application programming interfaces (API). API facilitates the different applications to talk with each other. Given how important APIs are in the digital-first world, developers and testers cannot undermine the significance of API testing.

API testing allows developers to send API requests to an API and monitor the responses to check if it’s behaving as expected. It checks the overall API’s functionality, such as its reliability, performance, and security.

In addition, every status code, header, and response body must match the API documentation. That’s why API testing is so crucial. In essence, it ensures that the API delivers on what it promises.

There are various ways to perform API testing. However, this article will explore a type of API functional testing called Supertest.

What is Supertest?

Supertest is a Node.js library that allows developers and testers to test the APIs. It enables developers and testers to write automated tests for routes and endpoints. Supertest can be used as a standalone library or with testing frameworks like Mocha and Jest. It provides a high-level abstraction to test HTTP requests. That is, if the developer’s Node.js application is running an HTTP server, they don’t need a running server.

You can make requests using Supertest directly. Besides, Supertest can integrate with other testing frameworks easily without any additional tools. With just a few lines of commands by sending a method like GET, PUT, and POST, Supertest can test any HTTP requests easily.

Why Use Supertest?

Why use supertest-ACCELQ

Most modern software use microservices and thus can function only with HTTP APIs. These HTTP APIs connect the frontend and backend components and third-party APIs for essential features.

With over 2 million weekly downloads, Supertest has become an obvious choice for performing HTTP API testing.

How to Get Started with Supertest?

Step 1: Install the Supertest package

  • Start with installing the project’s dev dependencies. For example, if TypeScript is being added, install the type definitions.
  • After the package installation, set up the test command in the package file.
  • Install the testing framework. It comes in handy in writing automated tests and will enable you to determine if any part of the application has gone wrong.

After installing the package and testing framework, it’s time to create the HTTP request.

Step 2: Create an HTTP request

  • The first way tests an external API or an internal API already deployed and running in the staging environment. All you need to do is provide the URL as a parameter. Use the GET() method. This tells the Supertest to use the HTTP GET verb. Additional methods can be included in the call for setting authentication. Developers and testers can also add custom HTTP headers or body too. Use the END() method to finalize the request and call the API server.
  • Another way of creating an HTTP request is useful if you have built the API with a Node.js framework. In this case, if necessary, the Supertest starts a test server and sends the requests there.

Till this point, we saw how to create the HTTP request. Now let’s look at how to do the actual testing.

Step 3: Set response expectations

  • Start with setting the expectations for a functional API test. You need to confirm that the API responds as expected.
  • Use the expect() method to extend the superagent. It allows you to test several things related to the API response.

The HTTP APIs will return with various status codes, such as the 4xx range that indicates an error and the 5xx range that indicates server errors. A successful API request will return with a status code ranging from 200 to 204, depending upon the information available in the API documentation.

Step 4: Check the response bodies

  • Ensure that the developers and testers don’t just check for the HTTP status codes and headers. That would result in shallow testing or smoke testing. Although it helps check if the API responds, it does not promise thorough testing of specific properties. Response bodies contain information about the output and the success or failure message. That’s why checking for response bodies is essential.
  • Check for response bodies. For example, if a DOG API returns a message and status property, check if they exist or if the API consumers expect specific properties in the JSON object; ensure they are present.
  • There are various ways to check the response bodies built into Supertest. For example, you can pass a string or regular expression to the expect() method. The string should match the whole body, and the regular expression would review parts of it.

Now that we know the basics of installing and performing Supertest, let’s find out what developers and testers can test.

Ready to Get Started?

Let our team experts walk you through how ACCELQ can assist you in achieving a true continuous testing automation

How To Design a Supertest?

  • For internal APIs, test all the exposed endpoints. You can either write a unit test for one endpoint or use multiple API calls to test the workflows involving numerous API calls.
  • For external APIs, test all the endpoints that are being consumed to ensure that they are reliable.

Summing Up

API testing is essential to keep all the integrated applications running. To that end, developers and testers must incorporate Supertest to automate the testing of routes and endpoints. This will allow the developers and testers to reduce errors and ensure that the status codes, headers, and response bodies adhere to the API documentation.

The steps outlined above will enable you to build your first API test with Supertest. Apart from Supertest, there are other ways to carry out automated API testing.

API Automation-ACCELQ

ACCELQ provides comprehensive support to companies to design, automate, and execute API testing. We also offer zero-code API test automation on the cloud that seamlessly integrates with UI testing to ensure that the testing takes place in the same simplified flow.

For a free trial of the API automation solution, contact us.

GEOSLEY-IMG.png

GEOSLEY ANDRADES

Director, Product Evangelist at ACCELQ.

Related Posts

How to improve the test automation-ACCELQBlogTest Automation
5 December 2022

How To Improve Test Automation Outcomes

There are several tips that testing teams have to keep in mind as they embark on the test automation journey. Here are they
Tips for testing in production-ACCELQBlogTesting
2 December 2022

Don’t Do It the Wrong Way – Tips for Testing in Production

Testing in production is post-deployment application testing to verify whether it is serving its intended purpose

Get started on your Codeless Test Automation journey

Talk to ACCELQ Team and see how you can get started.

Close Menu