Tech Speak: TDD
Tags: beginners, javascript, mocha, chai
Published: 2018-11-26 21:39:46

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/n3xhd7ptvooa96u0pn0r.jpg 
title: Tech Speak: TDD
published: true
description: A high level overview of Test Driven Development
tags: #beginners #JavaScript #Mocha #Chai
<meta prefix="og: http://ogp.me/ns#" property="og:image" content="{link to image address}" />
---
TDD? What is TDD? TDD is an acronym for <em>Test Driven Development</em>. So what is test driven development? It is the practice/programming style where developers do not write production code first, instead they write test for a certain unit within their code. These test encompass all aspects of results that can be returned from the unit. These are known as positive and negative test. 

The idea behind TDD is to write only enough unit test to fail, and conversely you only write enough production code for the all test to pass. The kicker is that you are not supposed to write any extra code unless it is needed to pass a test. 

There are many different ways to accomplish unit testing, but today we will be discussing <em>Mocha</em> and <em>Chai</em>. Mocha is a feature-rich JS test framework that runs on Node.js and in the browser. Chai is an assertion library that can be paired with any JS testing framework. In simple terms, an assertion library includes functions to verify that the returned values are correct. Now let's at some code...

So you are working for a company that uses TDD and they have asked you write test for a function called getGPA that takes in an array of numbers and returns the average. We have to start by requiring chai and the function we are going to be testing. 

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

We are requiring chai and the expect style, which allows for chainable language so you can construct assertions, and storing it in a variable name expect. We are also requiring the file in our program that houses the function we are going to test and storing in variable that is the same as our function name. 

Now that we have required the needed tools for testing we can begin writing our test...

    describe('getGPA', function() {
        
      //Positive Test
      it('should find the average of whole numbers', function() {
      expect(getGPA([2, 4, 3])).to.equal(3);
      });
      
      it('should find the average of the decimal numbers', function() {
      expect(getGPA([3.4, 2.8, 4.0, 3.9])).to.equal(3.525);
      });

      //Negative Test
      it('should return -1 for numeric non-array inputs', function() {
      expect(getGPA(3.4, 2.8, 4.0, 3.9)).to.equal(-1);
      });

      it('should return -1 for arrays of string', function() {
      expect(getGPA(['hiya', 'pal'])).to.equal(-1);
      });
    });

<ul>
<li><em>describe</em> is the function that takes in the function you are testing and the functions for testing your function.</li> 
<li><em>it</em> is a function that takes in a string of what the function should do and a function that is constructed using chai.</li> 
<li><em>expect</em> takes in the function you are testing and is chained with functions from the chai library. It is important to note that when passing your function into <em>expect</em> that you pass an argument into the function so it can be tested.</li> 
<li>The last part of the function chain is the <em>equal</em> function. The equal function takes in what should be the result of the function as an argument.</li>
</ul> 

And that is how you write a simple test for a unit of your code. 

We will look at the second part of testing, writing your code to test, next time. 

Until next time :)