DEV Community

Cover image for What is Mocha & Chai?
Jordan Davis
Jordan Davis

Posted on

What is Mocha & Chai?

When it comes to testing in JavaScript, two tools that are often used together are Mocha and Chai.
Mocha is a testing framework, while Chai is an assertion library.
They work excellently together to help do tons of different software development cycles.

Mocha
Mocha is a rich JavaScript testing framework that makes asynchronous testing simple.
It runs on Node.js and in the browser and allows you to write tests in various styles, whether BDD, TDD, or Unit-style.
Mocha has a robust API for running tests, hooking into test execution, and generating reports.
Mocha makes it easy to write tests that are easy to read and maintain and integrates well with other testing tools and libraries.

Chai
Chai is an assertion library that provides a clean, readable syntax for writing assertions in JavaScript.
It is commonly paired with Mocha but can be used with any testing framework.
Chai provides a variety of assertion styles, including should, expect, and assert, so you have plenty of choices.
Chai's API is designed to be easy to use, and it provides a range of built-in assertions for testing lots of JavaScript datatypes and structures.

What it's like using Mocha & Chai together
When Mocha and Chai are used together, they give you many options for testing JavaScript applications.
Mocha gives the structure and control flow for running tests, while Chai provides the assertions necessary for testing the application's behavior.
You must install both libraries using npm and then require them in your testing code to use them together.

Setup and Testing

First, we'd set up a new directory inside our terminal. We can run this command:

mkdir blog, then cd blog
Enter fullscreen mode Exit fullscreen mode

To initialize a new Node.js project, you can run the following command in the terminal:

npm init 
Enter fullscreen mode Exit fullscreen mode

To install Mocha and Chai as development dependencies, you can run the following command in the terminal:

npm install Mocha Chai --save-dev
Enter fullscreen mode Exit fullscreen mode

Then we create a new directory at the root of the project. This one will contain our function code to test:

mkdir src
Enter fullscreen mode Exit fullscreen mode

We will create a function to test :

function myFunction(str) {
  return str.length === 4;
}

Here we export myFunction
module.exports = myFunction;
Enter fullscreen mode Exit fullscreen mode

We still need a test directory to hold our test file:

mkdir test 
touch testMyFunction.js
Enter fullscreen mode Exit fullscreen mode

Inside testMyFunction, we write our test code:

const assert = require('chai').assert;
const myFunction = require('../src/myFunction');

describe('myFunction', function() {
  it('should return true if the input string has length 4', function() {
    const result = myFunction('abcd');
    assert.isTrue(result);
  });

  it('should return false if the input string has length other than 4', function() {
    const result = myFunction('abc');
    assert.isFalse(result);
  });
});
Enter fullscreen mode Exit fullscreen mode

To check if this is working, we then need to go to update the "test" script inside our package.json file to run Mocha:

"scripts": {
    "test": "mocha test/testMyFunction.js"
  },
Enter fullscreen mode Exit fullscreen mode

Now with that, we can run this command in our to check our test:

npm test
Enter fullscreen mode Exit fullscreen mode

We should get an output like this:

Test Passing

Conclusion
Now we're done! We used some terminal shortcuts, created a couple of folders and files, installed the dynamic duo Mocha & Chai, and made a passing test!
If you're new to testing in JavaScript, Mocha and Chai are great places to start. They're easy to learn and use, and it's an excellent start to building more complex testing suits!

Resources
https://mochajs.org
https://www.chaijs.com
https://www.browserstack.com/guide/unit-testing-for-nodejs-

Top comments (0)