{ "source": "doc/api/assert.md", "modules": [ { "textRaw": "Assert", "name": "assert", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "

The assert module provides a simple set of assertion tests that can be used to\ntest invariants.

\n", "methods": [ { "textRaw": "assert(value[, message])", "type": "method", "name": "assert", "meta": { "added": [ "v0.5.9" ] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "value" }, { "name": "message", "optional": true } ] } ], "desc": "

An alias of assert.ok().

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

Tests for deep equality between the actual and expected parameters.\nPrimitive values are compared with the equal comparison operator ( == ).

\n

Only enumerable "own" properties are considered. The deepEqual()\nimplementation does not test object prototypes, attached symbols, or\nnon-enumerable properties. This can lead to some potentially surprising\nresults. For example, the following example does not throw an AssertionError\nbecause the properties on the Error object are non-enumerable:

\n
// WARNING: This does not throw an AssertionError!\nassert.deepEqual(Error('a'), Error('b'));\n
\n

"Deep" equality means that the enumerable "own" properties of child objects\nare evaluated also:

\n
const assert = require('assert');\n\nconst obj1 = {\n  a: {\n    b: 1\n  }\n};\nconst obj2 = {\n  a: {\n    b: 2\n  }\n};\nconst obj3 = {\n  a: {\n    b: 1\n  }\n};\nconst obj4 = Object.create(obj1);\n\nassert.deepEqual(obj1, obj1);\n// OK, object is equal to itself\n\nassert.deepEqual(obj1, obj2);\n// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }\n// values of b are different\n\nassert.deepEqual(obj1, obj3);\n// OK, objects are equal\n\nassert.deepEqual(obj1, obj4);\n// AssertionError: { a: { b: 1 } } deepEqual {}\n// Prototypes are ignored\n
\n

If the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

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

Generally identical to assert.deepEqual() with two exceptions. First,\nprimitive values are compared using the strict equality operator ( === ).\nSecond, object comparisons include a strict equality check of their prototypes.

\n
const assert = require('assert');\n\nassert.deepEqual({ a: 1 }, { a: '1' });\n// OK, because 1 == '1'\n\nassert.deepStrictEqual({ a: 1 }, { a: '1' });\n// AssertionError: { a: 1 } deepStrictEqual { a: '1' }\n// because 1 !== '1' using strict equality\n
\n

If the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

\n" }, { "textRaw": "assert.doesNotThrow(block[, error][, message])", "type": "method", "name": "doesNotThrow", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`block` {Function} ", "name": "block", "type": "Function" }, { "textRaw": "`error` {RegExp|Function} ", "name": "error", "type": "RegExp|Function", "optional": true }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "block" }, { "name": "error", "optional": true }, { "name": "message", "optional": true } ] } ], "desc": "

Asserts that the function block does not throw an error. See\nassert.throws() for more details.

\n

When assert.doesNotThrow() is called, it will immediately call the block\nfunction.

\n

If an error is thrown and it is the same type as that specified by the error\nparameter, then an AssertionError is thrown. If the error is of a different\ntype, or if the error parameter is undefined, the error is propagated back\nto the caller.

\n

The following, for instance, will throw the TypeError because there is no\nmatching error type in the assertion:

\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  SyntaxError\n);\n
\n

However, the following will result in an AssertionError with the message\n'Got unwanted exception (TypeError)..':

\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  TypeError\n);\n
\n

If an AssertionError is thrown and a value is provided for the message\nparameter, the value of message will be appended to the AssertionError\nmessage:

\n
assert.doesNotThrow(\n  () => {\n    throw new TypeError('Wrong value');\n  },\n  TypeError,\n  'Whoops'\n);\n// Throws: AssertionError: Got unwanted exception (TypeError). Whoops\n
\n" }, { "textRaw": "assert.equal(actual, expected[, message])", "type": "method", "name": "equal", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests shallow, coercive equality between the actual and expected parameters\nusing the equal comparison operator ( == ).

\n
const assert = require('assert');\n\nassert.equal(1, 1);\n// OK, 1 == 1\nassert.equal(1, '1');\n// OK, 1 == '1'\n\nassert.equal(1, 2);\n// AssertionError: 1 == 2\nassert.equal({a: {b: 1}}, {a: {b: 1}});\n//AssertionError: { a: { b: 1 } } == { a: { b: 1 } }\n
\n

If the values are not equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

\n" }, { "textRaw": "assert.fail(message)", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true }, { "textRaw": "`operator` {string} **Default:** '!=' ", "name": "operator", "type": "string", "desc": "**Default:** '!='", "optional": true }, { "textRaw": "`stackStartFunction` {function} **Default:** `assert.fail` ", "name": "stackStartFunction", "type": "function", "desc": "**Default:** `assert.fail`", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true }, { "name": "operator", "optional": true }, { "name": "stackStartFunction", "optional": true } ] }, { "params": [ { "name": "message" } ] } ], "desc": "

Throws an AssertionError. If message is falsy, the error message is set as\nthe values of actual and expected separated by the provided operator.\nOtherwise, the error message is the value of message.\nIf stackStartFunction is provided, all stack frames above that function will\nbe removed from stacktrace (see Error.captureStackTrace).

\n
const assert = require('assert');\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError: whoops\n\nassert.fail('boom');\n// AssertionError: boom\n\nassert.fail('a', 'b');\n// AssertionError: 'a' != 'b'\n
\n

Example use of stackStartFunction for truncating the exception's stacktrace:

\n
function suppressFrame() {\n  assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError: 'a' !== 'b'\n//     at repl:1:1\n//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n//     ...\n
\n" }, { "textRaw": "assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])", "type": "method", "name": "fail", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true }, { "textRaw": "`operator` {string} **Default:** '!=' ", "name": "operator", "type": "string", "desc": "**Default:** '!='", "optional": true }, { "textRaw": "`stackStartFunction` {function} **Default:** `assert.fail` ", "name": "stackStartFunction", "type": "function", "desc": "**Default:** `assert.fail`", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true }, { "name": "operator", "optional": true }, { "name": "stackStartFunction", "optional": true } ] } ], "desc": "

Throws an AssertionError. If message is falsy, the error message is set as\nthe values of actual and expected separated by the provided operator.\nOtherwise, the error message is the value of message.\nIf stackStartFunction is provided, all stack frames above that function will\nbe removed from stacktrace (see Error.captureStackTrace).

\n
const assert = require('assert');\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError: 1 > 2\n\nassert.fail(1, 2, 'fail');\n// AssertionError: fail\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError: whoops\n\nassert.fail('boom');\n// AssertionError: boom\n\nassert.fail('a', 'b');\n// AssertionError: 'a' != 'b'\n
\n

Example use of stackStartFunction for truncating the exception's stacktrace:

\n
function suppressFrame() {\n  assert.fail('a', 'b', undefined, '!==', suppressFrame);\n}\nsuppressFrame();\n// AssertionError: 'a' !== 'b'\n//     at repl:1:1\n//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)\n//     ...\n
\n" }, { "textRaw": "assert.ifError(value)", "type": "method", "name": "ifError", "meta": { "added": [ "v0.1.97" ] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" } ] }, { "params": [ { "name": "value" } ] } ], "desc": "

Throws value if value is truthy. This is useful when testing the error\nargument in callbacks.

\n
const assert = require('assert');\n\nassert.ifError(null);\n// OK\nassert.ifError(0);\n// OK\nassert.ifError(1);\n// Throws 1\nassert.ifError('error');\n// Throws 'error'\nassert.ifError(new Error());\n// Throws Error\n
\n" }, { "textRaw": "assert.notDeepEqual(actual, expected[, message])", "type": "method", "name": "notDeepEqual", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

Tests for any deep inequality. Opposite of assert.deepEqual().

\n
const assert = require('assert');\n\nconst obj1 = {\n  a: {\n    b: 1\n  }\n};\nconst obj2 = {\n  a: {\n    b: 2\n  }\n};\nconst obj3 = {\n  a: {\n    b: 1\n  }\n};\nconst obj4 = Object.create(obj1);\n\nassert.notDeepEqual(obj1, obj1);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj2);\n// OK: obj1 and obj2 are not deeply equal\n\nassert.notDeepEqual(obj1, obj3);\n// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }\n\nassert.notDeepEqual(obj1, obj4);\n// OK: obj1 and obj4 are not deeply equal\n
\n

If the values are deeply equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

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

Tests for deep strict inequality. Opposite of assert.deepStrictEqual().

\n
const assert = require('assert');\n\nassert.notDeepEqual({a: 1}, {a: '1'});\n// AssertionError: { a: 1 } notDeepEqual { a: '1' }\n\nassert.notDeepStrictEqual({a: 1}, {a: '1'});\n// OK\n
\n

If the values are deeply and strictly equal, an AssertionError is thrown\nwith a message property set equal to the value of the message parameter. If\nthe message parameter is undefined, a default error message is assigned.

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

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

\n
const assert = require('assert');\n\nassert.notEqual(1, 2);\n// OK\n\nassert.notEqual(1, 1);\n// AssertionError: 1 != 1\n\nassert.notEqual(1, '1');\n// AssertionError: 1 != '1'\n
\n

If the values are equal, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

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

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

\n
const assert = require('assert');\n\nassert.notStrictEqual(1, 2);\n// OK\n\nassert.notStrictEqual(1, 1);\n// AssertionError: 1 !== 1\n\nassert.notStrictEqual(1, '1');\n// OK\n
\n

If the values are strictly equal, an AssertionError is thrown with a\nmessage property set equal to the value of the message parameter. If the\nmessage parameter is undefined, a default error message is assigned.

\n" }, { "textRaw": "assert.ok(value[, message])", "type": "method", "name": "ok", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} ", "name": "value", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "value" }, { "name": "message", "optional": true } ] } ], "desc": "

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

\n

If value is not truthy, an AssertionError is thrown with a message\nproperty set equal to the value of the message parameter. If the message\nparameter is undefined, a default error message is assigned.

\n
const assert = require('assert');\n\nassert.ok(true);\n// OK\nassert.ok(1);\n// OK\nassert.ok(false);\n// throws "AssertionError: false == true"\nassert.ok(0);\n// throws "AssertionError: 0 == true"\nassert.ok(false, 'it\\'s false');\n// throws "AssertionError: it's false"\n
\n" }, { "textRaw": "assert.strictEqual(actual, expected[, message])", "type": "method", "name": "strictEqual", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`actual` {any} ", "name": "actual", "type": "any" }, { "textRaw": "`expected` {any} ", "name": "expected", "type": "any" }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "actual" }, { "name": "expected" }, { "name": "message", "optional": true } ] } ], "desc": "

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

\n
const assert = require('assert');\n\nassert.strictEqual(1, 2);\n// AssertionError: 1 === 2\n\nassert.strictEqual(1, 1);\n// OK\n\nassert.strictEqual(1, '1');\n// AssertionError: 1 === '1'\n
\n

If the values are not strictly equal, an AssertionError is thrown with a\nmessage property set equal to the value of the message parameter. If the\nmessage parameter is undefined, a default error message is assigned.

\n" }, { "textRaw": "assert.throws(block[, error][, message])", "type": "method", "name": "throws", "meta": { "added": [ "v0.1.21" ] }, "signatures": [ { "params": [ { "textRaw": "`block` {Function} ", "name": "block", "type": "Function" }, { "textRaw": "`error` {RegExp|Function} ", "name": "error", "type": "RegExp|Function", "optional": true }, { "textRaw": "`message` {any} ", "name": "message", "type": "any", "optional": true } ] }, { "params": [ { "name": "block" }, { "name": "error", "optional": true }, { "name": "message", "optional": true } ] } ], "desc": "

Expects the function block to throw an error.

\n

If specified, error can be a constructor, RegExp, or validation\nfunction.

\n

If specified, message will be the message provided by the AssertionError if\nthe block fails to throw.

\n

Validate instanceof using constructor:

\n
assert.throws(\n  () => {\n    throw new Error('Wrong value');\n  },\n  Error\n);\n
\n

Validate error message using RegExp:

\n
assert.throws(\n  () => {\n    throw new Error('Wrong value');\n  },\n  /value/\n);\n
\n

Custom error validation:

\n
assert.throws(\n  () => {\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
\n

Note that error can not be a string. If a string is provided as the second\nargument, then error is assumed to be omitted and the string will be used for\nmessage instead. This can lead to easy-to-miss mistakes:

\n\n
// THIS IS A MISTAKE! DO NOT DO THIS!\nassert.throws(myFunction, 'missing foo', 'did not throw with expected message');\n\n// Do this instead.\nassert.throws(myFunction, /missing foo/, 'did not throw with expected message');\n
\n" } ], "type": "module", "displayName": "Assert" } ] }