{ "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

\n", "methods": [ { "textRaw": "assert.fail(actual, expected, message, operator)", "type": "method", "name": "fail", "desc": "

Throws an exception that displays the values for actual and expected\nseparated by the provided operator.\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message" }, { "name": "operator" } ] } ] }, { "textRaw": "assert(value[, message]), assert.ok(value[, message])", "type": "method", "name": "ok", "desc": "

Tests if value is truthy. It is equivalent to\nassert.equal(true, !!value, message).\n\n

\n", "signatures": [ { "params": [ { "name": "value" }, { "name": "message])" }, { "name": "assert.ok(value" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.equal(actual, expected[, message])", "type": "method", "name": "equal", "desc": "

Tests shallow, coercive equality with the equal comparison operator ( == ).\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.notEqual(actual, expected[, message])", "type": "method", "name": "notEqual", "desc": "

Tests shallow, coercive inequality with the not equal comparison operator\n( != ).\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.deepEqual(actual, expected[, message])", "type": "method", "name": "deepEqual", "desc": "

Tests for deep equality. Primitive values are compared with the equal\ncomparison operator ( == ).\n\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

\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

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.strictEqual(actual, expected[, message])", "type": "method", "name": "strictEqual", "desc": "

Tests strict equality as determined by the strict equality operator ( === ).\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.notStrictEqual(actual, expected[, message])", "type": "method", "name": "notStrictEqual", "desc": "

Tests strict inequality as determined by the strict not equal operator\n( !== ).\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.deepStrictEqual(actual, expected[, message])", "type": "method", "name": "deepStrictEqual", "desc": "

Tests for deep equality. Primitive values are compared with the strict equality\noperator ( === ).\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.notDeepStrictEqual(actual, expected[, message])", "type": "method", "name": "notDeepStrictEqual", "desc": "

Tests for deep inequality. Opposite of assert.deepStrictEqual.\n\n

\n", "signatures": [ { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ] }, { "textRaw": "assert.throws(block[, error][, message])", "type": "method", "name": "throws", "desc": "

Expects block to throw an error. error can be a constructor, RegExp, or\nvalidation function.\n\n

\n

Validate instanceof using constructor:\n\n

\n
assert.throws(\n  function() {\n    throw new Error("Wrong value");\n  },\n  Error\n);
\n

Validate error message using RegExp:\n\n

\n
assert.throws(\n  function() {\n    throw new Error("Wrong value");\n  },\n  /value/\n);
\n

Custom error validation:\n\n

\n
assert.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

\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

\n
assert.doesNotThrow(\n  function() {\n    throw new TypeError("Wrong value");\n  },\n  SyntaxError\n);
\n

In case error matches with the error thrown by block, an AssertionError\nis thrown instead.\n\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

\n", "signatures": [ { "params": [ { "name": "value" } ] } ] } ], "type": "module", "displayName": "Assert" } ] }