Node.js assert.deepStrictEqual() Function - GeeksforGeeks

07 Aug, 2020
The assert module provides a set of assertion functions for verifying invariants. The assert.deepStrictEqual() function tests for deep equality between the actual and expected parameters. If the condition is true it will not produce an output else an assertion error is raised. 
Syntax: 
assert.deepStrictEqual(actual, expected[, message])
Parameters: This function accepts the following parameters as mentioned above and described below: 
actual: This parameter holds the actual value that need to be evaluated. It is of any type.
expected: This parameter holds the expected value which is matched against actual value. It is of any type.
message: This parameter holds the error message of string or error type. It is an optional parameter.
Return Value: This function returns assertion error of object type. 
Installation of assert module: 
You can visit the link to Install assert module. You can install this package by using this command. npm install assertNote: Installation is an optional step as it is inbuilt Node.js module. After installing the assert module, you can check your assert version in command prompt using the command. npm version assertAfter that, you can just create a folder and add a file for example, index.js as shown below.Example 1: Filename: index.js  javascriptjavascript// Requiring the moduleconst assert = require('assert').strict;     // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });} catch(error) {    console.log("Error: ", error)}Steps to run the program:  The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  Error:  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:+ actual – expected {+   a: 1–   a: ‘1’ }   at Object. (C:\Users\Lenovo\Downloads\index.js:14:12)   at Module._compile (internal/modules/cjs/loader.js:1138:30)   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)   at Module.load (internal/modules/cjs/loader.js:986:32)   at Function.Module._load (internal/modules/cjs/loader.js:879:14)   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)   at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘deepStrictEqual’}Example 2: Filename: index.js  javascriptjavascript// Requiring the moduleconst assert = require('assert').strict;       // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });    console.log("No Error Occured")} catch(error) {    console.log("Error: ", error)}Steps to run the program:  The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  No Error OccuredReference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_deepstrictequal_actual_expected_message My Personal Notes
arrow_drop_upSave
You can visit the link to Install assert module. You can install this package by using this command. npm install assert
npm install assert
Note: Installation is an optional step as it is inbuilt Node.js module. 
After installing the assert module, you can check your assert version in command prompt using the command. npm version assert
npm version assert
After that, you can just create a folder and add a file for example, index.js as shown below.Example 1: Filename: index.js  javascriptjavascript// Requiring the moduleconst assert = require('assert').strict;     // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });} catch(error) {    console.log("Error: ", error)}Steps to run the program:  The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  Error:  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:+ actual – expected {+   a: 1–   a: ‘1’ }   at Object. (C:\Users\Lenovo\Downloads\index.js:14:12)   at Module._compile (internal/modules/cjs/loader.js:1138:30)   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)   at Module.load (internal/modules/cjs/loader.js:986:32)   at Function.Module._load (internal/modules/cjs/loader.js:879:14)   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)   at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘deepStrictEqual’}Example 2: Filename: index.js  javascriptjavascript// Requiring the moduleconst assert = require('assert').strict;       // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });    console.log("No Error Occured")} catch(error) {    console.log("Error: ", error)}Steps to run the program:  The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  No Error OccuredReference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_deepstrictequal_actual_expected_message My Personal Notes
arrow_drop_upSave
Example 1: Filename: index.js  
javascript
// Requiring the moduleconst assert = require('assert').strict;     // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });} catch(error) {    console.log("Error: ", error)}
Steps to run the program:  
The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  Error:  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:+ actual – expected {+   a: 1–   a: ‘1’ }   at Object. (C:\Users\Lenovo\Downloads\index.js:14:12)   at Module._compile (internal/modules/cjs/loader.js:1138:30)   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)   at Module.load (internal/modules/cjs/loader.js:986:32)   at Function.Module._load (internal/modules/cjs/loader.js:879:14)   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)   at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘deepStrictEqual’}
The project structure will look like this:  
Run index.js file using below command:  node index.js
node index.js
Output:  Error:  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:+ actual – expected {+   a: 1–   a: ‘1’ }   at Object. (C:\Users\Lenovo\Downloads\index.js:14:12)   at Module._compile (internal/modules/cjs/loader.js:1138:30)   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)   at Module.load (internal/modules/cjs/loader.js:986:32)   at Function.Module._load (internal/modules/cjs/loader.js:879:14)   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)   at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘deepStrictEqual’}
Error:  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:+ actual – expected
 {+   a: 1–   a: ‘1’ }   at Object. (C:\Users\Lenovo\Downloads\index.js:14:12)   at Module._compile (internal/modules/cjs/loader.js:1138:30)   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)   at Module.load (internal/modules/cjs/loader.js:986:32)   at Function.Module._load (internal/modules/cjs/loader.js:879:14)   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)   at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: [Object], expected: [Object], operator: ‘deepStrictEqual’}
Example 2: Filename: index.js  
javascript
// Requiring the moduleconst assert = require('assert').strict;       // Function calltry {    assert.deepStrictEqual({ a: '5' }, { a: '5' });    console.log("No Error Occured")} catch(error) {    console.log("Error: ", error)}
Steps to run the program:  
The project structure will look like this:  Run index.js file using below command:  node index.jsOutput:  No Error Occured
The project structure will look like this:  
Run index.js file using below command:  node index.js
node index.js
Output:  No Error Occured
No Error Occured
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_deepstrictequal_actual_expected_message 
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript