JavaScript testing with Mocha and Chai

Writing Unit Tests is one of the things which we should not avoid while we are building our projects. Unit tests allow us to make changes to the code quickly, without the effort to test the complete functionally in a manual process again, this saves hours of project time.

Of course, there are a lot more reasons to do unit tests. If you want to read about Unit Tests and why they are worth the effort, I recommend you this stack overflow article


  • Node.js
  • Code Editor
  • Mocha
  • Chai

What is Mocha?

Mocha is a javascript testing framework for Node.js, it is simple, extensible and allows Asynchronous testing. Mocha has a lot of features, if you want to see the full list I recommend the Mocha website

What is Chai?

Chai is our assertion framework (a TDD / BDD assertion framework) which we are using in this post. With Mocha, it is possible to use other frameworks, feel free to use your favorite kind of assertion framework.

Here is a list of assertion libraries which are officially listed on the chai website.

You also can use the Node.js build-in assert module, but I recommend to use one of the listed assertion libraries.

Install Mocha and Chai Globally

For installing mocha we are using the node package manager, you can install mocha by typing npm install -g mocha

We also have to do the same for the assertion framework chai by typing npm install chai --save-dev

The parameter -g installs the mocha as a global dependency, so the two frameworks are accessible like a command line tool. --save-dev installs the chai library as a development dependency.

For starting our tests it is important to add mocha as a script in our package.json. Later we can run our tests with npm test.

Folder structure

A good practice when writing tests are the usage of a test folder. This folder contains all tests in our project. Mocha is looking for tests in a folder which is called test.

|-- test.js

Mocha describe() and it()

The describe() function provides a way to group our tests in Mocha. describe() allows two parameters, the first is the name of the test group and should indicate what we are testing, the second is the callback function. It is up to you how you group your test cases.

The it() function is used for test cases. it() allows also two parameters the first is the explanation of the test and should be a human-readable description of the test itself and the second is the again a callback function which contains the test itself.

Writing Test Code

In the following we writing a very basic test case, assume we have a very simple function which converts Celsius to Fahrenheit and we want to write a unit test for this function.

Our project should now have the following structure

|-- temperature.tests.js

Running the Test

There are different ways to run tests with Mocha.

  • Running the Tests in the Browser or
  • Running the Tests on Node

In this example we are running the tests on node, how we can run the test in a Browser we learn in another post.

To start our test we open a console and write npm run test or mocha test, as you can see, the tests pass. The describe() which defines a group is highlighted and contains the it() (our unit test) as a nested line in the console output.

What`s next?

This post provides us the very basic knowledge how we can write tests with Mocha and Chai.

With this setup it is easy to start with Test Driven Development (assert) or Behavior Driven Development (expect/should).

If you want to learn more about the different frameworks check the documentation or my future posts.

If you are familiar with express.js you can look into their mocha tests

Sascha Heyer

Read more posts by this author.

Subscribe to Sascha Heyer, Software Engineer

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!