Getting started with Jest!

Aniket Pal
Feb 24 · 4 min read

Working in an enterprise or in an open-source project, every software has a huge codebase. Google has a codebase of 2 billion lines! Understanding and debugging each line manually is something that probably only Superman can do. So when contributing to a project, it is essential to keep in mind that your code doesn't disrupt the existing functionalities.

What is Testing?

In software, testing is the process of finding any gap, error, or missing requirements and verifying if it matches our needs.

Suppose you give input in a DC Machine, by your theoretical knowledge you will have some expected output right? So in testing, we generally check the difference in the expected value and the actual value and try to fix it as much as possible.

Flow chart describing testing
Flow chart describing testing
Flow chart describing the testing process

Testing is divided majorly into 3 categories

  • Unit Testing: testing one function
  • Integrating Testing: testing a function that calls a function
  • End to End Testing: validating a DOM

In this blog, I will focus more on Unit Testing, because unit testing is easy to implement and very commonly used.

What is jest?

Jest is a JavaScript testing framework powered by Facebook. It focuses more on simplicity and support for large web applications. It is used for testing applications using Babel, TypeScript, Node.js, React, Angular, Vue.js, and Svelte. Jest is one of the most popular test runners these days and the default choice for React projects.

Jest ships in the NPM package and you can install it in any JavaScript projects by just running 👇

Let’s see a demo

Setting up the Project

Now you are into your directory, so let’s initialize it with npm

The flag -y helps you initialize with default values.

Now, let’s install the jest npm package

The project structure is very important so let’s make it.

Project structure for testing

For testing, it is essential to name the testing file with the name of your JavaScript file you want to test and concatenating the word test in between. Over here we will be a testing script for subtracting 2 elements. The script is written in subtract.js and the corresponding testing file is subtract.test.js.

Open up and configure a script named for running Jest:

Now we are good to go 😁. Let’s start with the scripting of subtract.js and subtract.test.js

In subtract.js:

In subtract.test.js:

Now let’s test it!

After the test, it gives you an output showing the status of the code and comparing it with the actual result and the specified expected value. You will get an output similar to

For one or two functions you can determine but if there are more than one it becomes difficult to understand …

So we write jest --coverage

Jest coverage command gives a more detailed analysis where the test fails and code can be improved accordingly.

These are the basics of jest. To know more, you can refer to Jest. Happy testing!😁

Webwiznitr

Nothing is perfect but your website can be

Webwiznitr

Web development club of National Institute of Technology, we aim to inspire people to select web development as their second hobby.

Aniket Pal

Written by

Sophomore EE NITRKL’23 | Novice developer 😁

Webwiznitr

Web development club of National Institute of Technology, we aim to inspire people to select web development as their second hobby.