2012-02-27 20:07:12 +01:00
|
|
|
# Assert
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 5 - Locked
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
This module is used for writing unit tests for your applications, you can
|
|
|
|
access it with `require('assert')`.
|
|
|
|
|
2012-02-27 20:07:12 +01:00
|
|
|
## assert.fail(actual, expected, message, operator)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-05-24 07:40:09 +02:00
|
|
|
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert(value, message), assert.ok(value[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-11 01:48:08 +01:00
|
|
|
Tests if value is truthy, it is equivalent to `assert.equal(true, !!value, message);`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.equal(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.notEqual(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Tests shallow, coercive non-equality with the not equal comparison operator ( `!=` ).
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.deepEqual(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Tests for deep equality.
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.notDeepEqual(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Tests for any deep inequality.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.strictEqual(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Tests strict equality, as determined by the strict equality operator ( `===` )
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.notStrictEqual(actual, expected[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Tests strict non-equality, as determined by the strict not equal operator ( `!==` )
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.throws(block[, error]\[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-02-22 16:02:10 +01:00
|
|
|
Expects `block` to throw an error. `error` can be constructor, `RegExp` or
|
2010-12-02 20:07:47 +01: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 20:07:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"unexpected error"
|
|
|
|
);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## assert.doesNotThrow(block[, message])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-02-22 16:02:10 +01:00
|
|
|
Expects `block` not to throw an error, see `assert.throws` for details.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:07:12 +01:00
|
|
|
## assert.ifError(value)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01: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.
|