DEV Community

Cover image for Basic Unit Test- Starting with testing with jest, Explained.
Danish
Danish

Posted on

Basic Unit Test- Starting with testing with jest, Explained.

Testing using jest

Unit Test

Unit test refers to testing a unit of code any function or class
without external influence, just the functionality of single >piece of code.

Prereq -> make a npm project by creating a folder(testing) and in cmd write npm init --yes

Let's assume a simple js function absolute for testing purpose.

absolute.js

function absolute(number){      
       if(number > 0){
            return number;
       }else if(number < 0) {
            return -number;
       }else{ 
            return 0;
       }

}

module.exports.abs = absolute;

Enter fullscreen mode Exit fullscreen mode

Its a simple function which returns absolute of any given number.

Okay now lets install jest

npm i jest --save-dev

Yup! Install it as dev dependencies

now go into package.json file of your project

//You will see these lines

...//Above Code

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

...//Below Code

Change it to

 "scripts": {
    "test": "jest"
  },
Enter fullscreen mode Exit fullscreen mode

Concept

Before writing test we need to ask a crucial question
How many test I should write to properly test my code ?

And the answer is
Depends on code(function) execution paths

For example in our absolute function we have three execution >paths
Execution Path -> In how many ways a function exits i.e return >statements
To properly test it we have to write three tests !

One to check the positive number case
Second to check the negative number case.
Third to check the zero case.

*by default jest looks for a test or tests folder in the project directory or files ending with somename.spec.js or somename.test.js either inside the test folder or in the project directory.

Jest Concepts and functions

Functions

test("First Parameter is the name of test", ()=>{//second is function where test logic is written.})
expect(takes result of the function to be tested)
toBe(takes the expected output of the function)

Describe is used to group the similar tests,

describe("First parameter is the name of test",()=>{//Series of test})

it() is the function which we write instead of test in describe block.

Lets make a folder and a file.

test/absolute.test.js

Now lets write some tests !

const lib = require("../absolute");

describe("absolute", () => {
  it("will return positive number if input is positive", () => {
    const result = absolute(1);
    expect(result).toBe(1);
  });

  it("will return positive number if input is negative", () => {
    const result = absolute(-1);
    expect(result).toBe(1);
  });

  it("will return zero if input is zero", () => {
    const result = absolute(0);
    expect(result).toBe(0);
  });
});


Enter fullscreen mode Exit fullscreen mode

So we wrote test now lets run them
In the command prompt write npm test

npm test

Thanks for reading till here :)
Keep Coding.

Top comments (0)