node:assert
Node.js assert module, resolvable via both ESM import and CommonJS require.
The module export is itself callable — assert(value[, message]) is an alias
of assert.ok, and assert.ok === assert. The assertion methods below are
attached to that function, so const assert = require('assert'); assert(x) and
assert.strictEqual(a, b) both work.
Every assertion throws (or rejects with) an AssertionError when the condition
fails: a real Error subclass exported as assert.AssertionError, so err
instanceof assert.AssertionError and err.constructor.name === 'AssertionError'
hold. It carries name === 'AssertionError', code === 'ERR_ASSERTION', plus
actual, expected, and operator. new assert.AssertionError({ message,
actual, expected, operator }) is also supported.
assert.strict exposes the same callable surface in strict mode: the loose
comparisons are re-pointed at their strict variants (equal → strictEqual,
deepEqual → deepStrictEqual, and their negations), and assert.strict.strict
=== assert.strict.
Implemented operations (17/17)
| Operation | Summary |
|---|---|
ok(value, message?) | Throws unless value is truthy (like !!value). |
equal(actual, expected, message?) | == comparison. |
notEqual(actual, expected, message?) | != comparison. |
strictEqual(actual, expected, message?) | === comparison. |
notStrictEqual(actual, expected, message?) | !== comparison. |
deepEqual(actual, expected, message?) | Recursive abstract (==) comparison. |
deepStrictEqual(actual, expected, message?) | Recursive strict comparison (incl. typed-array types, NaN === NaN). |
notDeepEqual(actual, expected, message?) | Negation of deepEqual. |
notDeepStrictEqual(actual, expected, message?) | Negation of deepStrictEqual. |
throws(fn, expected?, message?) | Calls fn; fails unless it throws (matching expected if provided). |
doesNotThrow(fn, expected?, message?) | Calls fn; fails if it throws. |
rejects(asyncFn, expected?, message?) | Awaits asyncFn (or the provided Promise); resolves iff it rejected (matching expected). Returns a Promise. |
doesNotReject(asyncFn, expected?, message?) | Awaits asyncFn; resolves iff it did not reject. Returns a Promise. |
fail(message?) | Throws unconditionally. |
ifError(value) | Throws value unless it is null or undefined (so false, 0, and "" also throw, matching Node). |
match(string, regexp, message?) | Fails unless regexp.test(string) is true. |
doesNotMatch(string, regexp, message?) | Fails if regexp.test(string) is true. |
Deep equality
deepEqual and deepStrictEqual walk the value graph recursively with
full circular-reference handling. Covered types include:
Date(compared viagetTime())RegExp(compared onsource+flags)Error(compared onname+message)ArrayBuffer/DataView(bytewise)- Typed arrays (
Uint8Array,Int32Array, …) — in strict mode, concrete class must match (Uint8Array !== Int8Array) Map/Set— order-independent deep comparison of entries / values- Plain arrays and objects — own enumerable keys
Cycles terminate safely: a.self = a; b.self = b; assert.deepEqual(a, b) holds
when the non-cycle shape matches.
Expected-error matchers
throws and rejects accept an optional expected argument that narrows which
thrown value counts as a match:
null/undefined/ omitted — any thrown value matches.- Constructor function (e.g.
TypeError) — the thrown value must beinstanceofit, otherwise the constructor is called as a predicate withexpected(thrown)and must return a truthy value. RegExp— tested againstString(thrown).- Plain object — each own key must deep-strict-equal the thrown value's corresponding key (subset match).
Async return shape
rejects and doesNotReject return a Promise. The outer Promise resolves
with undefined when the assertion succeeds, or rejects with an
AssertionError-shaped Error when it fails. Invoking a non-thenable input
rejects immediately with a TypeError.
Example
const assert = require("node:assert");
// Strict equality
assert.strictEqual(1 + 1, 2); // ok
assert.throws(() => assert.strictEqual(1, "1")); // passes: mismatch throws
// Deep structural equality
assert.deepStrictEqual({ a: [1, 2, 3] }, { a: [1, 2, 3] });
// Async rejection
await assert.rejects(
async () => { throw new TypeError("nope"); },
TypeError,
);
// Error shape
try {
assert.strictEqual(1, 2);
} catch (e) {
e.name === "AssertionError"; // true
e.code === "ERR_ASSERTION"; // true
e.actual === 1; // true
e.expected === 2; // true
e.operator === "==="; // true
}See also
- Node.js upstream:
- Related:
node:util.types— type-checking predicates used inside assertion validators