• Node.js Video Tutorials

Node.js - assert.rejects() Function



The assert module provides a set of assertion functions for verifying invariants. The assert.rejects() function is an inbuilt function of the assert module of Node.js.

The Node.js assert.rejects() function awaits the promise passed in or the promise returned from the async function passed in and checks if the function is rejected.

Syntax

Following is the syntax of Node.js assert.rejects() function

assert.rejects(asyncFn [, error][, message]);

Parameters

This function accepts three parameters. The same are described below.

  • asyncFn − (Required) This parameter holds an async function that will throw errors synchronously.

  • error − (Optional) This parameter can hold Class, Regular Expression, Validation function, or an object where each property will be tested.

  • message − (Optional) String can be passed as an input into this parameter.

Return Value

If the async function throws an error or fails to return a promise, then assert.rejects() function will return a rejected promise with an error object.

Example

In the following example, we are passing an asyncFn and checking each property of the Node.js assert.rejects() function.

const assert = require('assert');
assert.rejects(
   async () => {
      throw new TypeError('This is an Error'); 
   },
   {
      name: 'TypeError',
      message: 'This is an Error'
   }
);

Output

When we compile and run the code, the function will not return a rejected promise with an error object.

// Returns nothing

Example

In the following example, we are passing an asyncFn and checking each property of the function.

const assert = require('assert').strict;
(async () => {
   assert.strictEqual(45, 46)
   await assert.rejects(
      async () => {
         throw new TypeError('This is an Error!');
      },
      (err) => {
         assert.strictEqual(err.name, 'TypeError');
         assert.strictEqual(err.message, 'This is an Error!');
         return true;
      }
   );
})();

Output

When we compile and run the code, the function returns a rejected promise with an error object as the async function is throwing an error. Because 45 !== 46.

(node:7151) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Input A
   expected to strictly equal input B:
+ expected - actual- 45
+ 46
   at /home/cg/root/639c3b5c17b34/main.js:4:10
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:15:3)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
(node:7151) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
   This error originated either by throwing inside of an async function without a catch block,
   or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7151) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
   In the future, promise rejections that are not handled will terminate the Node.js
   process with a non-zero exit code.

Example

In the following example, we are passing an asyncFn and in the error parameter, we are checking each property of the function.

const assert = require('assert').strict;
(async () => {
   assert.strictEqual(46, 46)
   await assert.rejects(
      async () => {
         throw new TypeError('This is an Error!');
      },
      (error) => {
         assert.strictEqual(error.name, 'TypeError');
         assert.strictEqual(error.message, 'This is an Error!');
         return true;
      }
   );
})();

Output

When we compile and run the code, the function doesn't return a rejected promise with an error object as the async function is not throwing an error. Because 46 == 46.

// Returns nothing
nodejs_assert_module.htm
Advertisements