0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/doc/api/assert.markdown

83 lines
2.1 KiB
Markdown
Raw Normal View History

2010-10-28 14:18:16 +02:00
## Assert
This module is used for writing unit tests for your applications, you can
access it with `require('assert')`.
### assert.fail(actual, expected, message, operator)
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
2010-10-28 14:18:16 +02:00
2011-10-01 13:46:49 +02:00
### assert(value, message), assert.ok(value, [message])
2010-10-28 14:18:16 +02:00
Tests if value is a `true` value, it is equivalent to `assert.equal(true, value, message);`
### assert.equal(actual, expected, [message])
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
2010-10-28 14:18:16 +02:00
### assert.notEqual(actual, expected, [message])
Tests shallow, coercive non-equality with the not equal comparison operator ( `!=` ).
### assert.deepEqual(actual, expected, [message])
Tests for deep equality.
### assert.notDeepEqual(actual, expected, [message])
Tests for any deep inequality.
2010-10-28 14:18:16 +02:00
### assert.strictEqual(actual, expected, [message])
Tests strict equality, as determined by the strict equality operator ( `===` )
2010-10-28 14:18:16 +02:00
### assert.notStrictEqual(actual, expected, [message])
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-12-02 20:07:47 +01:00
Expects `block` to throw an error. `error` can be constructor, regexp or
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, [error], [message])
2010-12-02 20:07:47 +01:00
Expects `block` not to throw an error, see assert.throws for details.
2010-10-28 14:18:16 +02:00
### assert.ifError(value)
Tests if value is not a false value, throws if it is a true value. Useful when
testing the first argument, `error` in callbacks.