Jake Pruitt

This is where I write.

← Home

Tests

When you hear “Software Testing”, you probably think of code, and tests, and instantly feel nautious. We all have test anxiety in one shape or form, and to add to that anxiety all the fear and mystery that is associated with code makes it ten times worse.

However, testing is (in my opinion) the most important part of a program. It is where you get to define behaviors and make your code as robust as possible. There are certain schools of thought that preach “Test Driven Development”, or TDD, which calls for writing your tests before you write any of your program code. You will initially see tons of failing tests, and then you have to write the code to match the tests, making the tests pass one by one.

I’m not going to cast my vote for or against TDD. That’s a holy war I’d rather avoid. Whether you write your tests before or after the code, you should definitely be writing tests that cover every line of your program code.

So let’s start testing.

Test Prep

The most popular way to set up testing in a Node.js is to download the test runner called Mocha (for all of you sharp cookies out there, you see the humor of a JavaScript tester called Mocha. #nerdhumor). To download it, you can just run:

$ npm install -g mocha

Now you have the mocha command in your console, and can call mocha test.js to run any JavaScript test file, in this case test.js. This allows your test.js file to have access to things that help us define test structures, like describe and it.

In order to test the selection sort algorithm we made yesterday, let’s create a file called selection-sort.test.js, right next to our selection-sort.js file. In the selection-sort.test.js, we’ll write:

// selection-sort.test.js

// These two lines import in the assert library and our
// selection sort file, so that we can call selectionSort
var assert = require('assert');
var selectionSort = require('./selection-sort');

// This line creates a suite of tests for our algorithm
describe('Selection Sort Algorithm', function() {
	
});

Now that we have an empty test, if we run the test, we should have an output of zero tests being run:

$ mocha selection-sort.test.js

//=> 0 passing (0ms)

Awesome, now on to writing individual tests.

Study Guide

We want to test two behaviors of our selection-sort algorithm, sorting numbers and sorting text alphabetically. So we create two it statements, which will be where we put those two feature tests. We also create two arrays to be sorted, all within the describe block:

// selection-sort.test.js
var assert = require('assert');
var selectionSort = require('./selection-sort');

describe('Selection Sort Algorithm', function() {
	var numArray = [11, 7, 10, 1, 4];
	var stringArray = ["a", "ab", "b", "A", "AB", "B"];

	it('should sort a number array', function() {

	});

	it('should sort a string array', function() {

	});
});

Now if we run mocha selection-sort.test.js, we get the following output:

Selection Sort Algorithm
  √ should sort a number array
  √ should sort a string array


2 passing (13ms)

Since our tests don’t have anything in them, they automatically pass.

Flunk ‘em

Now that everything is set up, let’s write some tests for each feature. We should be writing features that are intended to fail until we implement our Selection Sort algorithm correctly. In this case, we will sort the array with our algorithm, and sort the array using the native sort() method. If you have any questions about this section, check out documentation on assert and the Array.prototype.sort method.

it('should sort a number array', function() {
	var selectSorted = selectionSort(numArray);
	var nativeSorted = numArray.sort(function(a, b) { return a - b; });
	var errorMessage = 'The array was not sorted properly.' + 
		'\n\tExpected: ' + nativeSorted + 
		'\n\tBut instead got: ' + selectSorted;

	assert.deepEqual(selectSorted, nativeSorted, errorMessage);
});

The error message is optional, but I included it to show how to get more detail during tests, which is always appreciated. The testing library chai (yes, the coffee puns continue), can help with making testing even easier.

Once you copy this test to the string sorting test, and make a few adjustments to make it reference stringArray rather than numArray, you have a full suite of tests for the desired features.

To learn more about JavaScript testing design patters, check out JavaScript Testing Recipes by James Coglan. It’s first on my reading list.