Mocha JavaScript Test Framework: Guide and Examples

javascript mocha javascript framework

Mocha is a JavaScript test framework for Node.js. It runs tests in sequence and shows results. It works with any assertion library and supports asynchronous tests.

Benefits of Mocha Framework for JavaScript Testing

Mocha helps you run JavaScript tests with control and order. It works with any assertion library, so you can pick the one you want.

Let’s look at an example to understand it:

const assert = require('assert');

describe('Math tests', function() {
  it('adds numbers', function() {
    assert.strictEqual(2 + 3, 5);
  });
});

This test checks if 2 + 3 equals 5.

Here is the output in the terminal:

mocha test in javascript

Install and Set Up the Mocha Framework to Test in JavaScript

Install Mocha with npm. Run this command in your project folder.

npm install --save-dev mocha

Add a test script package.json after installation in the same root of your code.

"scripts": {
  "test": "mocha"
}

Mocha with Assertion Libraries (Chai Example)

Create a file named test.js. Write a test inside it.

const assert = require('assert');

describe('Array', function() {
  it('returns -1 if value not found', function() {
    assert.strictEqual([1, 2, 3].indexOf(4), -1);
  });
});

This test checks if indexOf(4) gives -1 for an array with numbers 1, 2, 3. Here is an image that shows you the result:

mocha test in js

Run Test with the CLI of the Mocha Framework in JavaScript

Mocha works well with Chai. Chai gives different styles for writing assertions.

Install Chai with npm.

npm install chai --save-dev

Here’s how it works in practice.

const chai = require('chai');
const expect = chai.expect;

describe('Chai test', function() {
  it('checks type of value', function() {
    expect('hello').to.be.a('string');
  });
});

This test checks if 'hello' is a string.

Run tests with the Mocha CLI. Use the command below.

npx mocha

The command finds test files and runs them. It shows the results in your terminal.

Here is the result:

chai test

Organize Tests in Projects

Keep tests in a folder named test in your project. Mocha looks inside this folder by default. Place each test file inside it.

Use names that describe the test purpose.

Wrapping Up

You learned the role of Mocha and how to install it. Also, you understood how to write a test and how to run it.

Here is a quick recap:

  • Mocha runs JavaScript tests with sequence and control.
  • It works with assertion libraries like Chai.
  • It runs tests from the CLI with npx mocha.
  • It supports the test organization inside a test folder.

FAQs

What is Mocha JavaScript Test Framework?

Mocha JavaScript Test Framework is a flexible test tool for Node.js and browsers. It supports asynchronous tests and provides descriptive test reports.

How do I install Mocha JavaScript Test Framework?

Use npm to install Mocha in your project:
npm install --save-dev mocha
  • Install globally: npm install -g mocha
  • Install locally: npm install --save-dev mocha

How do I write my first Mocha test?

Create a test file like test.js with the following:
const assert = require('assert');
describe('Array', function() {
  it('should return -1 when value is not present', function() {
    assert.equal([1,2,3].indexOf(4), -1);
  });
});
  1. Create test file: test.js
  2. Run test: npx mocha

How to run Mocha tests from command line?

Use the Mocha command in your project: mocha
  • Run all tests: npx mocha
  • Run specific file: npx mocha test/test.js

Similar Reads

JavaScript While Loop: How It Works with Examples

The JavaScript while loop runs code as long as a condition stays true. You can use it to repeat tasks…

What Is AJAX? How It Works in Web Development with Examples

AJAX keeps your web app fast. You do not need to reload the page every time you send or get…

Math.cbrt in JavaScript: How to Finds Cube Roots

JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…

Top 5 Code Editors: The Best IDE for JavaScript

If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…

JavaScript Object References and Copying for Beginners

Object References in JavaScript mean that variables do not store full objects but only their references in memory. What is…

Understanding JavaScript Objects with Examples

JavaScript Objects hold values as key and value pairs, and they help you store and access data. Understand the JavaScript…

JavaScript Ninja Code for Beginners

JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…

JavaScript toSpliced Function Explained with Examples

The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…

JavaScript for Loop: How to Iterate Arrays by Examples

JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…

JavaScript with Function Guide: Syntax with Examples

JavaScript has a statement called "with" that changes how code accesses object properties. The"with" Function makes a temporary scope, so…

Previous Article

How to Use sub Tag for Subscript Text in HTML

Next Article

JavaScript Nullish Coalescing Operator Guide with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.