ServerJS/Unit Testing/A

From MozillaWiki
Jump to: navigation, search

Assert

The assert module provides functions that throw AssertionError's when particular conditions are not met. The assert module must conform to the following interface.

var assert = require("assert");

The AssertionError is defined in assert.

new assert.AssertionError(message, actual, expected)
assert.AssertionError instanceof Error

All of the following functions may throw an AssertionError when a corresponding condition is not met, with a message that may be undefined if not provided. All assertion methods provide both the actual and expected values to the assertion error for display purposes.

The equivalence assertion tests a deep equality relation. All identical values are equivalent, as determined by ===. For pairs of values that both pass Array.isArray, equivalence is determined by identical length and the equivalence of all respective content. Other pairs that do not both pass typeof value == "object", equivalence is determined by ==. For all other Object pairs, equivalence is determined by having the same number of owned properties (as verified with Object.prototype.hasOwnProperty.call), the same set of keys (although not necessarily the same order), equivalent values for every corresponding key, and an identical "prototype" property.

assert.eq(expected, actual, [message]); 

Non-equivalence:

assert.ne(expected, actual, [message]); 

The identity assertion tests shallow identity with ===.

assert.is(expected, actual, [message]);

Non-identity:

assert.isnt(expected, actual, [message]);

The truth assertion tests whether a value is identical to true, as determined by the === operator.

assert.isTrue(actual, [message]);

Falsity:

assert.isFalse(actual, [message]);

The nullity assertion tests whether a value is identical to null, as determined by the === operator. undefined is equivalent but not identical to null, so checking whether a value is null or undefined can be determined by assert.eq(value).

assert.isNull(actual, [message]);

The truthiness assertion tests whether a value is equivalent to true, as determined by the == operator.

assert.eqTrue(actual, [message]);

Falsiness:

assert.eqFalse(actual, [message]);

Not a number:

assert.isNaN(value, [message]);

Expected to throw an error:

assert.error(block, [message]);

Expected to throw a particular type of error, distinguished by arguments length.

assert.error(block, ErrorType, [message]);

Test

The "test" module provides a "run" method that runs unit tests and catalogs their results. The "run" method must return the total number of failures, suitable for use as a process exit status code. The idiom for a self-running test module program would be:

if (module.id == require.main) 
  require("test").run(exports); 

run must accept any Object, usually a unit test module's exports. "run" will scan the object for all methods and object properties that have names that begin with "test", and certain flags. Sub-objects will be run as sub-tests.

exports.testPatience(function () {
  require('os').sleep(1000);
});
exports.subTest = require("./subTest");

Test names may be any String that begins with "test", not necessarily respecting a case convention.

[
  [{"a: 10}, {"a": 10}],
  [[1, 2, 3], [1, 2, 3]]
].forEach(function (pair) {
   exports['test ' + util.repr(pair[0])] = function () {
     assert.eq.apply(assert, pair);
   };
});

The addsGlobals flag must suppress the test runner from failing any test that adds a property to the global object. Otherwise, the test runner may make this check to spot likely missing var declarations.

exports.addsGlobals = true;