• Node.js Video Tutorials

Node.js - assert.notEqual() Function



The Node.js assert.notEqual() function will test both actual and expected parameters with the != operator. If the values are equal, then the function will throw AssertionError. Else if the values are not the same then the function will not return anything to the output.

This function is almost similar to assert.notStrictEqual() function.

Syntax

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

assert.notEqual(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 passing two distinct integers to the actual and expected parameters of the Node.js assert.notEqual() function.

const assert = require('assert');

var num1 = 5;
var num2 = 6;

assert.notEqual(num1, num2, "Both values are Not EQUAL");

Output

Since both the input parameters result differently, the function doesn't throw an AssertionError to the output.

// Returns nothing

Example

In the example below, we are passing two identical integers to the actual and expected parameters of the function.

const assert = require('assert');

var int1 = 65;
var int2 = 65;

assert.notEqual(int1, int2, "Both integers are EQUAL");

Output

Since both the input parameters result in the same, the function will throw an AssertionError along with the text in the message parameter.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both integers are EQUAL
   at Object.<anonymous> (/home/cg/root/639c38b0b0aed/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 following example below, we are creating two objects with distinct properties. Then we are passing both the objects as actual and expected to the function and also passed a text to the message parameter.

const assert = require('assert');
var obj1 = {
   Name : 'Krishna',
   Age : 28
};

var obj2 = {
   Name : 'Kasyap',
   Age : 30
};

assert.notEqual(obj1, obj2, "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 example below, we are passing two identical strings as inputs to the actual and expected parameters.

const assert = require('assert');

assert.notEqual('Tutorialspoint', 'Tutorialspoint', "Both the strings are EQUAL");

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.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both the strings are EQUAL
   at Object.<anonymous> (/home/cg/root/639c38b0b0aed/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