DEV Community

Cover image for Tech Speak: TDD
reduncan
reduncan

Posted on • Updated on

Tech Speak: TDD

TDD? What is TDD? TDD is an acronym for Test Driven Development. 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 Mocha and Chai. 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);
  });
});
  • describe is the function that takes in the function you are testing and the functions for testing your function.
  • it is a function that takes in a string of what the function should do and a function that is constructed using chai.
  • expect 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 expect that you pass an argument into the function so it can be tested.
  • The last part of the function chain is the equal function. The equal function takes in what should be the result of the function as an argument.

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 :)

Discussion (0)