Testing w/ Mocha and Chai
Tags: mocha, beginners, chai, javascript
Published: 2018-11-29 03:28:58

---
title: Testing w/ Mocha and Chai
published: true
description: Mocha Chai Testing
tags: #Mocha, #beginner, #Chai, #Javascript
cover_image: https://cdn-images-1.medium.com/max/1600/1*CrB6isZN6YXeM1rWmnjxHw.png
---

##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](https://www.chaijs.com/api/bdd/).

###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.
```javascript
const expect = require('chai').expect;
```

###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.

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

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. 

```javascript
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;
  });
});
```
###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. 

```javascript
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);
  });
});
```
###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.

```javascript
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;
```
###Test.js
We are going to export our function and require it in `test.js`.

```javascript
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;
  });
});
```
###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
```

#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](https://mochajs.org/)
[Chai](https://www.chaijs.com/api/bdd/)