Table of contents
1.
Introduction
2.
Setting up ExpressJs
2.1.
Installing express
3.
Creating express application
3.1.
app.js
3.2.
server.js
3.3.
Run
4.
Setting up Jest
5.
Testing
5.1.
Installing supertest, superagent
5.2.
Test code
5.2.1.
app.test.js
5.2.2.
output
5.3.
Test using supertest
5.3.1.
app.test.js
5.3.2.
Output
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Testing Express App with Jest

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn how to test the express apps. First, we will create our very own express application, and we will be using Jest, supertest, and superagent to test the Rest API calls of this application.

Let's get started, and I hope you learn a lot from this tutorial.

Setting up ExpressJs

In this section, we will set up and create an Express application. If you are unfamiliar with Express Js, please go through Introduction to Express Blog to understand the express and detailed process of setting up Express.

Installing express

First of all, make sure nodeJs and npm are installed into your machine.

Run the following command to initialize a node project.

npm init -y

After that, install expressJs to your project using the following command.

npm install Express

Use the following command to check the version of Express and make sure Express is installed.

npm --version express

If this command responds with a version name and not an error, we can create a new application.

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

  1. 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.
     
  2. 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.
     
  3. What does a supertest request do?
    SuperTest requests can make an API call to our express app with the GET method, POST method, etc.
     
  4. 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!

Live masterclass