• Node.js Video Tutorials

Node.js - assert.equal() Function



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

The Node.js assert.equal() function will test shallow equality on both of its input parameters using the == operator.

Whenever both the parameters are similar, the function doesn't throw AssertionError. If both the parameters are not similar, then it will throw an AssertionError to the output. This function is an alias of assert.strictEqaul() function.

Syntax

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

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

Note − NaN on both (actual & expected) sides is considered identical because it is specially handled and treated.

Example

In the example below, we are passing two identical integers as action and expected arguments of the Node.js assert.equal() function.

const assert = require('assert');
var num1 = 99;
var num2 = 99;
assert.equal(num1, num2, 'Both numbers are same');

Output

Since both action and expected are the same, after executing the code the function will not throw any AssertionError to the output.

// Returns nothing

Example

In the example below, we are passing two identical strings to action and the expected parameters of the Node.js assert.equal() function.

const assert = require('assert');
var txt1 = 'Tutorialspoint';
var txt2 = 'Tutorialspoint';
assert.equal(txt1, txt2, 'Both messages are same');

Output

When we compile and run the code, the function will not throw AssertionError to the output because both action and expected are the same.

// Returns nothing

Example

In the example below, we are passing two inputs, an integer to the actual parameter and a string to the expected parameter of the Node.js assert.equal()function.

const assert = require('assert');
var num1 = 7;
var txt1 = '7';
assert.equal(num1, txt1, 'Both values are same');

Output

When we compile and run the code, the function will not throw AssertionError to the output. This is true because 7 == '7'.

// Returns nothing

Example

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

const assert = require('assert');
assert.equal(45, 66, 'Both numbers are not identical');

Output

When we compile and run the code, the function will throw AssertionError along with the message to the output. This is false because 45 !== 66.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both numbers are not identical
   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 two inputs, an integer to the actual parameter and a string to the expected parameter of the function.

const assert = require('assert');
assert.equal(45, 'Hello', 'Both values are not identical');

Output

When we compile and run the code, the function will not throw AssertionError to the output. This is false because both the values are distinct 45 !== 'Hello'.

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both values are not identical
   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