Mocha Chai - Writing and running tests

Mocha Chai

← Prev

In this tutorial we will write some JavaScript code and then test them using Mocha and Chai.

In the previous tutorials Mocha Chai - Introduction and 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 Box class for testing

Lets go ahead and create the js directory inside the project folder. And then create the Box.js file inside the js directory and write the following code.

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

We are creating the Box class in the above code.

At the time of initialising the Box we will pass the length, width and height of the Box to the constructor.

To get the volume of the Box we can call the getVolume method.

In the last line we are exporting Box. So, in the test file we will require this Box.

Test code

Create a new file by the name Box.test.js inside the test directory.

We will write our tests inside this file.

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

$ npm run test

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

  0 passing (2ms)

Note! Since we don't have any test inside the Box.test.js file so, we are getting 0 passing in the output.

Require the assert from Chai module

Inside the Box.test.js file require assert from the Chai module.

The assert of Chai is similar to the Node.js assert but with some extra feature.

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

At this moment our test file will look like the following.

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

Note! We are not using the extension .js in the above line var Box = require('../js/Box');. It gets inferred automatically.

describe test

We use describe to group similar tests and it takes two arguments.

The first one describes the group of tests while the second one is a function and inside it we write some tests.

So, lets go ahead and describe our tests by writing the following code inside the Box.test.js file.

describe('Testing Box', function() {

  // some code goes here...

})

Using it to test

We write our test using the it method. And it takes two arguments. The first one describes the test and the second one is a function and inside it we write the test.

So, lets assert whether an object created using the Box class is an instance of the class.

For this we will use the instanceOf() method.

It takes two arguments. The first one is the object and the second one is the constructor.

describe('Testing Box', function() {

  it('should assert obj is instance of Box', function() {
    var obj = new Box(10, 20, 30);
    assert.instanceOf(obj, Box);
  })

})

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

$ npm run test

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

  Testing Box
    ✓ should assert obj is instance of Box

  1 passing (9ms)

So, our test is passing.

Awesome!

One more test to check the volume

We can write another test using it to check the getVolume method of the Box class.

To match the value we will use the equal method that takes two arguments. The first one is the actual value and the second one is the expected value.

To test the getVolume method we will pass 10, 20 and 30 as the length, width and height at the time of creating an object of the Box class.

The expected result is 10 x 20 x 30 = 6000.

So, if the actual value and expected value matches then the test will pass.

Our next code will look like the following.

it('should assert volume of the box to be 6000', function() {
  // create an object
  var obj = new Box(10, 20, 30);

  // now assert the volume
  assert.equal(obj.getVolume(), 6000);
})

Final test code

Our Box.test.js file will now look like the following.

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

describe('Testing Box', function() {

  it('should assert obj is instance of Box', function() {
    var obj = new Box(10, 20, 30);
    assert.instanceOf(obj, Box);
  })

  it('should assert volume of the box to be 6000', function() {
    // create an object
    var obj = new Box(10, 20, 30);

    // now assert the volume
    assert.equal(obj.getVolume(), 6000);
  })

})

On running the test we will get the following output.

$ npm run test

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

  Testing Box
    ✓ should assert obj is instance of Box
    ✓ should assert volume of the box to be 6000

  2 passing (10ms)

So, we can see that both the tests are passing.

← Prev

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT