assert.fail() function in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The assert module provides a bunch of different functionalities that are used for function assertion. The Assert.fail() throws an assertion error without checking anything. The error produced is either a default message or message passed in the function.

Syntax

assert.fail(message)

Parameters

The above parameters are described as below −

  • 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 – assertFail.js and copy the below code snippet. After creating the file use the below command to run this code.

node assertFail.js

assertFail.js

 Live Demo

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

// Function call
try {
   assert.fail("Error Occured");
} catch(error) {
   console.log("Error:", error)
}

Output

C:\home\node>> node assertFail.js
Error: { AssertionError [ERR_ASSERTION]: Error Occured
      at Object. (/home/node/mysql-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: undefined,
   expected: undefined,
   operator: 'fail' }

Example

Let's take a look at one more example.

 Live Demo

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

// Function call
try {
   assert.fail(new TypeError("Custom Defined Error: Not Supported"));
} catch(error) {
   console.log("Error:", error)
}

Output

C:\home\node>> node assertFail.js
Error: TypeError: Custom Defined Error: Not Supported
   at Object. (/home/node/mysql-test/assert.js:6:14)
   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)

We can see in the above example that we have passed a custom error – Type Error. Custom error will be passed if an instance of the error is passed instead of the Assertion Error.

raja
Published on 20-May-2021 11:31:32
Advertisements