{ "source": "doc/api/assert.md", "modules": [ { "textRaw": "Assert", "name": "assert", "stability": 3, "stabilityText": "Locked", "desc": "
The assert
module provides a simple set of assertion tests that can be used to\ntest invariants. The module is intended for internal use by Node.js, but can be\nused in application code via require('assert')
. However, assert
is not a\ntesting framework, and is not intended to be used as a general purpose assertion\nlibrary.
The API for the assert
module is Locked. This means that there will be no\nadditions or changes to any of the methods implemented and exposed by\nthe module.
An alias of assert.ok()
.
const assert = require('assert');\n\nassert(true);\n// OK\nassert(1);\n// OK\nassert(false);\n// throws "AssertionError: false == true"\nassert(0);\n// throws "AssertionError: 0 == true"\nassert(false, 'it\\'s false');\n// throws "AssertionError: it's false"\n
\n",
"signatures": [
{
"params": [
{
"name": "value"
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.deepEqual(actual, expected[, message])",
"type": "method",
"name": "deepEqual",
"meta": {
"added": [
"v0.1.21"
]
},
"desc": "Tests for deep equality between the actual
and expected
parameters.\nPrimitive values are compared with the equal comparison operator ( ==
).
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:
// 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:
\nconst 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
\nIf 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.
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.
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
\nIf 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.
Asserts that the function block
does not throw an error. See\nassert.throws()
for more details.
When assert.doesNotThrow()
is called, it will immediately call the block
\nfunction.
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.
The following, for instance, will throw the TypeError
because there is no\nmatching error type in the assertion:
assert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n SyntaxError\n);\n
\nHowever, the following will result in an AssertionError
with the message\n'Got unwanted exception (TypeError)..':
assert.doesNotThrow(\n () => {\n throw new TypeError('Wrong value');\n },\n TypeError\n);\n
\nIf an AssertionError
is thrown and a value is provided for the message
\nparameter, the value of message
will be appended to the AssertionError
\nmessage:
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",
"signatures": [
{
"params": [
{
"name": "block"
},
{
"name": "error",
"optional": true
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.equal(actual, expected[, message])",
"type": "method",
"name": "equal",
"meta": {
"added": [
"v0.1.21"
]
},
"desc": "Tests shallow, coercive equality between the actual
and expected
parameters\nusing the equal comparison operator ( ==
).
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
\nIf 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.
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
.
const assert = require('assert');\n\nassert.fail(1, 2, undefined, '>');\n// AssertionError: 1 > 2\n\nassert.fail(1, 2, 'whoops', '>');\n// AssertionError: whoops\n
\n",
"signatures": [
{
"params": [
{
"name": "actual"
},
{
"name": "expected"
},
{
"name": "message"
},
{
"name": "operator"
}
]
}
]
},
{
"textRaw": "assert.ifError(value)",
"type": "method",
"name": "ifError",
"meta": {
"added": [
"v0.1.97"
]
},
"desc": "Throws value
if value
is truthy. This is useful when testing the error
\nargument in callbacks.
const assert = require('assert');\n\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",
"signatures": [
{
"params": [
{
"name": "value"
}
]
}
]
},
{
"textRaw": "assert.notDeepEqual(actual, expected[, message])",
"type": "method",
"name": "notDeepEqual",
"meta": {
"added": [
"v0.1.21"
]
},
"desc": "Tests for any deep inequality. Opposite of assert.deepEqual()
.
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 obj2 are not deeply equal\n
\nIf 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.
Tests for deep strict inequality. Opposite of assert.deepStrictEqual()
.
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
\nIf 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.
Tests shallow, coercive inequality with the not equal comparison operator\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
\nIf 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.
Tests strict inequality as determined by the strict not equal operator\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
\nIf 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.
Tests if value
is truthy. It is equivalent to\nassert.equal(!!value, true, message)
.
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.
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",
"signatures": [
{
"params": [
{
"name": "value"
},
{
"name": "message",
"optional": true
}
]
}
]
},
{
"textRaw": "assert.strictEqual(actual, expected[, message])",
"type": "method",
"name": "strictEqual",
"meta": {
"added": [
"v0.1.21"
]
},
"desc": "Tests strict equality as determined by the strict equality operator ( ===
).
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
\nIf 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.
Expects the function block
to throw an error.
If specified, error
can be a constructor, RegExp
, or validation\nfunction.
If specified, message
will be the message provided by the AssertionError
if\nthe block fails to throw.
Validate instanceof using constructor:
\nassert.throws(\n () => {\n throw new Error('Wrong value');\n },\n Error\n);\n
\nValidate error message using RegExp
:
assert.throws(\n () => {\n throw new Error('Wrong value');\n },\n /value/\n);\n
\nCustom error validation:
\nassert.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
\nNote 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:
// 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",
"signatures": [
{
"params": [
{
"name": "block"
},
{
"name": "error",
"optional": true
},
{
"name": "message",
"optional": true
}
]
}
]
}
],
"type": "module",
"displayName": "Assert"
}
]
}