Node.js assert.ok() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.ok() function tests if the value is true or not. If the condition is true it will not produce an output else an assertion error is raised.
Syntax:
assert.ok(value[, message])
Parameters: This function accepts two parameters as mentioned above and described below:
- value: This parameter holds the expression that need to be evaluated.
- message: This parameter holds the error message of string type.
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
- After installing the assert module, you can check your assert version in command prompt using the command.
npm version assert
- After that, you can create a folder and add a file for example, index.js as shown below.
Note: Installation is an optional step as it is inbuilt Node.js module.
Example 1: Filename: index.js
javascript
// Requiring the module const assert = require( 'assert' ).strict; // Function call try { assert.ok( false , "It's false" ); } 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]: It’s false
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: false,
expected: true,
operator: ‘==’
}
Example 2: Filename: index.js
javascript
// Requiring the module const assert = require( 'assert' ).strict; // Function call try { assert.ok( typeof 12345 === 'number' ); console.log( "No error Occurred" ) } catch (error) { console.log( "Error: " , error) } // Function call try { assert.ok( typeof 12456 === 'string' ); } catch (error) { console.log( "Error Occurred: " , error) } |
Steps to run the program:
- The project structure will look like this:
- Run index.js file using below command:
node index.js
- Output:
No error Occurred
Error Occurred: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:assert.ok(typeof 12456 === ‘string’)
at Object. (C:\Users\Lenovo\Downloads\index.js:14: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: false,
expected: true,
operator: ‘==’
}
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_ok_value_message
Contact Us