assert.doesNotThrow() function in Node.js


The assert module provides a bunch of different functionalities that are used for function assertion. The assert.doesNotThrow will assert that the function is unable to throw an error. It will immediately call 'fn' when assert .doesNotThrow is called.

Syntax

assert.doesNotThrow(fn, [error], [message])

Parameters

The above parameters are described as below −

  • fn – This function will be called when assert.doesNotThrow is executed. It will call this function instead of throwing an error.

  • error – This parameter contains the regular expression for the specified error. It is an optional paramter.

  • message – This is an optional parameter. This is a user defined message printed when the function is executed.

Installing the Assert Module

npm install assert

The assert module is an inbuilt Node.js module, so you can skip this step as well. You can check the assert version using the following command to get the latest assert module.

npm version assert

Importing the module in your function

const assert = require("assert").strict;

Example

Create a file with the name – doesNotThrow.js and copy the below code snippet. After creating the file use the below command to run this code.

node doesNotThrow.js

doesNotThrow.js

// Importing the module
const assert = require('assert').strict;

try {
   // Calling the doesNotThrow method to stop throwing error
   assert.doesNotThrow(
      () => {
         throw new TypeError('Wrong value received. Please check !');
      },
   );
} catch(error) {
   console.log("Error:", error)
}

Output

C:\home
ode>> node doesNotThrow.js Error: { AssertionError [ERR_ASSERTION]: Got unwanted exception. Actual message: "Wrong value received. Please check !"    at Object. (/home/node/test/assert.js:6:9)    at Module._compile (internal/modules/cjs/loader.js:778:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    at Module.load (internal/modules/cjs/loader.js:653:32)    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    at Function.Module._load (internal/modules/cjs/loader.js:585:3)    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)    at startup (internal/bootstrap/node.js:283:19)    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) generatedMessage: false, name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', actual:    TypeError: Wrong value received. Please check !       at assert.doesNotThrow (/home/node/test/assert.js:8:9)       at getActual (assert.js:567:5)       at Function.doesNotThrow (assert.js:687:32)       at Object. (/home/node/test/assert.js:6:9)       at Module._compile (internal/modules/cjs/loader.js:778:30)       at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)       at Module.load (internal/modules/cjs/loader.js:653:32)       at tryModuleLoad (internal/modules/cjs/loader.js:593:12)       at Function.Module._load (internal/modules/cjs/loader.js:585:3)       at Function.Module.runMain (internal/modules/cjs/loader.js:831:12),    expected: undefined,    operator: 'doesNotThrow' }

Example

 Live Demo

Let's take a look at one more example.

// Importing the module
const assert = require('assert').strict;

try {
   assert.doesNotThrow(
      () => {
         throw new TypeError('Value passed is wrong !!');
      },
      /abcd/,
      'Whoops'
   );
} catch(error) {
   console.log("Error:", error)
}

Output

C:\home
ode>> node doesNotThrow.js Error: TypeError: Value passed is wrong !!    at assert.doesNotThrow (/home/node/test/assert.js:7:9)    at getActual (assert.js:567:5)    at Function.doesNotThrow (assert.js:687:32)    at Object. (/home/node/test/assert.js:5:9)    at Module._compile (internal/modules/cjs/loader.js:778:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    at Module.load (internal/modules/cjs/loader.js:653:32)    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    at Function.Module._load (internal/modules/cjs/loader.js:585:3)    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)

Updated on: 20-May-2021

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements