unit-test

The unit-test module makes it easy to find and run unit tests.

API Reference

Classes

test

Each function which represents a test case is passed a single argument test, which represents the test runner.

Methods
pass(message)

Marks a test as passing, with the given optional message.

[ message : string ]

Optional passing message.

fail(message)

Marks a test as failing, with the given optional message.

[ message : string ]

Optional failure message.

expectFail(func)

experimental Expect the test enclosed within func to fail.

func : function

A function that should contain a test that is expected to fail.

exception(e)

Marks a test as failing due to the given exception having been thrown. This can be put in a catch clause.

e : exception

An exception.

assert(a, message)

Ensures that a has a truthy value.

a : value

Value to verify.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertEqual(a, b, message)

Ensures that a == b without recursing into a or b.

a : value

A value.

b : value

Another value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNotEqual(a, b, message)

Ensures that a != b without recursing into a or b.

a : value

A value.

b : value

Another value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertStrictEqual(a, b, message)

Ensures that a === b without recursing into a or b.

a : value

A value.

b : value

Another value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNotStrictEqual(a, b, message)

Ensures that a !== b without recursing into a or b.

a : value

A value.

b : value

Another value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertMatches(string, regexp, message)

Ensures that the given string matches the given regular expression. If it does, marks a test as passing, otherwise marks a test as failing.

string : string

The string to test.

regexp : regexp

The string should match this regular expression.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertRaises(func, predicate, message)

Calls the function func with no arguments, expecting an exception to be raised. If nothing is raised, marks the test as failing. If an exception is raised, the exception's message property is compared with predicate: if predicate is a string, then a simple equality comparison is done with message. Otherwise, if predicate is a regular expression, message is tested against it.

func : function

A function that should raise an exception when called.

predicate : string,regexp

A string or regular expression to compare to the exception's message.

[ message : string ]

Depending on the outcome, a test is marked as passing or failing, and message is logged.

assertFunction(a, message)

Ensures that a is a function.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertUndefined(a, message)

Ensures that a is undefined. null, 0, and false will all fail.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNotUndefined(a, message)

Ensures that a is not undefined. null, 0, and false will all pass.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNull(a, message)

Ensures that a is null. undefined, 0, and false will all fail.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNotNull(a, message)

Ensures that a is not null. undefined, 0, and false will all pass.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertObject(a, message)

Ensures that a is an object. A function, string, or number will fail.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertString(a, message)

Ensures that a is a string.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertArray(a, message)

Ensures that a is an array.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

assertNumber(a, message)

Ensures that a is a number.

a : value

A value.

[ message : string ]

The test is marked as passing or failing depending on the result, logging message with it.

waitUntilDone(timeout)

Puts the test runner into asynchronous testing mode, waiting up to timeout milliseconds for test.done() to be called. This is intended for use in situations where a test suite schedules a callback, calls test.waitUntilDone(), and then calls test.done() in the callback.

[ timeout : integer ]

If this number of milliseconds elapses and test.done() has not yet been called, the test is marked as failing.

done()

Marks a test as being complete. Assumes a previous call to test.waitUntilDone().

Functions

findAndRunTests(options)

The list of directories is searched for SecurableModules that start with the prefix test-. Each module matching this criteria is expected to export functions that are test cases or a suite of test cases; each is called with a single argument, which is a Test Runner Object.

options : object

An object with the following properties:

dirs : string

A list of absolute paths representing directories to search for tests in. It's assumed that all of these directories are also in the module search path, i.e. any JS files found in them are SecurableModules that can be loaded via a call to require().

onDone : function

A function to call when testing is complete.