ToDo or Die — Learning TDD

Olushi Luqman O.
4 min readSep 26, 2018

In preparation for Andela Bootcamp, I learned a lot and I still am. During the first week alone, I had to unlearn and relearn many of the things I already know. In the course of my learning, I picked up an important skill that’s expected of every developer who wants to build and ship better products in the market today. That skill is TDD (Test-Driven development), a software development process that relies on the repetition of a very short development cycle.

Although the title of this article is rather misleading, it is, however, what I told myself when I heard we’re going to be writing lots of tests at Bootcamp. Andela is a company focused on excellence, so it’s important to instill in their developers the importance of testing a product to ensure its quality and success.

Below is just a fraction of the things I learned about testing while preparing for Bootcamp.

What is software testing?

Software testing is an investigation conducted to provide stakeholders with information about the quality of the software product or service under test. — Wikipedia

To reinforce my understanding about why testing is needed, I wrote a medium article about it here https://medium.com/@codeshifu/the-need-to-test-shipping-better-softwares-56696609c4ef

What is TDD (Test-Driven Development)?

Test-driven development (TDD) is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfil that test and refactoring. In other words, it’s one way to think through your requirements or design before you write your functional code.

The 3-step process of TDD

  1. Write a test, watch it fail
  2. Write code to make the test pass
  3. Refactor the code
3-step process of TDD — Image credit dywl

How to do TDD

To actually see how test-driven development works, let’s build a simple app.

Prerequisite

NodeJS

Project requirement

Create a basic calculator app that’s able to do addition. The app must be able to correctly add numbers together.

The requirement for this app is quite simple and straightforward, we just need to add two numbers together right? Not quite, Javascript is a dynamically typed language and what that means is we don’t explicitly set types for variables. It is one of the flexibility of the language and one of the reasons for its bad reputation over the years.

// Addition in javascript2 + 2 = 4;2 + “2” = 22; // oh oh!

Getting started

mkdir calculator-app && cd calculator-app

(this creates a directory called calculator-app and then moves you into it)

npm init -y

(creates a package.json file, we ignore stdin prompt with -y flag and accept the default)

Next, we’ll install a test framework & assertion library called mocha & chai.

npm install mocha chai — save-dev

In the package.json file we’ll add an npm script to run our test


“scripts”: {
“test”: “mocha”
}

Write a test that fails

Next, create a test directory in the project (calculator-app) root folder and create a file (under the just created test directory) called addition.spec.js.

Inside the addition.spec.js let’s create our test and watch it fail


const chai = require(‘chai’);
describe(‘#add’, () => {
it(‘should add two numbers correctly’, () => {
expect(add(2, 2)).to.equal(4);
});
});

If we run our test with npm run test it’ll fail. Great! First phase of TDD process complete.

failing test

Write code to pass the test

Create a file called addition.js file in our project root folder and add this simple function.


function add (a, b) {
return a + b;
}
module.exports = add;

Let’s update our addition.spec.js file to use the newly created function


const chai = require(‘chai’);
const add = require(‘../addition’);
describe(‘#add’, () => {
it(‘should add two numbers together correctly’, () => {
expect(add(2, 2)).to.equal(4);
});
it(‘should throw error for invalid argument’, () => {
expect(() => add(‘4’, 2)).to.throw(‘Invalid argument’);
});
});

Let’s re-run the test with npm run test and woohoo! 1 of 2 test passes.

test passes

Refactor

The last step of our TDD process is to refactor our add function in addition.js file.


function add (a, b) {
if (typeof a !== ‘number’ || typeof b !== ‘number’) throw new TypeError(‘invalid argument’);

return a + b;
}
module.exports = add;

Now if we run our test with npm run test everything passes.

all test passess

Thanks for reading, and see you soon.

--

--