The assert module provides a bunch of different functionalities that are used for function assertion. assert.strictEqual is used to check the equality between two objects or parameters. It will throw an assertion error if both the objects are not equal.
Syntax
assert.strictEqual(actual, expected[, message])
Parameters
The above parameters are described as below −
actual – This is the actual value that will be evaluated against the expected parameters.
expected – This is the expected parameter value which is matched against the actual value.
message – This parameter holds the string message value to be printed if the actual and expected parameters do not match. It is an optional field.
Installing the Assert Module
npm install assert
The assert module is an inbuilt Node.js module, so you can skip this step as well. You can check the assert version using the following command to get the latest assert module.
npm version assert
Importing the module in your function
const assert = require("assert");
Example
Create a file with name – assertStrictEqual.js and copy the below code snippet. After creating file, use the following command to run this code.
node assertStrictEqual.js
assertStrictEqual.js
// Importing the module const assert = require('assert').strict; var a = "TutorialsPoint"; var b = "TutorialsPoint"; try { // Calling the equal function to check equality assert.strictEqual(a, b); console.log("Actual and expected parameters are equal") } catch(error) { console.log("Error: ", error) }
Output
C:\home\node>> node assertStrictEqual.js Actual and expected parameters are equal
We can see in the above example that both the values are strictly equal.
Example
Let's take a look at one more example.
// Importing the module const assert = require('assert').strict; var a = "Welcome to TutorialsPoint"; var b = "TutorialsPoint"; try { // Calling the equal function to check equality assert.strictEqual(a, b); console.log("Actual and expected parameters are equal") } catch(error) { console.log("Error: ", error) }
Output
C:\home\node>> node assertStrictEqual.js Error: { AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B: + expected - actual - 'Welcome to TutorialsPoint' + 'TutorialsPoint' at Object. (/home/node/test/assert.js:9:9) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) generatedMessage: true, name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', actual: 'Welcome to TutorialsPoint', expected: 'TutorialsPoint', operator: 'strictEqual' }
In the above example, we can see that both the values are not equal. Therefore an assertion error was thrown by the assert strictEqual() method.