An assertion object must conform to the following specification.
Note: the "assert"
module identifier is conventionally reserved
for an assertion module.
-
The assertion object must have an
AssertionError
constructor function.-
All instances of
AssertionError
must be instances ofError
. -
An
AssertionError
must be instantiated with an options object describing the failure.-
Assertion options must have a
message
string. -
Assertion options must have an
actual
value. -
Assertion options must have an
expected
value, or string describing the expected value or range of values. -
Assertion options may have an
operator
string, that may represent any JavaScript operator or function name used to test whether the actual value met the expectation.
-
-
-
In every assertion function, given its criterion for passing or failing,
-
If
this
has apass
function property and the criterion is met, the assertion function must callpass
with the given message. -
If
this
has afail
function property and the criterion is not met, the assertion function must callfail
with anAssertionError
object with the correspondingmessage
,actual
value,expected
value description, andoperator
if applicable. -
If
this
does not have afail
function property and the criteron is not met, the assertion function must throw anAssertionError
object with the correspondingmessage
,actual
value,expected
value description, andoperator
if applicable.
-
-
The assertion object must have a function
ok(guard, message_opt)
.-
The criterion of
ok
is!!guard
. -
The corresponding
operator
for anAssertionError
is"=="
. -
The corresponding
expected
value for anAssertionError
istrue
.
-
-
The assertion object must have a function
equal(actual, expected, message_opt)
-
The criterion of
equal
is thatactual == expected
. -
The corresponding
operator
for anAssertionError
is"=="
.
-
-
The assertion object must have a function
notEqual(actual, unexpected, message_opt)
-
The criterion of
notEqual
is thatactual != expected
-
The corresponding
operator
for anAssertionError
is"!="
.
-
-
The assertion object must have a function
deepEqual(actual, expected, message_opt)
-
The corresponding
operator
for anAssertionError
is"deepEqual"
. -
The criterion for
deepEqual
is that:-
If
expected == actual
, they are deeply equal. -
If the expected value is a
Date
, they are deeply equal only if the actual value is aDate
representing the same time. -
If the
typeof
actual
orexpected
is not"object"
, they are deeply equal ifexpected == actual
. -
Otherwise, for all objects, including arrays, they are deeply equal only if they have the same number of owned, enumerable properties, the same property names (although not necessarily in the same order), and the respective values for each key are deeply equal.
-
-
-
The assertion object must have a function
notDeepEqual(actual, unexpected, message_opt)
-
The corresponding
operator
for anAssertionError
is"notDeepEqual"
. -
The criterion for
notDeepEqual
is that the actual and expected values do not meet the criterion ofdeepEqual
.
-
-
The assertion object must have a function
strictEqual(actual, expected, message_opt)
-
The criterion of
strictEqual
is thatexpected === actual
. -
The corresponding
operator
for anAssertionError
is"==="
.
-
-
The assertion object must have a function
notStrictEqual(actual, expected, message_opt)
-
The criterion of
notStrictEqual
is thatexpected !== actual
. -
The corresponding
operator
for anAssertionError
is"!=="
.
-
-
The assertion object must have a function
error(callback, Error_opt, message_opt)
Note: in the CommonJS specification, this was namedthrows
, however this name was not ergonomic before ECMAScript 5. Assertion objects may providethrows
for backward compatibility with that specification provided that they note that it is deprecated.-
The criterion for
error
is-
If
Error
is provided, callingcallback
must throw an error that is an instance of the givenError
. -
If
Error
is not provided, callingcallback
must throw an exception.
-
-
The corresponding
operator
for anAssertionError
is"throws"
. -
The corresponding
actual
value for anAssertionError
isundefined
if callback does not throw an error. -
The corresponding
actual
value for anAssertionError
is the exception thrown if the callback does throw an error. -
The corresponding
expected
value for anAssertionError
is the globalError
if theError
argument is undefined. -
The corresponding
expected
value for anAssertionError
is the givenError
argument if defined.
-
-
Custom assertion objects may provide additional assertion functions. Note: to use an alternate assertion object for logging, a test object must specify an
Assert
constructor property
-
A test function,
-
A test function may accept an assertion object as its first argument, indicating that the test may log results rather than fail at the first exception.
-
A test function of
length
2 must accept a completion function, indicating that the test will end when the test calls the completion function. -
If a test function does not have
length
2, it may return a promise to complete.- The completion value may be undefined.
-
-
A test object,
-
May have an
Assert
constructor function for providing custom assertions in the context of contained tests. -
All owned, enumerable properties of a test object that have names that start with
"test"
must be test objects or functions.
-
Note: by convention, the "test"
module in a package is reserved
for a test runner.
-
A test runner object must have a function
run(tests, log_opt)
-
If
log
is undefined,run
must construct a default logger object. -
If
tests
has anAssert
constructor function property,run
must use this function to create assertion objects. -
For each enumerable property of
tests
that starts with"test"
,run
must execute the corresponding test, proceeding to the next test only when the previous ends.-
If the corresponding value is a function,
-
Prepare: The test runner must create a logger object by calling
log.section()
. -
The test runner must create an assertion object and connect its
pass
andfail
function properties to thepass
andfail
properties of the constructed logger. -
Run: The test runner must call the test function with an assertion object and a completion function.
-
End: If a test function does not return a promise, the test ends upon returning.
-
If the test function returns a promise, the test ends when the promise is resolved. Note: resolution is either fulfillment or rejection.
-
If the
length
of the test function is2
, the test must eventually call the completion function and must not return a promise.- The test ends when the test calls the completion function.
-
Fail: If the test function throws an exception, or if the promise returned by the test function is rejected,
-
If that exception (or reason for rejection) is an instance of the
AssertionError
in the assertion object, the runner must call thefail
function oflog
with the exception. -
Otherwise, the runner must call the
error
function of thelog
with the given exception or reason for rejection.
-
-
Pass: If a test ends without throwing an exception or rejecting the returned promise, the runner must call the
pass
function of thelog
object with the key of the test function property.
-
-
If the corresponding value is an object,
-
run
must create a new logger by callinglog.section()
. -
run
must call itself with the test and logger object.
-
-
-
run
must return a promise.-
The promise must be fulfilled when the last contained test ends.
-
The last contained test of a test object with no properties is already ended.
-
-
-
A logger object must have a
pass
function property. -
A logger object must have a
fail
function property. -
A logger object must have an
error
function property. -
A logger object must have a
section
function property that, when called, returns a new logger object.