assert.notDeepStrictEqual() function in Node.js


The assert module provides a bunch of different functionalities that are used for function assertion. The assert.notDeepStrictEqual tests that two objects should not be deep strict equal. An assertion error is thrown if both the objects are strictly equal.

Syntax

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

Parameters

The above parameters are described as below −

  • actual – This parameter contains the actual value that needs to be compared.

  • expected – This parameter will hold the expected values to be evaluated agains the actual parameters.

  • message – This is an optional parameter. This is a user defined message printed when the function is executed.

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").strict;

Example

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

node notDeepStrictEqual.js

notDeepStrictEqual.js

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

try {
   // Checking if actual and expected parameters are equal
   assert.notDeepStrictEqual({ a: '21' }, { a: '24' });
   console.log("Objects are not equal")
} catch(error) {
   console.log("Error: ", error)
}

Output

C:\home
ode>> node notDeepStrictEqual.js Objects are not equal

Example

Let's take a look at one more example.

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

try {
   // Checking if actual and expected parameters are equal
   assert.notDeepStrictEqual({ a: '21' }, { a: '21' });
   console.log("Objects are not equal")
} catch(error) {
   console.log("Error: ", error)
}

Output

C:\home
ode>> node notDeepStrictEqual.js Error: { AssertionError [ERR_ASSERTION]: Identical input passed to notDeepStrictEqual: {    a: '21' }       at Object. (/home/node/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: '21' },    expected: { a: '21' },    operator: 'notDeepStrictEqual' }

We can see that in the above example both the values are 21 and of string type.

Updated on: 20-May-2021

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements