Node.js Assert Function
Node.js assert.deepStrictEqual() Function
When we say about the assert module this will provides a set of assertion functions to varifying invariants. And this assert.deepStrictEqual() function will tests for deep equality between the actual and expected parameters. IF the condition is true this will not produce an output else an assertion error is raised.
Below is the syntax for Node.js assert.deepStrictEqual() function
assert.deepStrictEqual(actual, expected[, message])
Above function will accept the following parameters as mentioned above and described as given below:-
(1)Actual:-This parameter will holds the actual value which need to be evaluated. And this is of any type.
(2)Expected:-This parameter will holds the expected value which is matched against actual value. And this is of any type.
(3)Message:-This parameter will holds the error message of string or error type. And this is and optional parameter.
This function will returns assertion error of object type as the Return value.
Use of Assert module:-
(1)You need to install Assert module and to intsall this package by using below command.
npm install assert
(2)Although installation is an optional step as it is inbuilt Node.js module.
(3)Once you install the assert module, you can check the seert version by below command.
npm version assert
Now we will create one sample code to check deepStrictEqual() function. Below is the code and file name is deepStrictFunction.js.
// Requiring the module
const assert = require('assert').strict;
// Function call
try {
assert.deepStrictEqual({ a: 1 }, { a: '1' });
} catch(error) {
console.log("Error: ", error)
}
Once we run the above file with Node.js command prompt we will use below command
Node deepStrictFunction.js
Once we run above command we will get the error as given below
Now we will take one more example where we have write the correct code with no error. And we use deepStrictEqual() function and name this file is deepFunction.js and code is given below.
// Requiring the module
const assert = require('assert').strict;
// Function call
try {
assert.deepStrictEqual({ a: '3' }, { a: '3' });
console.log("No Error Occurred")
} catch(error) {
console.log("Error: ", error)
}
Once we run the above in Node.js command prompt we will get the below output.