DEV Community

Cover image for Testing w/ Mocha and Chai
Matthew Carpenter
Matthew Carpenter

Posted on

Testing w/ Mocha and Chai

What is Mocha?

Mocha is a Javascript test framework running on Node.js and in the browser. The goal here is to give you a basic understanding of how Mocha works by writing your first test. In the example we will be creating a function that takes in 2 parameters (numbers) and returns the sum of the two parameters.

You mentioned Chai Latte?

Yes, Chai is a BDD/TDD assertion library for node and the browser. Chai can be paired with any Javascript testing framework. Chai code makes writing test more readable and easier to write. Chai also has great documentation.

Onward!

Ok, first we need to install mocha and chai. Then create a test.js file, this is where we will write our test.

npm install -g mocha chai

Next, we will require Chai in our test.js file.

const expect = require('chai').expect;
Enter fullscreen mode Exit fullscreen mode

What are we even testing for?

Well, we are creating a function for some program that will take in two numbers, and return the sum of those two numbers. What happens if the numbers that are passed are string? Or an array of numbers? what if they are negative values? Well, we create cases based on this criteria than build our function to pass the tests.

Positive test

We begin by calling the function describe(), which is for grouping. Describe takes in a string and a callback. Inside describe we call it() which are our test cases. See code example below.

describe('add', function() {
  it('should return the sum of two positive numbers', function() {
    expect(add( 2, 4 )).to.equal(6);
  });
});
Enter fullscreen mode Exit fullscreen mode

Now the code above should be natural and easy to read thanks to Chai. I'm describing the 'add' function. It should return the sum of two positive numbers. I expect the parameters 2 and 4 to equal 6. This is an example of a positive test.

Negative test

Next, let's add a negative test case.

describe('add', function() {
  it('should return the sum of two positive numbers', function() {
    expect(add( 2, 4 )).to.equal(6);
  });

  it('should return NaN if passed a word string', function() {
    expect(add( "hello", "pal" )).to.be.NaN;
  });
});
Enter fullscreen mode Exit fullscreen mode

Array test

Let's do one more, what if the numbers that are passed are inside an array? How should we handle this? Should we throw an error or sum the numbers? Let's opt to sum the numbers and return the result.

describe('add', function() {
  it('should return the sum of two positive numbers', function() {
    expect(add( 2, 4 )).to.equal(6);
  });

  it('should return NaN if passed a word string', function() {
    expect(add( "hello", "pal" )).to.be.NaN;
  });

  it('should return the sum of an array of numbers', function() {
    expect(add([1,2,3,4,5])).to.equal(15);
  });
});
Enter fullscreen mode Exit fullscreen mode

Build our function

Lastly, we need to build our function to pass the following test. Then we can run our test.js file and see if they are passing.

const add = function (num1, num2) {

  if ( Array.isArray(num1) ){
    let sum = 0;
    for ( let i = 0; i < num1.length; i++){
      sum += num1[i];
    }
    return sum;
  }
  else if ( isNaN(num1, num2) ){
    return NaN
  }

  return parseFloat(num1) + parseFloat(num2);

}

module.exports = add;
Enter fullscreen mode Exit fullscreen mode

Test.js

We are going to export our function and require it in test.js.

const expect = require('chai').expect;
const add = require('./add.js');

describe('add', function() {
  it('should return the sum of two positive numbers', function() {
    expect(add(2, 4)).to.equal(6);
  });

  it('should return the sum of an array of numbers', function() {
    expect(add([1,2,3,4,5])).to.equal(15);
  });

  it('should return NaN if passed a string', function() {
    expect(add('hello', 'pal')).to.be.NaN;
  });
});
Enter fullscreen mode Exit fullscreen mode

Let's run our test.js

In your command line you will need to type mocha test.js to run the following test on our function to see if they pass. If done correctly you should get three green check marks which indicate 3 passed test.

  add
    ✓ should return the sum of two positive numbers
    ✓ should return the sum of an array of numbers
    ✓ should return NaN if passed a string

  4 passing (9ms)
  1 pending
Enter fullscreen mode Exit fullscreen mode

conclusion

Hey, you made it! These are the basics of TDD/BDD using Mocha and Chai. Check the links below to learn more about Mocha/Chai.

Mocha
Chai

Discussion (0)