2012-02-27 11:07:12 -08:00
|
|
|
# Assert
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-10-12 13:58:00 -07:00
|
|
|
Stability: 3 - Locked
|
2012-03-02 15:14:03 -08:00
|
|
|
|
2015-10-12 13:58:00 -07:00
|
|
|
This module is used so that Node.js can test itself. It can be accessed with
|
|
|
|
`require('assert')`. However, it is recommended that a userland assertion
|
|
|
|
library be used instead.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2012-02-27 11:07:12 -08:00
|
|
|
## assert.fail(actual, expected, message, operator)
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-11 04:54:59 +01:00
|
|
|
Throws an exception that displays the values for `actual` and `expected`
|
|
|
|
separated by the provided operator.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-01-09 19:55:32 +02:00
|
|
|
## assert(value[, message]), assert.ok(value[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-11 04:54:59 +01:00
|
|
|
Tests if value is truthy. It is equivalent to
|
2015-10-26 13:51:30 -04:00
|
|
|
`assert.equal(!!value, true, message)`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.equal(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2010-11-21 17:22:34 -05:00
|
|
|
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.notEqual(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-17 23:17:15 -07:00
|
|
|
Tests shallow, coercive inequality with the not equal comparison operator
|
|
|
|
( `!=` ).
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.deepEqual(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-10-12 13:58:00 -07:00
|
|
|
Tests for deep equality. Primitive values are compared with the equal
|
|
|
|
comparison operator ( `==` ).
|
|
|
|
|
|
|
|
This only considers enumerable properties. It does not test object prototypes,
|
|
|
|
attached symbols, or non-enumerable properties. This can lead to some
|
|
|
|
potentially surprising results. For example, this does not throw an
|
|
|
|
`AssertionError` because the properties on the `Error` object are
|
|
|
|
non-enumerable:
|
|
|
|
|
|
|
|
// WARNING: This does not throw an AssertionError!
|
|
|
|
assert.deepEqual(Error('a'), Error('b'));
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.notDeepEqual(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-01-28 19:48:56 +03:00
|
|
|
Tests for any deep inequality. Opposite of `assert.deepEqual`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.strictEqual(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-17 23:17:15 -07:00
|
|
|
Tests strict equality as determined by the strict equality operator ( `===` ).
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-24 15:41:31 -07:00
|
|
|
## assert.notStrictEqual(actual, expected[, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-17 23:17:15 -07:00
|
|
|
Tests strict inequality as determined by the strict not equal operator
|
|
|
|
( `!==` ).
|
2015-01-28 19:48:56 +03:00
|
|
|
|
|
|
|
## assert.deepStrictEqual(actual, expected[, message])
|
|
|
|
|
|
|
|
Tests for deep equality. Primitive values are compared with the strict equality
|
|
|
|
operator ( `===` ).
|
|
|
|
|
|
|
|
## assert.notDeepStrictEqual(actual, expected[, message])
|
|
|
|
|
|
|
|
Tests for deep inequality. Opposite of `assert.deepStrictEqual`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2014-09-29 16:32:34 -07:00
|
|
|
## assert.throws(block[, error][, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-17 23:17:15 -07:00
|
|
|
Expects `block` to throw an error. `error` can be a constructor, `RegExp`, or
|
2010-12-02 11:07:47 -08:00
|
|
|
validation function.
|
|
|
|
|
|
|
|
Validate instanceof using constructor:
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
throw new Error("Wrong value");
|
|
|
|
},
|
|
|
|
Error
|
|
|
|
);
|
|
|
|
|
|
|
|
Validate error message using RegExp:
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
throw new Error("Wrong value");
|
|
|
|
},
|
|
|
|
/value/
|
|
|
|
);
|
|
|
|
|
|
|
|
Custom error validation:
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
throw new Error("Wrong value");
|
|
|
|
},
|
|
|
|
function(err) {
|
2010-12-21 18:42:52 +01:00
|
|
|
if ( (err instanceof Error) && /value/.test(err) ) {
|
|
|
|
return true;
|
2010-12-02 11:07:47 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"unexpected error"
|
|
|
|
);
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-11 04:54:59 +01:00
|
|
|
## assert.doesNotThrow(block[, error][, message])
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-11 04:54:59 +01:00
|
|
|
Expects `block` not to throw an error. See [assert.throws()](#assert_assert_throws_block_error_message) for more details.
|
|
|
|
|
|
|
|
If `block` throws an error and if it is of a different type from `error`, the
|
|
|
|
thrown error will get propagated back to the caller. The following call will
|
|
|
|
throw the `TypeError`, since we're not matching the error types in the
|
|
|
|
assertion.
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
|
|
|
function() {
|
|
|
|
throw new TypeError("Wrong value");
|
|
|
|
},
|
|
|
|
SyntaxError
|
|
|
|
);
|
|
|
|
|
|
|
|
In case `error` matches with the error thrown by `block`, an `AssertionError`
|
|
|
|
is thrown instead.
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
|
|
|
function() {
|
|
|
|
throw new TypeError("Wrong value");
|
|
|
|
},
|
|
|
|
TypeError
|
|
|
|
);
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2012-02-27 11:07:12 -08:00
|
|
|
## assert.ifError(value)
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-09-17 23:17:15 -07:00
|
|
|
Throws `value` if `value` is truthy. This is useful when testing the `error`
|
|
|
|
argument in callbacks.
|