# assert
A Node.js-compatible implementation of the core assert
module
NOTE
This is an abstract type. Any object of this structure can be used where this type is used.
# Overview
Titanium provides a number of shims and ports of core Node.js module functionality.
This module is intended to provide a Node-compatible port of the assert
core module.
More details on the Node.js API can be found in their assert module documentation (opens new window)
Note: The assert
module can also be invoked as a function and is equivalent to calling the assert.ok method:
const assert = require('assert');
assert(value, 'message'); // equivalent to assert.ok(value, 'message')
# Properties
# strict
an alias of assert
that enforces strict version of comparison methods
# Methods
# deepEqual
Tests for deep equality between the actual
and expected
parameters.
Consider using assert.deepStrictEqual()
instead.
assert.deepEqual()
can have surprising results.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# deepStrictEqual
Tests for deep equality between the actual
and expected
parameters.
"Deep" equality means that the enumerable "own" properties of child objects are recursively evaluated also by the following rules.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# doesNotReject
Awaits the asyncFn
promise or, if asyncFn
is a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is not rejected.
If asyncFn
is a function and it throws an error synchronously, assert.doesNotReject()
will return a rejected Promise
with that error. If the function does not return a promise, assert.doesNotReject()
will return a rejected Promise
with an ERR_INVALID_RETURN_VALUE
error. In both cases the error handler is skipped.
Using assert.doesNotReject()
is actually not useful because there is little benefit in catching a rejection and then rejecting it again. Instead, consider adding a comment next to the specific code path that should not reject and keep error messages as expressive as possible.
If specified, error
can be a Class
, RegExp
or a validation function. See assert.throws()
for more details.
Besides the async nature to await the completion behaves identically to assert.doesNotThrow()
.
Parameters
Name | Type | Description |
---|---|---|
asyncFn | Function | Promise<Object> | function to invoke |
error | RegExp | Function | The expected |
message | String | error message |
Returns
- Type
- void
# doesNotThrow
Asserts that the function fn
does not throw an error
.
Using assert.doesNotThrow()
is actually not useful because there is no benefit in catching an error and then rethrowing it. Instead, consider adding a comment next to the specific code path that should not throw and keep error messages as expressive as possible.
When assert.doesNotThrow()
is called, it will immediately call the fn
function.
If an error is thrown and it is the same type as that specified by the error
parameter, then an AssertionError
is thrown. If the error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
If specified, error
can be a Class, RegExp or a validation function. See assert.throws()
for more details.
Parameters
Name | Type | Description |
---|---|---|
fn | Function | function to invoke |
error | RegExp | Function | The expected |
message | String | error message |
Returns
- Type
- void
# equal
Tests shallow, coercive equality between the actual and expected parameters using the Abstract Equality Comparison (==
).
NaN
is special handled and treated as being identical in case both sides are NaN
.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# fail
Throws an AssertionError
with the provided error message or a default error message.
If the message
parameter is an instance of an Error
then it will be thrown instead of the AssertionError
.
Parameters
Name | Type | Description |
---|---|---|
message | String | Error | error message |
Returns
- Type
- void
# ifError
Throws value
if value
is not undefined
or null
.
This is useful when testing the error argument in callbacks.
The stack trace contains all frames from the error passed to ifError()
including the potential new frames for ifError()
itself.
Parameters
Name | Type | Description |
---|---|---|
value | any | potentially thrown value/object |
Returns
- Type
- void
# notDeepEqual
Tests for any deep inequality. Opposite of assert.deepEqual()
.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# notDeepStrictEqual
Tests for deep strict inequality. Opposite of assert.deepStrictEqual()
.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# notEqual
Tests shallow, coercive inequality with the Abstract Equality Comparison (!=
).
NaN
is special handled and treated as being identical in case both sides are NaN
.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# notStrictEqual
Tests strict inequality between the actual and expected parameters as determined by the SameValue Comparison.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# ok
Tests if value
is truthy. It is equivalent to assert.equal(!!value, true, message)
.
If value
is not truthy, an AssertionError
is thrown with a message property set equal to the value of the message
parameter.
If the message
parameter is undefined
, a default error message is assigned.
If the message
parameter is an instance of an Error
then it will be thrown instead of the AssertionError
.
If no arguments are passed in at all message will be set to the string: 'No value argument passed to \
assert.ok()`'`.
Parameters
Name | Type | Description |
---|---|---|
value | any | The input that is checked for being truthy. |
message | String | Error | error message |
Returns
- Type
- void
# rejects
Awaits the asyncFn
promise or, if asyncFn
is a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.
If asyncFn
is a function and it throws an error synchronously, assert.rejects()
will return a rejected Promise
with that error. If the function does not return a promise, assert.rejects()
will return a rejected Promise
with an ERR_INVALID_RETURN_VALUE
error. In both cases the error handler is skipped.
Besides the async nature to await the completion behaves identically to assert.throws()
.
If specified, error
can be a Class
, RegExp
, a validation function, an object where each property will be tested for, or an instance of error where each property will be tested for including the non-enumerable message
and name
properties.
If specified, message
will be the message provided by the AssertionError
if the asyncFn
fails to reject.
Parameters
Name | Type | Description |
---|---|---|
asyncFn | Function | Promise<any> | function to invoke |
error | RegExp | Function | Object | Error | The expected |
message | String | error message |
Returns
- Type
- void
# strictEqual
Tests strict equality between the actual and expected parameters as determined by the SameValue Comparison.
Parameters
Name | Type | Description |
---|---|---|
actual | any | The actual value |
expected | any | The expected value |
message | String | Error | error message |
Returns
- Type
- void
# throws
Expects the function fn
to throw an error.
If specified, error
can be a Class
, RegExp
, a validation function, a validation object where each property will be tested for strict deep equality, or an instance of error where each property will be tested for strict deep equality including the non-enumerable message
and name
properties.
When using an object, it is also possible to use a regular expression, when validating against a string property. See below for examples.
If specified, message
will be appended to the message provided by the AssertionError if the fn call fails to throw or in case the error validation fails.
Parameters
Name | Type | Description |
---|---|---|
fn | Function | function to invoke |
error | RegExp | Function | The expected |
message | String | error message |
Returns
- Type
- void