Testing code

At my first hackathon I heard much about testing the code while developing it with tools like Jasmine or Mocha. So I looked a bit into it and this is a kind of quick introduction of Jasmine, or at least how I got started while using Node.js.

A basic understanding of Node is necessary

In the following example I am using jasmine-node to test my functions. The program I wrote is a text transformation tool. The best thing to do when starting to develop something new is to think about the tests as the first thing. Knowing what you want to test makes it more easily to write the actuall code.

In my example I first thought about the things I want to do with my tool: flip the text horizontally as well as vertically, lowercase and uppercase it.

I will skip explaining the real code because the tests are important part. However here is the code for flipping the text vertically to which we will look at the test later:

righttoleft: function ( original ) {  
  var temp_string = original.split('');
  var string = [];
  for (var i = temp_string.length; i >= 0; i--) {
    string.push(temp_string[i]);
  }
  string = string.join('');
  return string;
}

https://github.com/dervondenbergen/testing-code/blob/master/functions.js#L28-L46

This is part of an object, which is just a module. It is very important to split the functions you want to test in different files or at least in an external module, because you need them in the test files.


When using Jasmine all the tests have to be in a folder called spec. Those files have to be *.coffee or *.js files. It is important that all files have a name like *-spec.js . Otherwise the test wouldn't get recognized by Jasmine.

The test specs for my flipping function looks like this:

var fn = require('../functions');  
describe('Text should be reversed:\n', function() {  
  it('It should be from right to left.', function() {   
    var original = 'chocolate';
    var result   = 'etalocohc';
    var reversed = fn.righttoleft(original);
    expect(result).toBe(reversed);
  });
});

https://github.com/dervondenbergen/testing-code/blob/master/spec/righttoleft-spec.js


spec.js

It is easier to understand if the file is split up in different parts.

var fn = require('../functions');

We can easily use our functions, because they are just a module, and so we won't have problems running the test on them.


describe

reference

describe starts a so called suite. It has a name, which will be the name of what will be tested. Beside the name, there also has to be a function. In our case is the name Text should be reversed: .

describe('Text should be reversed:\n', function(){  
  …
});

https://github.com/dervondenbergen/testing-code/blob/master/spec/righttoleft-spec.js#L3


it

reference

it defines a suite better. There can be multiple so called specs in one describe. The specs tell what is acutally tested in a suite. This doesn't make much sense for our example, but it eventually makes more sense for bigger programs.

it('It should be from right to left.', function() {  
  …
});

https://github.com/dervondenbergen/testing-code/blob/master/spec/righttoleft-spec.js#L5


expect

reference

expect is always bundeled with a so called matcher. One matcher is for example .toBe(). This function looks if something is like something else. We just compare a manually transformed string with one from the function.

var original = 'chocolate';  
var result   = 'etalocohc';  
var reversed = fn.righttoleft(original);

expect(result).toBe(reversed);  

Declaring the variables before testing them isn't required, the code above can be compromised. The disadvantage is that the code is not so beautiful.

expect('etalocohc').toBe(fn.righttoleft('chocolate'));  

https://github.com/dervondenbergen/testing-code/blob/master/spec/righttoleft-spec.js#L7-L12


Those are the basic things, that someone has to know to start with writing test. To actually run them you have to use the already mentioned jasmine-node. To install it globally, where it is needed, just run npm install jasmine-node -g.

In the folder of your project you have to run jasmine-node spec/ or jasmine-node spec/ --verbose to have a better overview of the tests.
jasmine-node spec/ --verbose output A full list of all flags and options is available at the jasmine-node README.md file.

A running example of the tool is online at examples.demont.is/testing-code. The full source code and all the specs can be viewed on GitHub.

comments powered by Disqus