[null, {"document_url": "https://coursework.vschool.io/mocha-chai-your-first-test/", "unformatted_src": "/content/images/2018/05/Screen-Shot-2018-05-14-at-4.08.56-PM.png", "src": "https://coursework.vschool.io/content/images/2018/05/Screen-Shot-2018-05-14-at-4.08.56-PM.png", "formatted_filename": "Screen Shot at"}, null, {"document_url": "https://coursework.vschool.io/mocha-chai-your-first-test/", "unformatted_src": "/content/images/2018/05/Screen-Shot-2018-05-14-at-4.28.43-PM.png", "src": "https://coursework.vschool.io/content/images/2018/05/Screen-Shot-2018-05-14-at-4.28.43-PM.png", "formatted_filename": "Screen Shot at"}, null, {"document_url": "https://coursework.vschool.io/mocha-chai-your-first-test/", "unformatted_src": "/content/images/2018/05/Screen-Shot-2018-05-14-at-4.49.03-PM.png", "src": "https://coursework.vschool.io/content/images/2018/05/Screen-Shot-2018-05-14-at-4.49.03-PM.png", "formatted_filename": "Screen Shot at"}, null, {"document_url": "https://coursework.vschool.io/mocha-chai-your-first-test/", "unformatted_src": "/content/images/2018/05/Screen-Shot-2018-05-14-at-4.57.40-PM.png", "src": "https://coursework.vschool.io/content/images/2018/05/Screen-Shot-2018-05-14-at-4.57.40-PM.png", "formatted_filename": "Screen Shot at"}, null]
Mocha is a test runner. It's sole job is to look for (or be told where to find) unit tests you've written, and to execute them. It also displays in a nice, easy format to show you how your unit tests faired - how many passed, how many and which ones failed.

Mocha Website:


Chai is an assertion library. Chai's libraries allow you to choose exactly how your unit tests should read to a developer. There are three libraries you get to choose from: assert, expect, and should.

All of these libraries essentially do the same thing, just with slightly different syntaxes. For simplicity, we're going to use the assert library, but you should definitely check out how tests are written in the expect and should libraries. (It's pretty nice, because they read like english).

Chai Website:

Assert API:


First we will install Mocha & Chai.

We are installing Chai locally to our nearest folder, but we are installing Mocha globally. Installing Mocha globally saves us a lot of hassle which is why we are doing it that way instead of locally.

After these have been installed, we will create two files:

In our app.js file we will put the material that we want to run the test on. In our example we will just be writing a simple function algorithm.

In our test.js file is where we will write the test that we are going to run. This is also where we will be importing Chai.



Ok, now we can break down our files to see just exactly what is happening.

In our app.js file we are just writing an empty function. That function will eventually do what we want it to. But for now we are leaving it empty.

The last line on the file is module.exports = addS;

This line is allowing us to export, or send, the content of that file to be used in another file, which in our case will be the test.js file.

Let's take a look at our test.js file.

First, we are importing Chai into our document.

As stated above, we only want to use the assert library for now, so the next line is just assigning the assert library to a variable assert.

Next, we are importing the contents of our app.js file into our test.js file so that we can use our function in our test.

Finally, we see a few functions that seemingly come from nowhere.

describe and it

These both come from the globally installed Mocha.

The describe is used to title the algorithm or function or to describe what it is supposed to do.

The it is used to tell what each test you write should or should not do.

The assert.deepEqual is from our Chai Assert Library.

The .deepEqual asserts that actual is deeply equal to expected.
(.deepEqual is for comparing arrays or objects, if we were comparing simple data types like numbers, booleans, strings, etc.. we would simply use .isEqual instead)

The assert.deepEqual also takes 2 arguments.

Let's fill out our test.js and look at an example of it running.

Go into your terminal and type mocha.

It should return something that looks like this: Notice that the first line in white is the same as what we wrote in our describe. The second line in red is the same as what we wrote in our it.

It then runs the test to check if:

Our Actual

is equal to

Our Expected

Since ['dog', 'cat', 'alligator'] is not equal to ['dogs', 'cats', 'alligators'] it gives us an error showing how the test failed.

So now we will go to our app.js and fix it so that our test will pass.

Now if we run mocha it will look like this. Now let's add a couple additional tests.

If we run mocha it will look like this: So we would now need to fix our algorithm so that it passes all of the tests.

Now mocha will look like this After seeing all this, perhaps it has crossed your mind to ask why this is necessary??

Below is a recap:

What is unit testing?

Unit testing is the practice of breaking your program into smaller pieces and inspect each piece with tests.

Tests run when changes are made to the code in your projects. The more often you run your tests the better.This helps you catch problems sooner and write better code.

Why should we be unit testing our code?

React Event Listeners

Handling Events Handling events with React elements is very similar to handling events on DOM elements. There are some…


A Regular Expression is simply a sequence of characters that define a search pattern. Regular expressions can be given…