The deepStrictEqual
method of the assert
module in Node.js uses the ===
operator (SameValue Comparison) to check for strict equality between two objects.
Strict deep equality means that the values and types of child objects are also compared.
The process is illustrated below:
To use the deepStrictEqual
method, you will need to install the assert
module using the command prompt, as shown below:
npm install assert
After the installation is complete, you will need to import the assert
module into the program, as shown below:
const assert = require('assert');
The prototype of the deepStrictEqual
method is shown below:
deepStrictEqual(actual, expected[, message])
The deepStrictEqual
method takes the following parameters:
actual
: The first of the two objects to compare.
expected
: The second of the two objects to compare.
message
: An optional parameter that holds the error message in case of an AssertionError. If this parameter is left empty, a default message is assigned.
If the values are not strictly deep-equal, then the deepStrictEqual
method throws an AssertionError
and the program terminates; otherwise, execution continues as normal.
In case of an error, the message
property of the AssertionError
is set equal to the message
parameter. If the message
parameter is not provided, a default value is assigned to the message
property of the AssertionError
.
The code below shows how the deepStrictEqual
method works in Node.js:
const assert = require('assert');// initializing objectsconst first = { a : { b : 10 } };const second = { a : { b : 10 } };const third = { a : { b : '10' } };const fourth = { a : { b : 30 } };const fifth = Object.create(first);// evaluating first expressiontry{assert.deepStrictEqual(first, second, "Assertion Error: The objects are not deep strictly equal.")console.log("No error.")}catch(error){console.log(error.message)}// evaluating second expressiontry{assert.deepStrictEqual(first, third, "Assertion Error: The objects are not deep strictly equal.")console.log("No error.")}catch(error){console.log(error.message)}// evaluating third expressiontry{assert.deepStrictEqual(first, fourth, "Assertion Error: The objects are not deep strictly equal.")console.log("No error.")}catch(error){console.log(error.message)}// evaluating fourth expressiontry{assert.deepStrictEqual(first, fifth, "Assertion Error: The objects are not deep strictly equal.")console.log("No error.")}catch(error){console.log(error.message)}
The code above uses different expressions to show the behavior of the deepStrictEqual
method.
First, different objects are initialized. The objects first
and second
are identical. The object third
has the same structure as first
, but third
has a value of type string rather than an integer. Similarly, fourth
also has the same structure as first
, but the value is different, i.e., . Finally, fifth
is a Prototype Object created using first
.
In the first expression in line , the actual
and expected
parameters are identical objects with the value , so the deepStrictEqual
method does not throw any errors. Therefore, only the try
branch of the try-catch
block executes.
In the second expression in line , the actual
and expected
parameters have identical structures but have different types. The actual
parameter is an integer, whereas the expected
parameter is a string. Since the deepStrictEqual
method requires the same types, it considers the two objects unequal and throws an error, which triggers the catch
branch of the try-catch
block. The code outputs the message associated with the error, i.e., the string provided as the message
parameter to the deepStrictEqual
method in line .
In the third expression in line , the actual
and expected
parameters are unequal, so an error is thrown, which triggers the catch
branch of the try-catch
block. The code outputs the message associated with the error, i.e., the string provided as the message
parameter to the deepStrictEqual
method in line .
Finally, the expression in line involves a comparison with a Prototype
. Since the Prototype
is not strictly deep-equal with first
, an error is thrown, which triggers the catch
branch of the try-catch
block. The code outputs the message associated with the error, i.e., the string provided as the message
parameter to the deepStrictEqual
method in line .
Note: You can read up further on the
deepStrictEqual
method and other similar functions in the official documentation.