Supertest API testing library
One of the key benefits of using Supertest is that it is built on top of the popular testing library, Superagent. This means that it has a similar syntax and functionality, making it easy for developers who are already familiar with Superagent to get started.
Supertest also provides a number of additional features that are specifically designed for testing APIs. For example, it allows developers to easily send JSON payloads in their requests and assert that the response has the correct status code and headers.
To get started with Supertest, you will first need to install it as a dependency in your project. Once you have done this, you can import the library in your test file and use it to make requests to your server-side code.
For example, you can use the .get() method to send a GET request to your server and assert that the response has the correct status code and contains the expected data.
In this example, the request(app) function creates a new Supertest object that is bound to our Express app, which allows us to make requests to the app’s routes. The .get(‘/’) method sends a GET request to the root route of the app and returns a promise that resolves to the response object.
For more examples and complete working project please use my GitHub project.
Tags: API testing
Categories: API testing