
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
Node.js - assert.deepEqual() Function
The NodeJS assert.deepEqual() function will test deep equality on both of its parameters actual and expected. Whenever both the parameters are similar, the function doesn't throw AssertionError. If both the parameters are not similar, then it will throw an AssertionError to the output. This function is an alias of assert.deepStrictEqaul() function.
The Node.js assert.deepEqual() function is an inbuilt function of the assert module of Node.js.
Syntax
Following is the syntax of Node.js assert.deepEqual() function −
assert.deepEqual(actual, expected[, message]);
Parameters
This function accepts three parameters. The same are described below.
actual − (required) The value passed in this parameter will be evaluated. The value can be of any type.
expected − (required) The value passed in this parameter will be compared to the actual value. The value can be of any type.
message − (optional) String or Error type can be passed as an input into this parameter.
Return Value
This function will return the AssertionError on the terminal if both actual and expected are not matched.
Example
In the example below, we are performing the addition and multiplication of two numbers and storing the results. Then we are passing both the results as actual and expected to the Node.js assert.deepEqual() function and also passed a text to the message parameter.
const assert = require('assert'); var x = 10, y = 5, z = 6; var add = x + z; var mul = y * x; assert.deepEqual(add, mul, 'Both the values are not similar');
Output
When we compile and run the code, the function will throw AssertionError along with the message to the output as both actual and expected are not matching.
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the values are not similar at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:7:8) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
Example
In the example below, we are creating two objects with some properties. Then we are passing both the objects as actual and expected to the Node.js assert.deepEqual() function and also passed a text to the message parameter.
const assert = require('assert'); var obj1 = {'name': 'Jay', 'age': 21}; var obj2 = {'name': 'Vikram', 'age': 51}; assert.deepEqual(obj1, obj2, 'Both the objects are not identical');
Output
When we compile and run the code, the function will throw AssertionError along with the message to the output as both actual and expected are not identical.
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the objects are not identical at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:6:8) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
Example
In the example below, we are creating two integer arrays and both arrays are similar. Then we are passing both the arrays as actual and expected to the Node.js assert.deepEqual() function and also passed a text to the message parameter.
const assert = require('assert'); var array1 = [1, 2, 3, 4, 5]; var array2 = [1, 2, 3, 4, 5]; assert.deepEqual(array1, array2, 'Both the arrays identical');
Output
When we compile and run the code, the function will not throw any AssertionError to the output as both actual and expected are identical. Instead; the function will not return anything to the output.
// Returns nothing