Getting started with Jest!

Aniket Pal
Webwiznitr
Published in
4 min readFeb 24, 2021

--

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 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 👇

npm install --save-dev jest 

Let’s see a demo

Setting up the Project

mkdir jestDemo
cd jestDemo

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

npm init -y

The flag -y helps you initialize with default values.

Now, let’s install the jest npm package

npm install --save-dev jest

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 package.json and configure a script named test for running Jest:

"scripts": {
"test": "jest"
},

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

In subtract.js:

function subtract(a,b){
return a-b
}
module.exports = subtract

In subtract.test.js:

const subtract = require('./subtract')test("Must subtract properly",() =>{
expect (subtract(1,2)).toBe(-1)
})

Now let’s test it!

npm test 

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!😁

--

--

Aniket Pal
Webwiznitr

Sophomore EE NITRKL’23 | Novice developer 😁