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 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
// 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
// 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