Mocha Chai - Introduction

Mocha Chai

Next →

In this tutorial series we will learn to use Mocha and Chai to test JavaScript code.

What is Mocha?

Mocha is a JavaScript testing framework that runs on Node.js and in the browser.

If you want to learn more about Mocha then check out Mocha tutorials.

What is Chai?

Chai is a BDD/TDD assertion library for Node and the browser and can be used with any testing framework (like Mocha).

BDD = Behavior Driven Development

TDD = Test Driven Development

What are we going to do?

In this tutorial series we will be writing some JavaScript code and then testing the code using Mocha and Chai.

So, lets go ahead and setup our development environment.

The complete project code of this tutorial series is there on my GitHub mocha-chai-project repository.

Install Node.js and npm

First we will install Node.js and npm and will use npm to install Mocha and Chai.

So, head over to their website and download the latest stable verison of Node and npm and install it.

Here is a turoial on how to install Node.js and npm on Mac.

After installing Node.js and npm run the following command node -v and npm -v to check their versions.

At the time of writing this tutorial I was using the following versions.

$ node -v
v7.7.2
$ npm -v
5.8.0

To use Mocha v3 and up you will need npm v2 or newer and Node v4 or newer version.

Now create a new project

Open your favourite text editor or IDE and create a new project folder and name it mocha-chai-project.

Feel free to use any name you like.

I will use the terminal to create the project folder and cd into it.

$ mkdir mocha-chai-project && cd mocha-chai-project

Initialise npm

Now run the following command npm init in the terminal or command prompt inside the project directory to initalise npm.

It will ask you to fill some fields. For this educational project you can ignore them by pressing Enter. Or fill them as you like.

If you want to initialise npm with default values use npm init -y command.

On success it will create package.json file inside the project folder.

Install Mocha and Chai

Now we will install Mocha and Chai as development dependency inside the project folder using npm

Use the following command to install Mocha and Chai.

$ npm install --save-dev mocha chai

At the time of writing this tutorial I was using the following versions.

$ npm install --save-dev mocha chai
+ mocha@5.0.5
+ chai@4.1.2
added 31 packages from 302 contributors in 3.87s

Alright, in the next tutorial we will setup Mocha and our project to start writing test codes.

Next →

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT