assert.notDeepEqual() function in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The assert module provides a bunch of different functionalities that are used for function assertion. The Assert.notDeepEqual() checks inequality between the actual and expected paramters. Also the parameters should not be deep equal. An error will be thrown if the condition is not fulfilled.

Syntax

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

Parameters

The above parameters are described as below −

  • actual – This parameter will hold the actual value which needs to be compared.

  • expected – This will hold the expected parameter value that needs to be checked with.

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

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 – assertNotDeepEqual.js and copy the below code snippet. After creating the file use the below command to run this code.

node assertNotDeepEqual.js

assertNotDeepEqual.js

 Live Demo

// Importing the module
const assert = require('assert').strict;
try {
   // Both the values should not be identical
   assert.notDeepEqual({ a: '21' }, { a: '21' });
} catch(error) {
   console.log("Error: ", error)
}

Output

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

Example

Let's take a look at one more example.

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

try {
   // Both the values should not be identical
   assert.notDeepEqual({ a: 21 }, { a: '21' });
   console.log("Values are not identical")
} catch(error) {
   console.log("Error: ", error)
}

Output

C:\home\node>> node assertNotDeepEqual.js
Values are not identical

We can see in the above example that one value is of type string whereas the other value is of type integer which is why they are not equal.

raja
Published on 20-May-2021 11:34:14
Advertisements