Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท util, assert and the Test Runner

Node.js assert() and assert.ok() function

Reference for assert() and assert.ok() in Node.js: check that a value is truthy, supply a custom message or error, and see how failures are reported.

assert(value) and assert.ok(value) are the same function. They do nothing when value is truthy and throw an AssertionError when it is falsy. They are the simplest check in the node:assert module and the right tool for conditions that have no better dedicated method, such as assert(list.length > 0).

Because they only test truthiness, they say little about what the values were. When you compare two values, prefer assert.strictEqual() or assert.deepStrictEqual(), which print both sides.

NODE
import assert from "node:assert/strict";

assert(1 + 1 === 2);
assert.ok("non-empty");

Try it

Node.js Passing and failing checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Custom messages and custom errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
assert(value: unknown, message?: string | Error): asserts value
assert.ok(value: unknown, message?: string | Error): asserts value
Name Description
value The value to test. Any truthy value passes.
message Optional. A string becomes the error message. An Error instance is thrown instead of an AssertionError.

Returns: undefined (in TypeScript the signature asserts value narrows the type after the call).

Errors: an AssertionError with code "ERR_ASSERTION" and operator "==". If you call assert.ok() with no arguments at all, the message says that no value argument was passed. With a custom message, generatedMessage is false.

Usage notes

  • Truthiness rules apply: 0, "", null, undefined, NaN and false fail; an empty array or object passes.
  • The default message depends on the environment. Node may try to show the source expression that failed, or fall back to a generic message such as false == true. Do not parse it; give an explicit message when the text matters.
  • Passing an Error as the message lets you throw your own error class, and the error's own type and message are kept.
  • Do not use assert for validating user input in production code paths that must keep working: assertions express programmer expectations. Throw a normal error for input problems.
  • assert.ok(value) and assert(value) differ only in spelling. In the legacy and strict modules both behave the same.
  • Use assert.strictEqual() for values: its failure message prints both values, while assert(a === b) only says false.