assert.deepStrictEqual() function in Node.js


The assert module provides a bunch of different functionalities that are used for function assertion. One of them is deepStrictEqual() function. This function is used to test the deep equality between the actual and expected parameters. An assertion error will be raised if the condition is not fulfilled.

Syntax

assert.deepStrictEqual(actual, expected[, message])

Parameters

The above parameters are described as below −

  • actual – This is the actual value that will be evaluated against the expected parameters.

  • expected – This is the expected parameter value which is matched against the actual value.

  • message – This parameter holds the string message value to be printed if the actual and expected parameters do not match. It is an optional field.

Installing the Assert Module

npm install assert

The assert module is an inbuilt Node.js module, so you can skip this step as well. You can check the assert version using the following command to get the latest assert module.

npm version assert

Importing the module in your function

const assert = require("assert");

Example

Create a file with name – assertDeepStrict.js and copy the below code snippet. After creating file, use the following command to run this code.

node assertDeepStrict.js

assertDeepStrict.js

// Importing the assert module
const assert = require('assert').strict;

try {
   // Calling the deep strict function
   assert.deepStrictEqual({ a: 3 }, { a: '3' });
   console.log("No Error Occured...")
} catch(error) {
   console.log("Error: ", error)
}

Output

C:\home
ode>> node assertDeepStrict.js Error: { AssertionError [ERR_ASSERTION]: Input A expected to strictly deepequal input B: + expected - actual    {       - a: 3       + a: '3'    }    at Object.<anonymous> (/home/node/mysql-test/assert.js:6:9)    at Module._compile (internal/modules/cjs/loader.js:778:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    at Module.load (internal/modules/cjs/loader.js:653:32)    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    at Function.Module._load (internal/modules/cjs/loader.js:585:3)    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)    at startup (internal/bootstrap/node.js:283:19)    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) generatedMessage: true, name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', actual: { a: 3 }, expected: { a: '3' }, operator: 'deepStrictEqual' }

We can see in the above example that one value was integer whereas the other value was string. Due to this the method threw the above error.

Example

Let's take a look at one more example.

// Importing the assert module
const assert = require('assert').strict;

try {
   // Calling the deep strict function
   // Both the values are string
   assert.deepStrictEqual({ a: '3' }, { a: '3' });
   console.log("No Error Occured...")
} catch(error) {
   console.log("Error: ", error)
}

Output

C:\home
ode>> node assertDeepStrict.js No Error Occured...

Updated on: 20-May-2021

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements