Assertions

assert.rejects()

Checks a rejected promise or the promise returned by a function. Await the assertion so a failed assertion belongs to the test lifecycle. Match an error type or code rather than brittle full messages. Tested on Node.js 22.16.0. Example output (one line per entry): ["matched", "ERR_ASSERTION"].

Formal Syntax & Structure

nodejs syntaxnodejs
assert.rejects(asyncFn[, error][, message])

Parameters & Attributes

NameTypeDescription
asyncFnFunction | PromiseAsynchronous operation.
errorRegExp | Function | objectExpected rejection matcher.
messagestring | ErrorFailure detail.

Return Value

Promise<void>

Fulfills when the rejection matches.

Working Example

Example Snippetnodejs
import assert from 'node:assert/strict';await assert.rejects(async () => { throw new RangeError('limit'); }, RangeError);console.log('matched');try {    await assert.rejects(Promise.resolve(1));}catch (e) {    console.log(e.code);}
Node.js Reference