Working with Assert Module

1. Using assert() for Truthy Checks

Example

import assert from "node:assert/strict";
assert(value, "value must be truthy");
Note: Always import assert/strict — uses === instead of ==.

2. Using assert.ok for Boolean Checks

APIEquivalent
assert.ok(x, msg)Same as assert(x, msg)

3. Using assert.strictEqual (===)

CheckBehavior
strictEqual(a,b)Throws if a !== b
NaN handlingNaN === NaN in strict mode (Object.is)

4. Using assert.notStrictEqual (!==)

APIThrows When
notStrictEqual(a,b)a === b

5. Using assert.deepStrictEqual

Example

assert.deepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // OK
assert.deepStrictEqual(new Map([["a",1]]), new Map([["a",1]]));
ComparesDetail
Own enumerable propsRecursively
PrototypesMust match
SymbolsCompared

6. Using assert.notDeepStrictEqual

APIThrows When
notDeepStrictEqual(a,b)Deep equal

7. Using assert.throws (error testing)

Example

assert.throws(() => JSON.parse("nope"), {
  name: "SyntaxError",
  message: /Unexpected token/
});

8. Using assert.rejects

Example

await assert.rejects(fetchUser(-1), { message: /not found/ });

9. Using assert.doesNotThrow

APIUse
doesNotThrow(fn)Asserts no synchronous throw
doesNotReject(promise)Async variant

10. Using assert.ifError

APIBehavior
assert.ifError(err)Throws if err is truthy
Use caseNode-style callbacks

11. Using assert.match

APIUse
assert.match(str, /pattern/)Throws if no match
assert.doesNotMatchReverse

12. Creating Custom Assertion Messages

Example: AssertionError

throw new assert.AssertionError({
  message: "user must be active",
  actual: user.status,
  expected: "active",
  operator: "==="
});