Open In App

Node.js assert.notDeepStrictEqual() Function

Last Updated : 13 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The assert module provides a set of assertion functions for verifying invariants. The assert.notDeepStrictEqual() function tests for deep strict inequality. If the condition is true it will not produce an output else an assertion error is raised. Syntax:

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

Parameters: This function accepts the following parameters as mentioned above and described below:

  • actual: This parameter holds the actual value that need to be evaluated. It is of any type.
  • expected: This parameter holds the expected value which is matched against actual value. It is of any type.
  • message: This parameter holds the error message of string or error type. It is an optional parameter.

Return Value: This function returns assertion error of object type.
Installation of assert module:

  1. You can visit the link to Install assert module. You can install this package by using this command.
npm install assert
  1. Note: Installation is an optional step as it is inbuilt Node.js module.
  2. After installing the assert module, you can check your assert version in command prompt using the command.
npm version assert
  1. After that, you can create a folder and add a file for example, index.js as shown below.

Example 1: Filename: index.js

javascript




// Requiring the module
const assert = require('assert').strict;
    
// Function call
try {
  assert.notDeepStrictEqual({ a: '1' }, { a: '24' });
  console.log("No Error Occurred")
} catch(error) {
  console.log("Error: ", error)
}


Steps to run the program:

  1. The project structure will look like this:
  2. Run index.js file using below command:
node index.js
  1. Output:
No Error Occurred

Example 2: Filename: index.js

javascript




// Requiring the module
const assert = require('assert').strict;
   
// Function call
try {
    assert.notDeepStrictEqual({ a: '1' }, { a: '1' })
} catch(error) {
    console.log("Error: ", error)
}


Steps to run the program: 
 

  1. The project structure will look like this:
  2. Run index.js file using below command:
node index.js
  1. Output:

Error: AssertionError [ERR_ASSERTION]: Expected “actual” not to be strictly deep-equal to: { a: ‘1’ } at Object. (C:\Users\Lenovo\Downloads\index.js:14:12) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘notDeepStrictEqual’ }

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_notdeepstrictequal_actual_expected_message



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads