Skip to main content

What Should You Know About Supertest?

Supertest-ACCELQ
Posted On: 26 November 2022

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.

For an API provider functional testing is crucial to ensure that the APIs constantly deliver what they are meant for. Likewise, API consumers may choose to run tests for third-party APIs and a good tool is essential to test both internal and external APIs. This article will explore SuperTest, a Node.js testing library for HTTP APIs.

What is Supertest and why use one?

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.

It can be used as a standalone library or with testing frameworks like Mocha and Jest and provides a high-level abstraction to test HTTP requests. This means that in case the developer’s Node.js application is running an HTTP server, they don’t need a running server. Supertest is normally used by testers and developers for writing automated tests for routes and endpoints. It also extends another library called superagent which is a JavaScript client for Node.js and the browser. Usually, SuperTest is used as a standalone library or with popular JavaScript testing frameworks like Mocha and Jest.

Supertest can integrate with other testing frameworks easily with just a few lines of commands by sending a method like GET, PUT, and POST.

Why Use Supertest?

Most modern software use microservices and thus can function only with HTTP APIs that connect the frontend and backend components and third-party APIs for essential features. As mentioned earlier, SuperTest provides a high-level abstraction to test HTTP requests which means that if the  Node.js application is running an HTTP server; a running server is not essential. Requests can be made directly and to test any HTTP requests, just a few lines of commands are required by sending a method like GET, PUT and POST.

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 can help write automated tests and can determine if any part of the application has gone wrong.
  • After installing the package and testing framework, create the HTTP request.

Step 2: Create an HTTP request

The first approach 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 approach of creating an HTTP request is useful if the API is built with a Node.js framework. In this case, if necessary, the Supertest starts a test server and sends the requests there. The actual testing will now follow.

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 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 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. For instance, 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.

Download the 100% Free Guide

Master the essentials of advanced approach
to object recognition.

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.

A note on Supertest Jest and npm.

As discussed in detail, Supertest is a testing library used to test Node.js APIs; it needs a robust framework like Jest or Mocha. Jest is a testing framework or tool used to write tests for any JavaScript codebase, including Node.js projects. It’s intuitive, well-documented, easy configuration, and can be customized to meet any requirement. Supertest npm is a module for Node.js that allows testing of APIs using a small, powerful API; it simplifies the process of creating and running tests, identifies bugs easily and keeps the code running smoothly. Once Node.js and npm are installed on the system, tests can be written using a suitable framework. Supertest npm supports a wide range of frameworks, including Mocha, Jasmine, and Jest.

Conclusion

API testing is essential to keep all the integrated applications running and hence it is critical that developers and testers incorporate Supertest to automate the testing of routes and endpoints. This will help 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 the first API test with Supertest.

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 Andrades

Director, Product Evangelist at ACCELQ

Geosley is a Test Automation Evangelist and Community builder at ACCELQ. Being passionate about continuous learning, Geosley helps ACCELQ with innovative solutions to transform test automation to be simpler, more reliable, and sustainable for the real world.

Related Posts

Efficient Testing in Devops-ACCELQBlogTesting
20 October 2022

Using Branching Strategies to Efficient Testing in DevOps

A branching strategy helps define the delivery team's functions and how each feature, improvement, or bug fix is handled/tested
Best practices and metrics of production testingBlogTesting
1 December 2023

Best Practices and Metrics for Successful Production Testing

Step up your software game! Learn the ins and outs of production testing with our straightforward guide on best practices and crucial metrics.

Get started on your Codeless Test Automation journey

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

Close Menu