• Node.js Video Tutorials

Node.js - assert.notDeepEqual() Function



The assert module provides a set of assertion functions for verifying invariants. The Node.js assert.notDeepEqual() function is an inbuilt function of the assert module of Node.js.

The Node.js assert.notDeepEqual() function will compare whether the values passed are deeply equal or not. If the values are deeply equal the function will throw an AssertionError with the text inside the message parameter. Else the values are not equal it will not throw any AssertionError and it doesn't return anything to output.

This method is almost similar to assert.notDeepStrictEqual() function and the opposite of assert.deepEqual() function.

Syntax

Following is the syntax of Node.js assert.notDeepEqual() function

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

Parameters

This method accepts three parameters. The same is 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 an AssertionError on the terminal if both actual and expected are matched.

Example

In the example below, we are passing two different integer values as actual and expected to the Node.js assert.notDeepEqual() function and also passing a text to the message parameter.

const assert = require('assert');
var num1 = 34;
var num2 = 45;
assert.notDeepEqual(num1, num2, "Both the numbers are NOT EQUAL");

Output

When we compile and run the code, the function will not throw AssertionError to the output as both actual and expected are not matching.

// Returns nothing

Example

In the example below, we are passing two inputs, an integer to the actual parameter and a string to the expected parameter of the Node.js assert.notDeepEqual () function.

const assert = require('assert');
var string1 = '7';
var num1 = 7;
assert.notDeepEqual(string1, num1, "Both the values are EQUAL");

Output

When we compile and run the code, the function will throw AssertionError to the output because 7 == '7'.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both the values are EQUAL
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/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 nested objects with different properties. Then we are passing both the objects as actual and expected to the function and also passing a text to the messageparameter.

const assert = require('assert');
var object1 = {
   Name : 'Mike',
   Id : 17,
   Salary : {
      "2020-2021" : '45000INR',
      "2021-2022" : '50000INR',
   },
   address: {
      Area: {
         address1: "White field road",
         address2: "Brown wheat field",
      },
   }
};

var object2 = {
   Name : 'Mike',
   Id : 17,
   Salary : {
      "2020-2021" : '45000INR',
      "2021-2022" : '50000INR',
   },
   address: {
      Area: {
         address1: "White field road",
         address2: "Brown wheat field",
      },
   }
};
assert.notDeepEqual(object1, object2, "Both the objects are EQUAL")

Output

When we compile and run the above program, it throws AssertionError along with the message to the output as both actual and expected are identical.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both the objects are EQUAL
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:33: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 nested objects with similar properties. Then we are passing both the objects as actual and expected to the function and also passing a text to the message parameter.

const assert = require('assert');
var object1 = {
   Name : 'John',
   Id : 27,
   Salary : {
      "2020-2021" : '55000INR',
      "2021-2022" : '70000INR',
   },
};

var object2 = {
   Name : 'Mike',
   Id : 17,
   Salary : {
      "2020-2021" : '45000INR',
      "2021-2022" : '50000INR',
   },
};

assert.notDeepEqual(object1, object2, "Both the objects are NOT EQUAL");

Output

So when we compile and run the code, the function will not throw AssertionError to the output as both actual and expected are not the same.

// Returns nothing
nodejs_assert_module.htm
Advertisements