Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is the deepEqual method of the assert module in Node.js?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The deepEqual method of the assert module in Node.js uses the == operator (Abstract Equality Comparison) to check for equality between two objects.

Deep equality means that the values of child objects are also compared.

The process is illustrated below:

Note: You can view a list of rules for the == operator here.

To use the deepEqual 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 deepEqual method is shown below:

deepEqual(actual, expected[, message])

Parameters

The deepEqual 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.

Return value

If the values are not equal, then the deepEqual 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.

Note: The deepEqual method also handles NaN(Not-A-Number) comparisons. If both values are NaN, the deepEqual method recognizes them as being identical.

Example

The code below shows how the deepEqual method works in Node.js:

const assert = require('assert');
// initializing objects
const 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 expression
try{
assert.deepEqual(first, second, "Assertion Error: The objects are not deep equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating second expression
try{
assert.deepEqual(first, third, "Assertion Error: The objects are not deep equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating third expression
try{
assert.deepEqual(first, fourth, "Assertion Error: The objects are not deep equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating fourth expression
try{
assert.deepEqual(first, fifth, "Assertion Error: The objects are not deep equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}

Explanation

The code above uses 44 different expressions to show the behavior of the deepEqual method.

First, 55 different objects are initialized. The objects first and second are identical. The object third has the same structure as first, but it 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., 3030. Finally, fifth is a Prototype Object created using first.

In the first expression in line 1212, the actual and expected parameters are identical objects with the value 1010, so the deepEqual method does not throw any errors. Therefore, only the try branch of the try-catch block executes.

In the second expression in line 2121, 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 deepEqual method only checks values, it considers the objects equal and does not throw any errors. Therefore, only the try branch of the try-catch block executes.

In the third expression in line 3030, 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 deepEqual method in line 3030.

Finally, the expression in line 3939 involves a comparison with a Prototype. Since the implementation of the deepEqual method does not test Prototypes, 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 deepEqual method in line 3939.

Note: You can read up further on the deepEqual method and other similar functions in the official documentation.

RELATED TAGS

node.js

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring