Node.js assert.fail() Function Last Updated : 07 Aug, 2020 Comments Improve Suggest changes Like Article Like Report The assert module provides a set of assertion functions for verifying invariants. The assert.fail() function throws an AssertionError with the provided the error message or with a default error message. Syntax: assert.fail([message]) Parameters: This function accepts following parameters as mentioned above and described below: message This parameter holds the error message of string or error type. It is an optional parameter. Return Value: This function returns assertion error of object type. Installation of assert module: You can visit the link to Install assert module. You can install this package by using this command. npm install assert Note: Installation is an optional step as it is inbuilt Node.js module. After installing the assert module, you can check your assert version in command prompt using the command. npm version assert After that, you can just create a folder and add a file for example, index.js as shown below. Example 1: Filename: index.js javascript // Requiring the module const assert = require('assert').strict; // Function call try { assert.fail(); } catch(error) { console.log("Error:", error) } Steps to run the program: The project structure will look like this: Run index.js file using below command: node index.js Output: Error: AssertionError [ERR_ASSERTION]: Failed at Object. (C:\Users\Lenovo\Downloads\index.js:6:12) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: undefined, expected: undefined, operator: 'fail' } Example 2: Filename: index.js javascript // Requiring the module const assert = require('assert').strict; // Function call try { assert.fail(new TypeError('My custom defined error')); } catch(error) { console.log("Error:", error) } Steps to run the program: The project structure will look like this: Run index.js file using below command: node index.js Output: Error: TypeError: My custom defined error at Object. (C:\Users\Lenovo\Downloads\index.js:6:17) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_fail_message Comment More infoAdvertise with us Next Article Node.js assert.ifError() Function gouravhammad Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods NodeJS-assert Similar Reads Node.js assert() Function The assert() function in Node.js is used for testing and verifying assumptions in your code. It is part of the built-in assert module, which provides a set of assertion functions to perform various checks and validations.Node assert FunctionIn assert() function, if the value is not truth, then a Ass 3 min read Node.js assert.deepStrictEqual() Function The assert module provides a set of assertion functions for verifying invariants. The assert.deepStrictEqual() function tests for deep equality between the actual and expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.deepStr 2 min read Node.js assert.doesNotThrow() Function The assert module provides a set of assertion functions for verifying invariants. The assert.doesNotThrow() function asserts that the function fn does not throw an error. Syntax: assert.doesNotThrow(fn[, error][, message]) Parameters: This function accepts the following parameters as mentioned above 3 min read Node.js assert.equal() Function The assert module provides a set of assertion functions for verifying invariants. The assert.equal() function tests for equality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.equal(actual, exp 2 min read Node.js assert.fail() Function The assert module provides a set of assertion functions for verifying invariants. The assert.fail() function throws an AssertionError with the provided the error message or with a default error message. Syntax: assert.fail([message]) Parameters: This function accepts following parameters as mentione 2 min read Node.js assert.ifError() Function The assert module provides a set of assertion functions for verifying invariants. The assert.ifError() function throws value if value is not undefined or null. When testing the error argument in callbacks, this function is very useful.Syntax:  assert.ifError(value) value: This parameter holds the ac 2 min read Node.js assert.match() Function The assert module provides a set of assertion functions for verifying invariants. The assert.match() function expects the string input to match the regular expression. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.match(string, regexp[, mes 2 min read Node.js assert.notDeepEqual() Function The assert module provides a set of assertion functions for verifying invariants. The assert.notDeepEqual() function tests deep strict inequality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised.Syntax: assert.no 2 min read Node.js assert.notDeepStrictEqual() Function The assert module provides a set of assertion functions for verifying invariants. The assert.notDeepStrictEqual() function tests for deep strict inequality. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.notDeepStrictEqual(actual, expected[, 2 min read Node.js assert.notEqual() Function The assert module provides a set of assertion functions for verifying invariants. The assert.notEqual() function tests strict inequality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.notEqual( 2 min read Like