Getting Started With Node.js Mocha Testing

  • by
Nodejs Mocha Test

Automated testing has become a critical part of any application. One of the most popular Node.js testing frameworks that helps teams create this automated confidence is Mocha. In this article we’ll first install and setup Mocha and then move on to breaking down how to write a basic test in Mocha.

Prerequisites

  • Yarn or npm installed
  • Node.js installed
  • Basic understanding of Javascript

Setting up Mocha

1.) Use npm or yarn to install Mocha. If you’re working in a new directory this should automatically create a package.json file and node_modules directory for you.

$ yarn add mocha

Or

$ npm install mocha

2.) Make a new directory for your tests to live in. Note that this assumes you are currently in the root (top) directory of your project.

$ mkdir test

3.) Create a test.js file in your new test directory (test/test.js). For this example we are importing the Node.js assertion library and then using it to write our first test. We’ll get into the syntax of the test later, but for now you can copy + paste this into your test.js file.

var assert = require("assert");

describe("Hello World", function () {
  it("should return the string Hello World", function () {
    assert.equal("Hello World", "Hello World");
  });
});

4.) Add a script to our package.json to run our tests. Your package.json should look similar to the following.

{
  "scripts": {
    "test": "mocha"
  },
  "dependencies": {
    "mocha": "^8.2.1"
  }
}

5.) Finally we’ll confirm that we set everything up correctly by running our tests with `yarn test` or `npm test`.

$ yarn test
yarn run v1.12.3
 mocha
  Hello World
    ✓ should return the string Hello World
  1 passing (3ms)

Done in 0.17s.

Perfect! We see our Hello World test is passing as we expected. And we now know we can always call `yarn test` or `npm test` to run our tests!

Mocha Test Syntax

Let’s create a basic Mocha test an break down each part. We’ll start with the test below.

describe("Name of Test", function () {
  it("What the test should do or return", function (done) {
    // do something
    done();
  });
});

Our test starts with a describe block. Describe is a way for Mocha to help label your test, it takes a string as the name of your test and then defines a function to run.

The `it` call helps you quickly figure out what it is your test is expecting. That way if your test fails you can quickly determine the return value and what the test was expecting for the return value.

Within our `it` call we also define a function and tell it to expect a call to `done()` when it’s completed running. If your function calls done() without passing a parameter it indicates a success. If it passes a parameter back then Mocha determines that your test has failed (i.e. done(“error”) ).

Wrap-up

This short article should provide you with the tools necessary to start writing up some Mocha tests. For more information I would encourage you to explore the official Mocha documentation and just start writing some tests!

Leave a Reply

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