• Node.js Video Tutorials

Node.js - assert.deepStrictEqual() Function



The Node.js assert.deepStrictEqual() function will test deep strict equality on both of its parameters actual and expected. It's better to prefer to assert.deepStrictEqual() instead of assert.deepEqual() since this method has an ample amount of comparison differences.

Syntax

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

assert.deepStrictEqual(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.

Example

In the example below, we are creating two objects with the same properties and we are changing the type of a particular property (As an integer in the first object & as a string in the second object). Then we passed both the objects as actual and expected to the Node.js assert.deepStrictEqual() function and also passed a text to the message parameter.

const assert = require('assert');
var object1 = {
   Name: 'Roy',
   Age: 34,
   Nickname: 'Godsway'
};

var object2 = {
   Name: 'Roy',
   Age: '34',
   Nickname: 'Godsway'
}
assert.deepStrictEqual(object1, object2, 'Both the objects are not identical');

Output

When we compile and run the code, the function will throw AssertionError along with the message to the output as both actual and expected are not identical. This failed because 34 !== '34'.

assert.js:79
   throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Both the objects are not identical
   at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:15: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 creating two objects with the same properties.

const assert = require('assert');
var object1 = {
   Name: 'Roy',
   Age: 34,
   Nickname: 'Godsway'
};

var object2 = {
   Name: 'Roy',
   Age: 34,
   Nickname: 'Godsway'
}
assert.deepStrictEqual(object1, object2, 'Both the objects are identical');

Output

When we compile and run the code, the function doesn't throw AssertionError to the output as both actual and expected are identical. This is true because 34 == 34.

// Returns nothing

Example

In the example below, we are passing a string and an object to the function to compare.

const assert = require('assert');

assert.deepStrictEqual(new String('Tutorialspoint'), Object('Tutorialspoint'));

Output

When we compile and run the code, the function doesn't throw AssertionError to the output as both actual and expected are identical. This is true because the object and the string are identical when unwrapped.

// Returns nothing

Example

In the example below, we are passing 0 and -0 to the function to compare.

const assert = require('assert');

var zero = 0;
var negzero = -0
assert.deepStrictEqual(zero, negzero, 'Both are not same');

Output

When we compile and run the code, the function will throw AssertionError along with the message to the output as both actual and expected are not identical. This is true because 0 !== -0.

assert.js:79
   throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Both are not same
   at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:5: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 -0 and -0 to the function to compare.

const assert = require('assert');

var neggzero = -0;
var negzero = -0
assert.deepStrictEqual(neggzero, negzero, 'Both are same');

Output

When we compile and run the code, the function doesn't throw AssertionError to the output as both actual and expected are identical. This is true because -0 == -0.

// Returns nothing

Example

In the example below, we passed 5 and 6 to the function to compare. But we haven't passed any text into the message parameter. So, the function will assign a default error message.

const assert = require('assert');

assert.deepStrictEqual(5, 6);

Output

When we compile and run the code, the function will throw AssertionError along with the default error message assigned by the function to the output as both actual and expected are not identical.

assert.js:79
   throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B:
+ expected - actual

- 5
+ 6
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/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