Getting Started
The first step to start working with is to create a node project if you don't already have one where you want to work. For Example,
$ mkdir mocha_project
Then we will initialize the npm project in our project directory like this.
$ npm init
The above command creates a Node.js project. It will also add the node_modules and package.json files to your project directory. After that, we will install mocha locally to our project, as we explained here.
We also need to set up the test script as mocha in our package.json file to run the tests.
"scripts": {
"test": "mocha"
},
Our package.json looks something like this.
{
"name": "mocha_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^9.2.1"
}
}
Now you are ready to work on Mocha to write tests.
Example
Now that you have your project ready, you can try out an elementary test code in mocha to see whether the setup works or not.
This article will use a simple indexOf() function test that returns -1 when the given value is not present.
We will put the test file (test.js) in a separate ./test folder in our project directory so that npm automatically looks for the test file in that folder.
$ mkdir test/test.js
Now, all we have to do is to write the test code and start working with the Mocha framework.
Program
var assert = require("assert");
describe("Array", function () {
describe("#indexOf()", function () {
it("should return -1 when if value is not present", function () {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});

You can also try this code with Online Javascript Compiler
Run Code
You can now run the given test file from the command line using the following command.
$ npm test
Output

FAQs
-
What is the default interface in Mocha?
Mocha.js provides a variety of interfaces for defining hooks, test suites, and individual tests. These include Test Suite Specification(TSS), Exports, QUnit, and Require. The default interface of Mocha is Behavior Driven Development(BDD), where we develop the application around the behavior a user expects to experience when interacting with it.
-
Which is better, jest or Mocha?
The Jest framework was initially designed for React and mainly focused on simplicity of implementation. On the other hand, Mocha offered a considerable dose of flexibility regarding test development and was initially intended for Node.js.
-
Does Mocha run tests in parallel?
Mocha does not run individual tests in parallel.
That means if we hand Mocha a single test file, it will spawn a single worker process, and the worker process will run the file. You will be penalized for using parallel mode. If you only have one test file.
-
What is the difference between Mocha and Chai?
Mocha is a JavaScript test framework that runs on Node.js and allows asynchronous testing using any assertion library. Chai, an assertion library, provides various functions and methods in a clean syntax that compares the output of a particular test with its expected value. We often use Chai and Mocha together. The fundamental difference between the two is that mocha is a framework while chai is a library.
Key Takeaways
Mocha is a handy, potent, and accessible framework that makes writing tests a great pleasure. We have just started our journey into working with Mocha, and there is a lot to explore here. The Mocha testing framework has endless potential with exciting features and libraries that we are yet to explore.
If you are starting on learning unit testing, be sure to check our excellent articles on Getting Started with Jest and Writing your first Unit Test with Jest to widen your knowledge in this field. Also, be sure to head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more. Till then, Happy Learning!