Unit Testing with Jest

abhinav bussa
4 min readApr 24, 2021

--

Testing is the major step in software development. Testing an application involves manual and automation testing from QA Engineers which covers almost all the features under the application, so why do you think we need unit tests?

Unit testing can help developers in accomplishing the exact behavior of the feature of an application. These come into the picture in the development model called Test Driven Development (TDD). In this model unit tests should be written first to drive the software development before any code is written.

What is Unit Test?
Unit testing is software testing where individual units (components) of software are tested. The purpose of unit testing is to validate that each unit of the software performs as designed. A unit is the smallest testable part of any software which saves time and money during the software development life cycle and lets developers write faster more effective code.

What is Jest?
Jest is an open-source unit testing Javascript framework that was created by Christoph Nakazawa and is maintained by Facebook. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue, and more!

For testing, we will create a simple application with Node js. So from the command line let’s make a new directory, initialize npm, create a file, and install Jest:

$ mkdir test
$ cd test
$ npm init -y$ touch script.js
$ npm install --save-dev jest
$ code . (open the new dir in whatever code editor you use)

Now open package.json and specify “test” to run Jest by replacing the text with “jest”:

Building our first test

Create a js file with a simple function to add 2 numbers.
script.js

Let’s create a new file with the same name of the file that we will be testing, but add “.test.js” to hold our test which will have actual tests. Every test file using Jest will need to have “.test.js” at the end of the name in order for it to be detected as a test case. Below is a simple test created with Jest syntax to test if 2 + 3 gets a result of 5 for our sum function:
script.test.js.

The “test” function takes two arguments; the first parameter is a text in which we define what that test is going to check for (this is just a readable text, it has nothing to do with the test operations).

The second parameter is a function that contains the test we will run. Inside this function, we have the “expect” function that takes in a specified function to be tested (“sum”) with arguments of our choosing. This is called Assertion.

The result is then checked against our expected result using “matchers” like .toBe (which checks for strict equality ===). See a full list of matchers here.

Now let’s run:

$ npm test

The terminal should output something similar to this:

Tada! our first test is passing. Let's try another test within the same test file but with a different matcher. We can export both functions.

Updated script file with another function called checkSquare this is to check if the squared value of a parameter is equal to the value that is passed as a parameter and return a boolean.

script.js

Let's update the test file for the newly added function.

Run $ npm test and see what happens. The test checking our checkSquare function is failing:

If you look at the type of assertion we are doing on the newly added test case, we see that we are checking that the checkSquare should return true but that's not the case as 24 is not the square of 5. So to make the test case pass we need to test with another matcher called toBeFalsy which checks for falsy values.

It is worth noting that you can write multiple tests in the same file for each unit that you are testing. And make sure you bookmark the Jest Cheat Sheet!

Thanks for reading along to get a basic understanding of Jest.

--

--

abhinav bussa
abhinav bussa

Written by abhinav bussa

Javascript Developer | Creative | Energetic | Enthusiast

No responses yet