Assert

Node.js assert module — assertion testing functions.

All assertion functions throw an AssertionError (an Error subclass with actual, expected, and operator properties) when the assertion fails.

Beyond this IDL surface, the enriched module also provides: assert.strict (callable, with loose methods remapped to strict equivalents), assert.AssertionError constructor, and assert.default self-reference.

Module: node:assert Support: Experimental Since: 1.1.0 Docs: Node.js Docs See also: https://nodejs.org/api/assert.html

17 members: 17 undocumented

Static Methods

⚪ ok(value, message)
Tests if `value` is truthy. Equivalent to `assert.equal(!!value, true)`.
⚪ equal(actual, expected, message)
Tests shallow equality between `actual` and `expected` using `==`.
⚪ notEqual(actual, expected, message)
Tests shallow inequality between `actual` and `expected` using `!=`.
⚪ strictEqual(actual, expected, message)
Tests strict equality between `actual` and `expected` using `===`.
⚪ notStrictEqual(actual, expected, message)
Tests strict inequality between `actual` and `expected` using `!==`.
⚪ deepEqual(actual, expected, message)
Tests for deep equality between `actual` and `expected` using abstract equality (`==`) for leaf comparisons.
⚪ deepStrictEqual(actual, expected, message)
Tests for deep strict equality between `actual` and `expected` using strict equality (`===`) for leaf comparisons.
⚪ notDeepEqual(actual, expected, message)
Tests that `actual` is NOT deeply equal to `expected` (abstract equality).
⚪ notDeepStrictEqual(actual, expected, message)
Tests that `actual` is NOT deeply strictly equal to `expected`.
⚪ throws(fn, error, message)
Expects the function `fn` to throw an error. Optionally validates the thrown error.
⚪ doesNotThrow(fn, error, message)
Asserts that the function `fn` does not throw an error.
⚪ rejects(asyncFn, error, message)
Awaits the `asyncFn` promise (or calls it if it's a function) and expects it to reject.
⚪ doesNotReject(asyncFn, error, message)
Awaits the `asyncFn` promise (or calls it if it's a function) and expects it NOT to reject.
⚪ fail(message)
Throws an AssertionError unconditionally. If `message` is an Error, it is thrown directly.
⚪ ifError(value)
If `value` is truthy (not null/undefined/false), throws it. Used in callbacks: `assert.ifError(err)`.
⚪ match(string, regexp, message)
Expects `string` to match the regular expression `regexp`.
⚪ doesNotMatch(string, regexp, message)
Expects `string` NOT to match the regular expression `regexp`.