Skip to content

node:assert

How Elide supports Node's assert and assert/strict modules: which assertions work, and where loose deep equality, the Assert class and error messages differ.

On this page

node:assert and node:assert/strict provide Node’s assertion functions, from ok and deepStrictEqual to throws, rejects, match and partialDeepStrictEqual.

Example

js
// assert.mjs
import assert from "node:assert/strict";

assert.deepEqual({ ids: [1, 2] }, { ids: [1, 2] });
assert.throws(() => JSON.parse("{"), SyntaxError);
await assert.rejects(Promise.reject(new Error("nope")), /nope/);

try {
  assert.equal(1 + 1, 3);
} catch (error) {
  console.log(error.name, error.code, error.actual, error.expected);
}
utf-8 · js · Lines: 12

elide run assert.mjs prints:

text
AssertionError ERR_ASSERTION 2 3
utf-8 · text · Lines: 1

Differences from Node

  • assert.Assert is not a constructor.
  • Loose deepEqual treats null and undefined as different, and compares proxies differently.
  • A symbol message, or a number as expected in throws(), raises AssertionError instead of ERR_INVALID_ARG_TYPE.
  • AssertionError.operator holds the method name (strictEqual), not a symbol (===).

Reference

Status: Partial · 7 of 13 suite tests pass (53.8%) · since 1.1.0

Exports

ExportKindStatusNotes
ok()functionFullTests if value is truthy
equal()functionFullTests shallow equality between actual and expected using ==
notEqual()functionFullTests shallow inequality between actual and expected using !=
strictEqual()functionFullTests strict equality between actual and expected using ===
notStrictEqual()functionFullTests strict inequality between actual and expected using !==
deepEqual()functionFullTests for deep equality between actual and expected using abstract equality (==) for leaf comparisons
deepStrictEqual()functionFullTests for deep strict equality between actual and expected using strict equality (===) for leaf…
notDeepEqual()functionFullTests that actual is NOT deeply equal to expected (abstract equality)
notDeepStrictEqual()functionFullTests that actual is NOT deeply strictly equal to expected
throws()functionFullExpects the function fn to throw an error
doesNotThrow()functionFullAsserts that the function fn does not throw an error
rejects()functionFullAwaits the asyncFn promise (or calls it if it’s a function) and expects it to reject
doesNotReject()functionFullAwaits the asyncFn promise (or calls it if it’s a function) and expects it NOT to reject
fail()functionFullThrows an AssertionError unconditionally
ifError()functionFullIf value is truthy (not null/undefined/false), throws it
match()functionFullExpects string to match the regular expression regexp
doesNotMatch()functionFullExpects string NOT to match the regular expression regexp

Not implemented

Assert, assert/strict.Assert.

Tests: node-api node:assert: 7 pass, 6 fail, 0 error, 1 skipped. Failing: test-assert-async.js, test-assert-class-destructuring.js, test-assert-class.js, test-assert-deep.js, test-assert-esm-cjs-message-verify.js, test-assert.js.

Was this page helpful?