Mocha

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

Mocha

Mocha and CBT

Mocha is a powerful and easy-to-use testing framework for Node.js. Its power comes from its flexibility. With support for all the popular test writing styles (BDD, TDD, Exports, QUnit or Require) and seemingly endless ways to output test results, it is no wonder Mocha has a 94% satisfaction rating among those who have used it.

Install and setup

Unlike other frameworks, it is dead simple to get started with Mocha. If you do not have npm, you can get it here.

To install Mocha globally:

$ npm install -g mocha

To install Mocha as a project dependency:

$ npm install --save-dev mocha

Once Mocha is installed, the only other setup you will need to do is create a test directory in your project root. That is where Mocha will look for *.js (or, alternatively, *.coffee) files to run.

Optional: install an assertion library

You can use Node's built-in assert() function in your automated tests, but great tests require more nuanced assertions than assert() allows. If you have a favorite assertion library, go ahead and use it. If you do not, I recommend Chai. Install it the same way we installed Mocha:

Globally:

$ npm install -g chai

Or for your project:

$ npm install --save-dev chai

Write tests

For this example we will be testing a simple login page to make sure that it rejects bad login attempts, and accepts good login attempts.

Definitely take a look at the example test in this repo! It is heavily commented, so that it is easy to read and understand.

The basic structure of each test is as follows:

  • Each test suite, or "feature", is enclosed in a describe() block.

  • For each describe() block, you can call special methods before(), after(), beforeEach(), and afterEach(). Those methods will run before the whole suite, after the whole suite, before each test case, and after each test case, respectively.

  • Tests are defined by calling it() inside a describe() block.

  • Each function inside a describe() block will block execution of other tests until done() is called.

Here is an example of a simple test of a function that adds two numbers together and returns a promise. Before each test we call a reset method on the adder. We also have a cleanUp function at the bottom to where we would put any test cleanup code.

describe("number adder"{
    beforeEach(function resetAdder(done){
        numberAdder.reset()
        .then(done);
    });
    it("returns the sum of two positive numbers", function(done){
        numberAdder(7,3)
        .then(function(result){
            assert(result == 10);
        })
        .then(done);
    });
    it("returns the sum of a positive and negative number", {
        numberAdder(7,-3)
        .then(function(result){
            assert(result == 4);
        })
        .then(done);
    });
    after(function cleanUp(done){
        // any test cleanup
        done();
    })
});

Take a look at login.js and you will see a very similar structure.

Run tests

To run your tests, just navigate to the root project folder and run $ mocha. It is that easy!

Help!

If you got stuck, or something does not make sense, do not worry! Just shoot an email to [email protected] and we will help you out.

Happy testing!

See Also

Test Frameworks and Tools
About Selenium Testing
Selenium and JavaScript
CucumberJS
InternJS
Jasmine
Jest
Karma
NightwatchJS
Protractor
WebDriverIO and CrossBrowserTesting

Highlight search results