Chai and Mocha as testing framework

Sakshi Mishra
4 min readJun 21, 2020

Still testing your code with print statements?

Still using debugger to test if input to a particular function is correct?

Well here is something which might help you to test individual units of your code and make your Node.js application more robust and reliable.

Chai and Mocha

In unit testing, individual components of software are tested. A unit may be an individual program, function, procedure, etc. This testing helps in the detection of problems early in the development cycle.

Chai is an assertion library for Node.js and is best used with mocha as a testing framework. Mocha is a JavaScript test framework running on Node.js which doesn’t have a built-in assertion library. Hence we can use any assertion library with mocha. Now the question is what are assertions?

Assertions are boolean functions that are used to test the behavior of the program . These functions will return “true” unless there is a bug in the program. A lot of developers choose Chai as their assertion library. Chai comes with three flavors of assertions: should style, expect style, and assert style.

Installing chai and mocha globally for Node.js framework

$ npm install -g chai

$ npm install -g mocha

Write your first test case

Create a project and initialize it by running npm init. Create a simple function that adds two numbers and returns the sum.

Now to test this simple function create it’s test file and put it inside a folder named “test”.Then require ‘chai’ and you can use any of its three styles. Here I have used it’s ‘assert’ style. Then, require the file whose functions you want to test.

Now while writing a test case you should know two functions describe() and it().

  1. describe() takes two arguments, the first is the name of the test and the second is a callback function that makes calls to it() functions written inside it
  2. it() also takes two arguments, the first describes what a test should do and the second is a callback function that has the actual logic to test

Now we can use different assertion tests included in “assert”. Here, in this case, I have used assert.equal(“actual”,“expected”) that tests equality between the actual result and the expected result.

To run test cases

$ npm test

And include the following inside package.json file

While running the test cases we received the following output

Here one test fails because the actual result was 76 and we were expecting it to be equal to 67. And the other one passes as the expected value was equal to the actual value.

Similarly, now you can create test cases for all the files that you have in your project. Now, what if I want to check that how much percentage of code is covered by my test cases?

Istanbul nyc

A library that is used with mocha to test the code coverage by running the test cases

Install nyc

$ npm i nyc

And to run nyc include nyc in package.json file

Now by running the command “npm test” we will also get a test coverage report. Here I am sharing the test coverage report of one of my projects below

Looking into the test coverage report in detail

Now let us see how to read the test coverage report by understanding what each field signifies.

  1. File: lists all the files on which test cases are written
  2. Stmts: displays the percentage of statements that have been executed.
  3. Branch: If the code has branching let’s say if and else statements, do both conditions get executed?
  4. Funcs: percentage of functions that gets executed
  5. Lines: percentage of lines (code lines) getting executed
  6. Uncovered Line: gives the code line numbers which didn’t get executed while running the test cases

Done

Well, now you are ready to create test cases of your Node.js application and get the test coverage report for the same!! This process helps in making the application more robust and detecting the errors early in the development cycle.

Thanks for reading and if my post was helpful please hit the little clap :)

--

--