The assert module provides a bunch of different functionalities that are used for function assertion. The assert.notEqual tests that two objects should not be equal. An assertion error is thrown if both the objects are equal.
Syntax
assert.notEqual (actual, expected, [message])
Parameters
The above parameters are described as below −
actual – This parameter contains the actual value that needs to be compared.
expected – This parameter will hold the expected values to be evaluated agains the actual parameters.
message – This is an optional parameter. This is a user defined message printed when the function is executed.
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 – notDeepStrictEqual.js and copy the below code snippet. After creating the file use the below command to run this code.
node notEqual.js
notDeepStrictEqual.js
// Importing the module const assert = require('assert').strict; var a = 3; var b = "3"; try { // Checking if a & b are equal assert.notEqual(a, b) console.log("a & b are not equal") } catch(error) { console.log("Error Occured: ", error) }
Output
C:\home\node>> node notEqual.js a & b are not equal
Example
Let's take a look at one more example.
// Import the module const assert = require('assert').strict; var a = 3; var b = 3; try { // Checking if a & b are equal assert.notEqual(a, b) console.log("a & b are not equal") } catch(error) { console.log("Error Occured: ", error) }
Output
C:\home\node>> node notEqual.js Error Occured: { AssertionError [ERR_ASSERTION]: Identical input passed to notStrictEqual: 3 at Object.<anonymous> (/home/node/test/assert.js:10: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: 3, expected: 3, operator: 'notStrictEqual' }