Node.js assert.doesNotThrow() Function Last Updated : 07 Aug, 2020 Comments Improve Suggest changes 1 Likes Like Report 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 and described below: fn: This parameter is a function which does not throw an error. error: This parameter is a regular expression or function. It is the specified error. It is an optional parameter. 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.doesNotThrow( () => { throw new TypeError('Wrong value'); }, ); } 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]: Got unwanted exception. Actual message: "Wrong value" 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: false, code: 'ERR_ASSERTION', actual: TypeError: Wrong value at C:\Users\Lenovo\index.js:8:17 at getActual (assert.js:657:5) at Function.doesNotThrow (assert.js:805:32) at Object. (C:\Users\NEW\Assert Function\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, expected: undefined, operator: 'doesNotThrow' } Example 2: Filename: index.js javascript // Requiring the module const assert = require('assert').strict; // Function call try { assert.doesNotThrow( () => { throw new TypeError('The Wrong value error'); }, /abcd/, 'Whoops' ); } 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: The Wrong value error at C:\Users\Lenovo\Downloads\Geeksforgeeks Internship\NEW\Assert\index.js:8:17 at getActual (assert.js:657:5) at Function.doesNotThrow (assert.js:805:32) at Object. (C:\Internship\NEW\Assert Function\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 Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_doesnotthrow_fn_error_message Create Quiz Comment G gouravhammad Follow 1 Improve G gouravhammad Follow 1 Improve Article Tags : Web Technologies Node.js Node.js-Methods NodeJS-assert Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like