Test REST APIs using Jest and Supertest
Testing is an important practice that is often ignored. It helps to prevent bugs, reduce development costs, and improve performance. In this article, we are going to test a rest API using a testing framework called Jest.
First, we will create a simple express application and will separate our app from the server so it won't listen to the port after testing.
We will install the required packages for testing our app.
We also need to add a test script in package.json.
We will create a POST route that will send us the reverse of the string passed in the request.
We will create a reverse.test.js file to test this route using jest and supertest.
Now, we will test the route.
Voilà! We have successfully tested our route.
Source code: https://github.com/Xebec19/jest-medium