Testing Code With Jest

Amritpal Singh
3 min readFeb 9, 2020

--

Hello Everyone,

Today I will be talking about testing your code in Node.js using an npm package Jest. Jest is an open-source framework created by Facebook for testing applications in JavaScript. Unit testing is a level of software testing where individual parts of source code are tested. A unit is the smallest testable part of any software. The purpose of testing is to ensure that the application performs as expected.

Why Test?

  • Saves Time
  • Creates Reliable Software
  • Gives flexibility to the developers

In order to install Jest, you must have Node.js installed. You can download Node from here. After you installed it successfully follow the steps below.

Installing and Setting up Jest

First, lets check if Node was installed successfully $ node -v
v12.2.0

We use Node version v12.2.0, if you don’t see any version number that means Node.js was not installed properly.

Create a new folder from terminal (mkdir jest-testing) and cd into the folder
and run the following command
npm init -y

We initiate a new Node application. This will create a package.json file.

Note: When -y is passed to NPM, it uses the defaults instead of asking questions

Next, install jest by running the following command

npm i --dev jest

After installing jest, your package.json should look like this following

{
"name": "Blog-Jest-Testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^25.1.0"
}
}
--------------------------------------------------------------------As you see under dependencies, "jest" is mentioned.
Now You need to alter the "scripts" => "test": Instead of "echo \"Error: no test specified\" && exit 1"Change it to "jest""scripts": {
"test": "jest"
},

After this step, you are ready to test.

Let’s do some testing on a simple addition function

  1. First, Create addition.js file (function file)
  2. Second, Create addition.test.js
addition.js function add(num1,num2) {
return num1 + num2
}
module.exports = add;

Note: Module.export will allow us to access add function in other files

addition.test.jsWe first need to require the file addition fileconst add = require('./addition')test("adds 2+2 to equal 4 ", () => {
expect(add(2,2)).toBe(4);
})

Explanation: In addition.test.js, jest gives as a method called test(). When we call test(), we have to provide two arguments, first is a string (name or mini description of the test) and is a function. Inside the function, jest gives us a library called expect(), we call it as a function and we pass in add function that we required from addition.js file. expect() gives us over 50 methods to use, we will be using .toBe() which checks for equality. We pass in the expected value which is 4. Now if you read, it is sort of like English. Expect add(2,2) value to be 4.

Now when you run this npm test
PASS ./addition.test.js
✓ adds 2+2 to equal 4 (3ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.108s
Ran all test suites.

Test passed, our function performed as expected.

Here is my Github Repository for this example.

Happy Coding :)

--

--