Behavior Driven Development with Mocha

Behaviour Driven Development — A brief introduction:

Behaviour Driven Development primarily focuses on the behavior of the end-user. The test cases focus on how a particular module should behave for particular action or input of the user. In a BDD the test cases are written before the software functionality is even implemented.

The steps to execute BDD are as follows:

  • Pick a specific software functionality
  • Write test cases for the chosen functionality
  • Develop the software functionality
  • Run the test cases against the developed functionality
  • Fix bugs and make relevant changes till all the test cases pass
  • Repeat the cycle for any new requirement

BDD and Mocha:

Mocha provides a variety of interfaces suitable for Behaviour Driven Development, Test Driven Development, Exports, QUnit, etc. which helps developers to execute test cases in any way desired or suitable for the development cycle.

For the purpose of executing/implementing BDD in projects, Mocha provides interfaces such as describe(), it(), before(), after(), beforeEach(), afterEach() etc.

Installing Mocha:

Mocha can be installed either locally or globally in the development machine.

To install mocha globally use the following command:

npm i — global mocha

To install mocha as a dependency in your project use the following command:

npm i — save-dev mocha

Mocha automatically detects test cases inside the test directory of your project. So, create a test directory in your project root folder.

The following example focuses on mocha installed as a dependency in a local project. The project should have the following structure once mocha is installed.

Modify the test script in your package.json file to run test cases using Mocha.

The following is a sample test case that tests the behavior of the indexOf function.

To execute the test case, use the following command:

npm test

The output will look as follows:

The above is a sample example on how to write and execute test cases with mocha. In real time the test case will test any functions or code blocks that will be written for the software/project module to be developed.


Implementing test cases helps to eliminate the need to manually test the application for each piece of change. Once the test cases are in place, we can be at peace because no other code can break the software by accident.

For instance, in a group of large developers working ceaselessly to fix bugs or constantly making changes to the code base, if the behavior of a particular module is covered in a test case, no one can accidentally break any expected functionality because the test cases will fail and alert the developer to fix the bug.

Although it takes time to develop the test cases and put it in place, the advantages outweigh the effort put in to implement the test cases.

Author: Shiny Metilda Christina Joseph

Your Digital Transformation partner. We are here to share knowledge on varied technologies, updates; and to stay in touch with the tech-space.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store