Mocha - Writing and running tests

Mocha

← Prev

In this tutorial we will write test code and start testing our JavaScript code using Mocha.

In the previous tutorials Mocha - Introduction and Mocha - Setup Mocha for testing we learned how to install Mocha and then set it up for testing. Feel free to check that out.

Creating a simple JavaScript code for testing

In the following code we are creating Box class that will be used to create objects.

We are saving the code inside the file Box.js which is inside the js directory.

var Box = function(length, width, height) {
  this.length = length;
  this.width = width;
  this.height = height;
};

Box.prototype.getVolume = function() {
  return this.length * this.width * this.height;
};

module.exports = Box;

Explanation

Inside the Box.js file we are creating the Box class which will be used to instantiate objects of the Box.

We initialise the Box by passing the length, width and height of the Box to the constructor at the time of object creation.

To get the volume we call the getVolume method.

In the last line we are exporting the Box. This Box will be required in the test code.

Writing the test code

Inside the test directory create a new file by the name Box.test.js. This is were we will write the test code.

At this moment the test file is empty and if we run the command npm run test we will get the following output.

$ npm run test

> mocha-project@1.0.0 test /Users/yusufshakeel/Documents/GitHub/mocha-project
> mocha || true

  0 passing (2ms)

Since we don't have any test so, we get 0 passing test.

Require the assert module

The first thing we will do is requrie the assert module of Node in our Box.test.js file.

The assert module provides simple set of assertion tests.

Next we will require the Box module in our test file.

So, our code will look like the following.

var assert = require('assert');
var Box = require('../js/Box');

Note! We don't have to put the extension .js in the above line var Box = require('../js/Box');. It gets inferred automatically.

Now we will describe our test.

describe('Testing Box', function() {
  // some code goes here ...
})

Note! We use describe to group similar tests.

The first parameter describes the group of tests. The second parameter is a function which holds one or more tests.

Inside the describe we will write our actual tests using the it method.

In this tutorial we will test the volume of the Box.

So, our code will look like the following.

describe('Testing Box', function() {

  it('should assert the volume of the Box', function() {
    // some code goes here...
  })

})

For the it method the first parameter describes the test. The second parameter is a function inside which we write our test code.

Assert value using the equal method

Inside the function body of the it we will write the following code to assert the volume of the Box.

Assume that the box is 10 cm in length, 20 cm in breadth and 30 cm in height.

So, the volume of the box will be 10 x 20 x 30 = 6000 cm3.

To assert this we will write the following code.

var obj = new Box(10, 20, 30);
assert.equal(obj.getVolume(), 6000);

So, we are using the equal method which takes two arguments. First is the actual value and the second is the expected value. If they both match then the test is successful otherwise the test fails.

Final test code

Our Box.test.js file will hold the following code.

var assert = require('assert');
var Box = require('../js/Box');

describe('Testing Box', function() {

  it('should assert the volume of the Box', function() {
    var obj = new Box(10, 20, 30);
    assert.equal(obj.getVolume(), 6000);
  })

})

If we now run the test using npm run test we will get the following output.

$ npm run test

> mocha-project@1.0.0 test /Users/yusufshakeel/Documents/GitHub/mocha-project
> mocha || true

  Testing Box
    ✓ should assert the volume of the Box

  1 passing (8ms)

So, from the above output we can tell that the test is passing and the getVolume() method is working correctly.

← Prev