• Node.js Video Tutorials

Node.js assert.notDeepStrictEqual() Function



The Node.js assert.notDeepStrictEqual() function will deeply, strictly, and equally compare the input (actual and expected) values passed that are not equal. It will throw an AssertionError if both the input values are the same and it returns nothing if both the values are distinct.

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

Syntax

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

assert.notDeepStrictEqual(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 matched.

Example

In the following example below, we are creating two nested objects with different properties. Then we are passing both the objects as actual and expected to the Node.js assert.notDeepStrictEqual() function and also passed a text to the message parameter.

const assert = require('assert');
var Movie1 = {
   Name : 'RRR',
   Director : 'Rajamouli',
   Boxoffice : {
      "2022-2023" : '1200 crores',
   },
};

var Movie2 = {
   Name : 'KGf',
   Director : 'Prasanth Neel',
   Boxoffice : {
      "2022-2023" : '1400 crores',
   },
};

assert.notDeepStrictEqual(Movie1, Movie2, "Both the objects are NOT EQUAL");

Output

Since the actual and expected results are different, the function will not throw an AssertionError when the code is compiled and executed.

// Returns nothing

Example

In the following example below, we are creating two nested objects with similar properties. Then we are passing both the objects to actual and expected parameters along with a text to the message parameter.

const assert = require('assert');
var Movie1 = {
   Name : 'RRR',
   Director : 'Rajamouli',
   Boxoffice : {
      "2022-2023" : '1200 crores',
   },
};

var Movie2 = {
   Name : 'RRR',
   Director : 'Rajamouli',
   Boxoffice : {
      "2022-2023" : '1200 crores',
   },
};

assert.notDeepStrictEqual(Movie1, Movie2, "Both the objects are EQUAL");

Output

Since the actual and expected results are the same, the function will throw an AssertionError along with the message to the output.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both the objects are EQUAL
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:19: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 passing -0 and -0 to the function to compare.

const assert = require('assert');

assert.notDeepStrictEqual(-0, -0, "Both the values are EQUAL");

Output

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

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both the values are EQUAL
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:3: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 passing -0 and 0 to the function to compare.

const assert = require('assert');

assert.notDeepStrictEqual(-0, 0, "Both the values are NOT EQUAL");

Output

When we compile and run the code, the function doesn't throw AssertionError to the output as both actual and expected are not the same.

// Returns nothing

Example

In the example below, we are passing two identical strings as inputs to the actual and expected parameters.

const assert = require('assert');

assert.notDeepStrictEqual('Hello', 'Hello');

Output

Since the actual and expected results are the same, the function will throw an AssertionError to the output when the code is compiled and executed.

As there is no text passed inside the message parameter, the function will assign a default error message.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Identical input passed to notDeepStrictEqual: 'Hello'
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:3: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)
nodejs_assert_module.htm
Advertisements