{ "source": "doc/api/assert.markdown", "modules": [ { "textRaw": "Assert", "name": "assert", "stability": 3, "stabilityText": "Locked", "desc": "
This module is used so that Node.js can test itself. It can be accessed with\nrequire('assert')
. However, it is recommended that a userland assertion\nlibrary be used instead.\n\n
Throws an exception that displays the values for actual
and expected
\nseparated by the provided operator.\n\n
Tests if value is truthy. It is equivalent to\nassert.equal(true, !!value, message)
.\n\n
Tests shallow, coercive equality with the equal comparison operator ( ==
).\n\n
Tests shallow, coercive inequality with the not equal comparison operator\n( !=
).\n\n
Tests for deep equality. Primitive values are compared with the equal\ncomparison operator ( ==
).\n\n
This only considers enumerable properties. It does not test object prototypes,\nattached symbols, or non-enumerable properties. This can lead to some\npotentially surprising results. For example, this does not throw an\nAssertionError
because the properties on the Error
object are\nnon-enumerable:\n\n
// WARNING: This does not throw an AssertionError!\nassert.deepEqual(Error('a'), Error('b'));
\n",
"signatures": [
{
"params": [
{
"name": "actual"
},
{
"name": "expected"
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.notDeepEqual(actual, expected[, message])",
"type": "method",
"name": "notDeepEqual",
"desc": "Tests for any deep inequality. Opposite of assert.deepEqual
.\n\n
Tests strict equality as determined by the strict equality operator ( ===
).\n\n
Tests strict inequality as determined by the strict not equal operator\n( !==
).\n\n
Tests for deep equality. Primitive values are compared with the strict equality\noperator ( ===
).\n\n
Tests for deep inequality. Opposite of assert.deepStrictEqual
.\n\n
Expects block
to throw an error. error
can be a constructor, RegExp
, or\nvalidation function.\n\n
Validate instanceof using constructor:\n\n
\nassert.throws(\n function() {\n throw new Error("Wrong value");\n },\n Error\n);
\nValidate error message using RegExp:\n\n
\nassert.throws(\n function() {\n throw new Error("Wrong value");\n },\n /value/\n);
\nCustom error validation:\n\n
\nassert.throws(\n function() {\n throw new Error("Wrong value");\n },\n function(err) {\n if ( (err instanceof Error) && /value/.test(err) ) {\n return true;\n }\n },\n "unexpected error"\n);
\n",
"signatures": [
{
"params": [
{
"name": "block"
},
{
"name": "error",
"optional": true
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.doesNotThrow(block[, error][, message])",
"type": "method",
"name": "doesNotThrow",
"desc": "Expects block
not to throw an error. See assert.throws() for more details.\n\n
If block
throws an error and if it is of a different type from error
, the\nthrown error will get propagated back to the caller. The following call will\nthrow the TypeError
, since we're not matching the error types in the\nassertion.\n\n
assert.doesNotThrow(\n function() {\n throw new TypeError("Wrong value");\n },\n SyntaxError\n);
\nIn case error
matches with the error thrown by block
, an AssertionError
\nis thrown instead.\n\n
assert.doesNotThrow(\n function() {\n throw new TypeError("Wrong value");\n },\n TypeError\n);
\n",
"signatures": [
{
"params": [
{
"name": "block"
},
{
"name": "error",
"optional": true
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.ifError(value)",
"type": "method",
"name": "ifError",
"desc": "Throws value
if value
is truthy. This is useful when testing the error
\nargument in callbacks.\n