Skip to content
Related Articles

Related Articles

Improve Article

Node.js assert.notStrictEqual() Function

  • Last Updated : 09 Aug, 2020

The assert module provides a set of assertion functions for verifying invariants. The assert.notStrictEqual() function tests strict inequality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised.

Syntax:

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

Parameters: This function has three parameters, the actual parameter is of any type, the expected parameter is of any type and the message parameter can be of string or error type.
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
  2. Note: Installation is an optional step as it is inbuilt Node.js module.
  3. After installing the assert module, you can check your assert version in command prompt using the command.
    npm version assert
  4. 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;
  
var a = 2;
var b = 2;
   
// Function call
try {
    assert.notStrictEqual(a, b)
} 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
  3. Output:

    Error: AssertionError [ERR_ASSERTION]: Expected “actual” to be strictly unequal to: 2
    at Object. (C:\Users\Lenovo\Downloads\index.js:9: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: 2,
    expected: 2,
    operator: ‘notStrictEqual’
    }

Example 2: Filename: index.js

javascript




// Requiring the module
const assert = require('assert').strict;
  
var a = 4;
var b = "Four";
   
// Function call
try {
    assert.notStrictEqual(a, b)
    console.log("No Error Occured")
} catch(error) {
    console.log("Error: ", error)
}
  
a = 4;
b = 4;
  
// Function call
try {
    assert.notStrictEqual(a, b)
} catch(error) {
    console.log("Error Occured: ", 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
  3. Output:

    No Error Occured
    Error Occured: AssertionError [ERR_ASSERTION]: Expected “actual” to be strictly
    unequal to: 4
    at Object. (C:\Users\Lenovo\Downloads\index.js:20: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: 4,
    expected: 4,
    operator: ‘notStrictEqual’
    }

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :