2012-02-28 03:07:12 +08:00
|
|
|
# Assert
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2015-02-25 08:15:26 +08:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 07:14:03 +08:00
|
|
|
|
2010-10-28 20:18:16 +08:00
|
|
|
This module is used for writing unit tests for your applications, you can
|
|
|
|
access it with `require('assert')`.
|
|
|
|
|
2012-02-28 03:07:12 +08:00
|
|
|
## assert.fail(actual, expected, message, operator)
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2011-05-24 13:40:09 +08:00
|
|
|
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2015-01-10 01:55:32 +08:00
|
|
|
## assert(value[, message]), assert.ok(value[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2012-03-11 08:48:08 +08:00
|
|
|
Tests if value is truthy, it is equivalent to `assert.equal(true, !!value, message);`
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.equal(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-22 06:22:34 +08:00
|
|
|
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.notEqual(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
|
|
|
Tests shallow, coercive non-equality with the not equal comparison operator ( `!=` ).
|
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.deepEqual(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2015-01-29 00:48:56 +08:00
|
|
|
Tests for deep equality. Primitive values are compared with the equal comparison
|
|
|
|
operator ( `==` ). Doesn't take object prototypes into account.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.notDeepEqual(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2015-01-29 00:48:56 +08:00
|
|
|
Tests for any deep inequality. Opposite of `assert.deepEqual`.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.strictEqual(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-22 06:22:34 +08:00
|
|
|
Tests strict equality, as determined by the strict equality operator ( `===` )
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.notStrictEqual(actual, expected[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2015-01-29 00:48:56 +08:00
|
|
|
Tests strict non-equality, as determined by the strict not equal
|
|
|
|
operator ( `!==` )
|
|
|
|
|
|
|
|
## 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 20:18:16 +08:00
|
|
|
|
2014-09-30 07:32:34 +08:00
|
|
|
## assert.throws(block[, error][, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-02-22 23:02:10 +08:00
|
|
|
Expects `block` to throw an error. `error` can be constructor, `RegExp` or
|
2010-12-03 03: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-22 01:42:52 +08:00
|
|
|
if ( (err instanceof Error) && /value/.test(err) ) {
|
|
|
|
return true;
|
2010-12-03 03:07:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"unexpected error"
|
|
|
|
);
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-25 06:41:31 +08:00
|
|
|
## assert.doesNotThrow(block[, message])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-02-22 23:02:10 +08:00
|
|
|
Expects `block` not to throw an error, see `assert.throws` for details.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2012-02-28 03:07:12 +08:00
|
|
|
## assert.ifError(value)
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
Tests if value is not a false value, throws if it is a true value. Useful when
|
|
|
|
testing the first argument, `error` in callbacks.
|