Creating express application
We will create an app.js file containing all the routing information of our application, and we will also create a server.js file that will import app.js and listen to the localhost server.
app.js
In app.js, first we are importing express, then we are creating a route “/” with a get method, and exporting the file using module.exports.
//app.js
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.status(200).send("Welcome to Coding Ninjas!");
});
module.exports = app;
server.js
//server.js
const app = require("./app");
app.listen(3000, () => {
console.log("server is listening at port 3000...");
});
Upto this point the directory structure will look like this.

Run
Use the following command to run our application.
node server.js
The following will be shown In the terminal.

Our application is live at http://localhost:3000/.

Setting up Jest
Jest is a very trendy testing framework, and it is used to test both front-end and back-end JavaScript-based applications. It was designed to check JavaScript-based applications.
If you are not familiar with Jest, I highly recommend you to check out Getting started with Jest and Writing first unit test in Jest blogs to understand more about Jest, how to set up Jest, and writing sample unit tests using Jest.
Testing
Now that our app is up and running let's test it using Jest, supertest, and superagent.
supertest is a superagent-driven library for testing node.js HTTP servers. It provides a very high-level abstraction for testing HTTP.
Installing supertest, superagent
Please run the following command to install supertest and superagent in our project.
npm install supertest superagent
You can see that in terminal that following packages are installed successfully.

Test code
Create a file function.test.js in the same folder as app.js, and Jest will automatically detect that this is our test file and write the following test in it. We are using async-await to fetch the response from a get request. We are testing if the response has a status code equal to 200. 200 response code means "OK."
app.test.js
const request = require("supertest");
const app = require("./app");
test("GET method response has status code = 200", async () => {
const response = await request(app).get("/");
expect(response.statusCode).toBe(200);
});
output
Open another terminal, since on one terminal server.js is running, and run npm test.

Test using supertest
Supertest provides testers an excellent way to test express applications; we can simply return the statement without the need for asynchronous methods like callbacks, promises, async-await.
app.test.js
const request = require("supertest");
const app = require("./app");
test("GET method response has status code = 200", () => {
return request(app)
.get("/")
.expect(200);
});
Output

FAQs
-
What is a supertest?
SuperTest is a Node. Js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node.Js and the browser. Developers can use SuperTest as a standalone library or JavaScript testing frameworks like Mocha or Jest.
-
What is supertest used for?
SuperTest is an HTTP assertions library that allows you to test your Node. Js HTTP servers. It is built on top of the SuperAgent library, an HTTP client for Node.js.
-
What does a supertest request do?
SuperTest requests can make an API call to our express app with the GET method, POST method, etc.
-
Is jest good for API testing?
Jest is excellent for validating APIs because it comes with a bundle of tools that make writing tests more manageable. While Jest is most often used for simple API testing scenarios and assertions, it can also test complex data structures.
Key Takeaways
In conclusion, using Jest along with supertest, we can test express apps and their RESTful routes. Supertest is an assertion library built on the top of superagent, which is used to test NodeJs HTTP server.
Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Learning!