In this article, I am going to discuss one of the popular Unit Testing frameworks, Mocha and the assertion framework chai.js.

Mocha is one of the well-known frameworks for JavaScript unit testing. If you are very new to JavaScript unit testing, then please visit my previous article.

In the previous article, I covered the basic unit testing concept with Jasmine. Now, in this part, I am going to discuss one more framework, Mocha, and the assertion framework, chai.js.

Mocha is a testing JavaScript framework so it is hosted on node.js. It is easy to understand and also, the reporting feature of Mocha is good. So, without wasting our time, we can start learning about it.

First, visit here for downloading and installing Mocha.

Two options are available here using npm.

  1. Local (only for the project)

    npm install –save-dev mocha

  2. Global

    npm install –global mocha

Create a simple application using Mocha

I have installed Mocha globally.

Now create a folder and open that in any IDE/Editor, like Sublime Text or Visual Studio Code (Here, I have used Sublime Text).

JavaScript Testing Using Mocha And Chai

Create a test folder and inside that, add one file named first.test.js.

JavaScript Testing Using Mocha And Chai

Open the terminal and use the command – mocha

If you are using local Mocha installer, then instead of it, use ./node_modules/mocha/bin/mocha

JavaScript Testing Using Mocha And Chai

Also, you can change in package.json.

  1. “scripts”: {  
  2.     “test”: “mocha”  
  3.   }  

Then, one can use the command to run the application – npm test

While writing a test case, we have got two keywords.

  1. Describe – Describe is a group of test cases that can be used to test a specific behavior or functionality of the JavaScript application. The describe function contains two parameters – name and function. In this function, we can add one or many descriptions of its block.

  2. It – it is like an individual test case.
    It is also having the same two params as describe – name of “it” feature, and function. This function contains the actual code alone with assertions. We will see what is an assertion in detail.

Test execution hooks

Hooks mean some methods where we can write the common code which executes before and after the test case. 

  1. describe(‘calculator application’, function() {  
  2.   before(function() {  
  3.     // runs before all tests in this block  
  4.   });  
  5.   after(function() {  
  6.     // runs after all tests in this block  
  7.   });  
  8.   beforeEach(function() {  
  9.     // runs before each test in this block  
  10.   });  
  11.   afterEach(function() {  
  12.     // runs after each test in this block  
  13.   });  
  14.   // test cases  
  15.   it(‘addition’,function() {  
  16.     //addition test case logic  
  17.   });  
  18.   it(‘subtraction’,function() {  
  19.     //subtraction test case logic  
  20.   });  
  21.   it(‘multiplication’,function() {  
  22.     //multiplication test case logic  
  23.   });  
  24.   it(‘division’,function() {  
  25.     //division test case logic  
  26.   });  
  27. });  


Assertion module is a part of node.js module. This can be used not only for Mocha but all other frameworks to validate the test case. Assertion has two main parameters – actual and expected. There are so many frameworks available to extend the node.js assertion and add some very good features so that we can implement a better assertion in our test case. One of the popular frameworks is chai.js.

Integrate chai.js in Mocha application

First, download and install chai.js.

npm install chai –save

Please visit here for a detailed understanding of chai.js.

Using the below line of code, you can import Chai assertion packages in your JavaScript test case.

  1. var assert = require(‘chai’).assert; 

The below line of code gives you some basic information on asserts.

  1. Var foo = ‘bar’, beverages = { tea: [ ‘chai’, ‘matcha’, ‘oolong’ ] };  
  2. assert.typeOf(foo, ‘string’); // without optional message  
  3. assert.typeOf(foo, ‘string’, ‘foo is a string’); // with optional message  
  4. assert.equal(foo, ‘bar’, ‘foo equal `bar`’);  
  5. assert.lengthOf(foo, 3, ‘foo`s value has a length of 3’);  
  6. assert.lengthOf(beverages.tea, 3, ‘beverages has 3 types of tea’);  

Assertion in chai.js in details,

There are so many assertions available. Let us create and run the sample Mocha application with chai.js.

  1. var assert = require(‘chai’).assert;  
  2. describe(‘mocha testing with chai assert ‘, function() {  
  3.     it(‘test case for foo’, function() {  
  4.         var foo = ‘bar’;  
  5.         assert.typeOf(foo, ‘string’); // without optional message  
  6.         assert.typeOf(foo, ‘string’, ‘foo is a string’); // with optional message  
  7.         assert.equal(foo, ‘bar’, ‘foo equal `bar`’);  
  8.         assert.lengthOf(foo, 3, ‘foo`s value has a length of 3’);  
  9.     });  
  10.     it(‘check beverages has 3 types of tea’, function() {  
  11.         var beverages = { tea: [ ‘chai’, ‘matcha’, ‘oolong’ ] };  
  12.         assert.lengthOf(beverages.tea, 3, ‘beverages has 3 types of tea’);  
  13.     });  
  14. });  
JavaScript Testing Using Mocha And Chai

Generation of test cases dynamically.

  1. var assert = require(‘chai’).assert;  
  3. function add() {  
  4.   return, curr) {  
  5.     return prev + curr;  
  6.   }, 0);  
  7. }  
  9. describe(‘add()’, function() {  
  10.   var tests = [  
  11.     {args: [1, 2],       expected: 3},  
  12.     {args: [1, 2, 3],    expected: 6},  
  13.     {args: [1, 2, 3, 4], expected: 10}  
  14.   ];  
  16.   tests.forEach(function(test) {  
  17.     it(‘correctly adds ‘ + test.args.length + ‘ args’, function() {  
  18.       var res = add.apply(null, test.args);  
  19.       assert.equal(res, test.expected);  
  20.     });  
  21.   });  
  22. });  
JavaScript Testing Using Mocha And Chai


Mocha framework is used not only for unit testing but also, we can use it to implement a RESTful framework and an Automation framework using Selenium Webdriver.


Please enter your comment!
Please enter your name here