Node.js Assert Complete Reference
Assert module in Node.js provides a bunch of facilities that are useful for the assertion of the function. The assert module provides a set of assertion functions for verifying invariants. If the condition is true it will output nothing else an assertion error is given by the console.
Example:
Javascript
// Requiring the module const assert = require( 'assert' ).strict; var a = "GeeksforGeeks" ; var b = "Geeks4Geek" ; // Function call try { assert.strictEqual(a, b); } catch (error) { console.log( "Error: " , error) } |
Output:
Error: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual – expected + ‘GeeksforGeeks’ – ‘Geeks4Geek’ ^ at Object. (C:\Users\Lenovo\Downloads\index.js:4: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: ‘GeeksforGeeks’, expected: ‘Geeks4Geek’, operator: ‘strictEqual’ }
The Complete List of NodeJS Assert are listed below:an
NodeJS Assert |
Description |
---|---|
Node.js assert() Function | If the value is not the truth, then a AssertionError is thrown with a message property set equal to the value of the message parameter. |
Node.js assert.deepStrictEqual() Function | The assert.deepStrictEqual() function tests for deep equality between the actual and expected parameters. |
Node.js assert.doesNotThrow() Function | The assert.doesNotThrow() function asserts that the function fn does not throw an error. |
Node.js assert.equal() Function | The assert.equal() function tests for equality between the actual and the expected parameters. |
Node.js assert.fail() Function | The assert.fail() function throws an AssertionError with the provided error message or with a default error message. |
Node.js assert.ifError() Function | The assert.ifError() function throws a value if the value is not defined or null. |
Node.js assert.match() Function | The assert.match() function expects the string input to match the regular expression. |
Node.js assert.notDeepEqual() Function | The assert.notDeepEqual() function tests deep strict inequality between the actual and the expected parameters. |
Node.js assert.notDeepStrictEqual() Function | The assert.notDeepStrictEqual() function tests for deep strict inequality. |
Node.js assert.notEqual() Function | The assert.notEqual() function tests strict inequality between the actual and the expected parameters. |
Node.js assert.ok() Function | The assert module provides a set of assertion functions for verifying invariants. |
Node.js assert.rejects() Function | The assert.rejects() function awaits the asyncFn promise |
Node.js assert.strictEqual() Function | The assert.strictEqual() function tests strict equality between the actual and expected parameters. |