Nodeunit
Simple syntax, powerful tools. Nodeunit provides easy async unit testing for node.js and the browser.
- Simple to use
- Just export the tests from a module
- Works with node.js and in the browser
- Helps you avoid common pitfalls when testing asynchronous code
- Easy to add test cases with setUp and tearDown functions if you wish
- Flexible reporters for custom output, built-in support for HTML and jUnit XML
- Allows the use of mocks and stubs
Contributors
- alexgorbatchev
- alexkwolfe
- azatoth
- kadirpekel
- lambdalisue
- luebken
- orlandov
- Sannis
- sstephenson
- thegreatape
- mmalecki
- and thanks to cjohansen for input and advice on implementing setUp and tearDown functions. See cjohansen's fork.
Also, check out gerad's nodeunit-dsl project, which implements a 'pretty dsl on top of nodeunit'.
More contributor information can be found in the CONTRIBUTORS.md file.
Usage
Here is an example unit test module:
exports.testSomething = function(test) {
test.expect(1);
test.ok(true, "this assertion should pass");
test.done();
};
exports.testSomethingElse = function(test) {
test.ok(false, "this assertion should fail");
test.done();
};
When run using the included test runner, this will output the following:
Installation
There are two options for installing nodeunit:
-
Clone / download nodeunit from github, then:
make && sudo make install
-
Install via npm:
npm install nodeunit -g
API Documentation
Nodeunit uses the functions available in the node.js assert module:
- ok(value, [message]) - Tests if value is a true value.
- equal(actual, expected, [message]) - Tests shallow, coercive equality with the equal comparison operator ( == ).
- notEqual(actual, expected, [message]) - Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
- deepEqual(actual, expected, [message]) - Tests for deep equality.
- notDeepEqual(actual, expected, [message]) - Tests for any deep inequality.
- strictEqual(actual, expected, [message]) - Tests strict equality, as determined by the strict equality operator ( === )
- notStrictEqual(actual, expected, [message]) - Tests strict non-equality, as determined by the strict not equal operator ( !== )
- throws(block, [error], [message]) - Expects block to throw an error.
- doesNotThrow(block, [error], [message]) - Expects block not to throw an error.
- ifError(value) - Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.
Nodeunit also provides the following functions within tests:
- expect(amount) - Specify how many assertions are expected to run within a test. Very useful for ensuring that all your callbacks and assertions are run.
- done() - Finish the current test function, and move on to the next. ALL tests should call this!
Nodeunit aims to be simple and easy to learn. This is achieved through using existing structures (such as node.js modules) to maximum effect, and reducing the API where possible, to make it easier to digest.
Tests are simply exported from a module, but they are still run in the order they are defined.
Note: Users of old nodeunit versions may remember using ok, equals and
same in the style of qunit, instead of the assert functions above. These
functions still exist for backwards compatibility, and are simply aliases to
their assert module counterparts.
Asynchronous Testing
When testing asynchronous code, there are a number of sharp edges to watch out for. Thankfully, nodeunit is designed to help you avoid as many of these pitfalls as possible. For the most part, testing asynchronous code in nodeunit just works.
Tests run in series
While running tests in parallel seems like a good idea for speeding up your test suite, in practice I've found it means writing much more complicated tests. Because of node's module cache, running tests in parallel means mocking and stubbing is pretty much impossible. One of the nicest things about testing in javascript is the ease of doing stubs:
var _readFile = fs.readFile;
fs.readFile = function(path, callback) {
// it's a stub!
};
// test function that uses fs.readFile
// we're done
fs.readFile = _readFile;