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.
Optional passing message.
fail(message)
Marks a test as failing, with the given optional message.
Optional failure message.
expectFail(func)
experimental Expect the test enclosed within func
to fail.
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.
An exception.
assert(a, message)
Ensures that a
has a truthy value.
Value to verify.
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.
Another value.
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.
Another value.
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.
Another value.
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.
Another value.
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.
The string to test.
The string should match this regular expression.
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.
A function that should raise an exception when called.
A string or regular expression to compare to the exception's message.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
An object with the following properties:
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()
.
A function to call when testing is complete.