0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/doc/api/assert.markdown

85 lines
2.1 KiB
Markdown
Raw Normal View History

2012-02-27 20:07:12 +01:00
# Assert
2010-10-28 14:18:16 +02: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
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
2010-10-28 14:18:16 +02:00
## assert(value, message), assert.ok(value[, message])
2010-10-28 14:18:16 +02:00
Tests if value is truthy, it is equivalent to `assert.equal(true, !!value, message);`
2010-10-28 14:18:16 +02:00
## assert.equal(actual, expected[, message])
2010-10-28 14:18:16 +02:00
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
2010-10-28 14:18:16 +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 ( `!=` ).
## assert.deepEqual(actual, expected[, message])
2010-10-28 14:18:16 +02:00
Tests for deep equality.
## assert.notDeepEqual(actual, expected[, message])
2010-10-28 14:18:16 +02:00
Tests for any deep inequality.
2010-10-28 14:18:16 +02:00
## assert.strictEqual(actual, expected[, message])
2010-10-28 14:18:16 +02:00
Tests strict equality, as determined by the strict equality operator ( `===` )
2010-10-28 14:18:16 +02:00
## assert.notStrictEqual(actual, expected[, message])
2010-10-28 14:18:16 +02:00
Tests strict non-equality, as determined by the strict not equal operator ( `!==` )
2010-10-28 14:18:16 +02:00
## assert.throws(block[, error]\[, message])
2010-10-28 14:18:16 +02: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
## assert.doesNotThrow(block[, message])
2010-10-28 14:18:16 +02: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
Tests if value is not a false value, throws if it is a true value. Useful when
testing the first argument, `error` in callbacks.