Open In App

Node.js assert.strictEqual() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The assert module provides a set of assertion functions for verifying invariants. The assert.strictEqual() function tests strict 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.strictEqual(actual, expected[, message])

Parameters: This function accepts the following parameters as mentioned above and described below:

  • actual: This parameter holds the actual value that needs to be evaluated. It is of any type.
  • expected: This parameter holds the expected value which is matched against the 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 an assertion error of object type. 

Installation of assert module:

npm install assert

Note: Installation is an optional step as it is an inbuilt Node.js module.

  • After installing the assert module, you can check your assert version in the command prompt using the command.
npm version assert

Project Structure

 

  • After that, you can just create a folder and add a file, for example, index.js as shown below.

Example 1: 

Filename: index.js 

javascript




// Requiring the module
const assert = require('assert').strict;
 
let a = "GeeksforGeeks";
let b = "Geeks4Geek";
 
// Function call
try {
    assert.strictEqual(a, b);
} catch (error) {
    console.log("Error: ", error)
}


Steps to run the program:

  • Run the index.js file using the below command:
node index.js

Output:

Error: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual – expected + ‘GeeksforGeeks’ – ‘Geeks4Geek’ ^ at Object. (C:\Users\Lenovo\Downloads\index.js:4: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: ‘GeeksforGeeks’, expected: ‘Geeks4Geek’, operator: ‘strictEqual’ }

Example 2: 

Filename: index.js 

javascript




// Requiring the module
const assert = require('assert').strict;
 
// Function call
try {
    assert.strictEqual(5, 5);
    console.log("No Error Occurred");
} catch (error) {
    console.log("Error: ", error)
}
 
// Function call
try {
    assert.strictEqual(2, 5, new
        TypeError('Inputs are not same'));
} catch (error) {
    console.log("Error Occurred: ", error)
}


Steps to run the program:

  • Run the index.js file using the below command:
node index.js

Output:

No Error Occurred Error Occurred: TypeError: Inputs are not same at Object. (C:\Users\Lenovo\Downloads\index.js:14:30) 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

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_strictequal_actual_expected_message



Last Updated : 06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads