Skip to content

Testing

shz edited this page Jul 7, 2011 · 2 revisions

Testing

Tests are run by executing the test.js file in the root directory. For example:

node test.js

The output should be self-explanatory.

Defining Tests

Tests should be added as simple javascript modules to the testing directory. These will be automatically loaded by runner.js, which contains the testing logic.

In order to be tested, the module must contain two exports: desc, which should be a string describing the test module, and run, which will be called with the test runner and should perform the actual testing.

Writing Tests

The test runner object contains all the magic. It is capable of running simple boolean tests:

r.test('true is true', true);
r.test('false is false', false == false);
r.test('foo is defined', !!foo.bar)

It is also capable of testing other functions:

r.test('Utility Tests', testUtil);

If an exception is thrown in a function being tested (including a test module's run function), the runner will handle it accordingly.

Deferred Tests

The test runner has a method defer which allows tests to be run across callbacks. The function takes the test description string as the first parameter, and optionally a timeout length as the second parameter (default 2000ms), which is the name of the test, and returns an object with a single method, done, which should be called with the result of the test to mark it complete. For example:

var d = r.defer('callback fires'); //default timeout 2000ms
setTimeout(function() { d.done(true) }, 1000);

And using timeout:

//this test will fail
var d = r.defer('callback fires', 1000); //explicit timeout 1000ms
setTimeout(function() { d.done(true) }, 2000);

Example

Below is an example test module.

var testBoolean = function(r) {
    r.test('true is true', true);
    r.test('false is false', false == false);
};
var testTimeout = function(r) {
    var d = r.defer('timeout fires');
    setTimeout(function() {
        d.done(true);
    }, 1000);
};

exports.desc = "Example Tests";
exports.run = function(r) {
    r.test('boolean tests', testBoolean);
    r.test('timeout tests', testTimeout);
};
Clone this wiki locally