crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Node.js Assert Function
Question Posted on 07 May 2024Home >> Tutorial >> Node.js Tutorial >> Node.js Assert Function

Node.js Assert Function

Node.js assert.deepStrictEqual() Function


When we say about the assert module this will provides a set of assertion functions to varifying invariants. And this assert.deepStrictEqual() function will tests for deep equality between the actual and expected parameters. IF the condition is true this will not produce an output else an assertion error is raised.

Below is the syntax for Node.js assert.deepStrictEqual() function


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



Above function will accept the following parameters as mentioned above and described as given below:-

(1)Actual:-This parameter will holds the actual value which need to be evaluated. And this is of any type.

(2)Expected:-This parameter will holds the expected value which is matched against actual value. And this is of any type.

(3)Message:-This parameter will holds the error message of string or error type. And this is and optional parameter.

This function will returns assertion error of object type as the Return value.

Use of Assert module:-

(1)You need to install Assert module and to intsall this package by using below command.

npm install assert


(2)Although installation is an optional step as it is inbuilt Node.js module.
(3)Once you install the assert module, you can check the seert version by below command.

npm version assert



Now we will create one sample code to check deepStrictEqual() function. Below is the code and file name is deepStrictFunction.js.


// Requiring the module
const assert = require('assert').strict;

// Function call
try {
assert.deepStrictEqual({ a: 1 }, { a: '1' });
} catch(error) {
console.log("Error: ", error)
}



Once we run the above file with Node.js command prompt we will use below command


Node deepStrictFunction.js



Once we run above command we will get the error as given below


Node.js Assert

Now we will take one more example where we have write the correct code with no error. And we use deepStrictEqual() function and name this file is deepFunction.js and code is given below.


// Requiring the module
const assert = require('assert').strict;

// Function call
try {
assert.deepStrictEqual({ a: '3' }, { a: '3' });
console.log("No Error Occurred")
} catch(error) {
console.log("Error: ", error)
}



Once we run the above in Node.js command prompt we will get the below output.


Node.js Assert function
0
0




.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

Parts of Node.js

Install Node.js Linux

Node.js First Example

Node.js Globals

Node.js DNS






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap