Open In App
Related Articles

Node.js Assert Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Assert module in Node.js provides a bunch of facilities that are useful for the assertion of the function. The assert module provides a set of assertion functions for verifying invariants. If the condition is true it will output nothing else an assertion error is given by the console.

Example:

Javascript

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

                    

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’ }

The Complete List of NodeJS Assert are listed below:an

NodeJS Assert

Description

Node.js assert() FunctionIf the value is not the truth, then a AssertionError is thrown with a message property set equal to the value of the message parameter.
Node.js assert.deepStrictEqual() FunctionThe assert.deepStrictEqual() function tests for deep equality between the actual and expected parameters.
Node.js assert.doesNotThrow() FunctionThe assert.doesNotThrow() function asserts that the function fn does not throw an error.
Node.js assert.equal() FunctionThe assert.equal() function tests for equality between the actual and the expected parameters.
Node.js assert.fail() FunctionThe assert.fail() function throws an AssertionError with the provided error message or with a default error message.
Node.js assert.ifError() FunctionThe assert.ifError() function throws a value if the value is not defined or null.
Node.js assert.match() FunctionThe assert.match() function expects the string input to match the regular expression.
Node.js assert.notDeepEqual() FunctionThe assert.notDeepEqual() function tests deep strict inequality between the actual and the expected parameters.
Node.js assert.notDeepStrictEqual() Function The assert.notDeepStrictEqual() function tests for deep strict inequality.
Node.js assert.notEqual() FunctionThe assert.notEqual() function tests strict inequality between the actual and the expected parameters.
Node.js assert.ok() FunctionThe assert module provides a set of assertion functions for verifying invariants.
Node.js assert.rejects() FunctionThe assert.rejects() function awaits the asyncFn promise 
Node.js assert.strictEqual() FunctionThe assert.strictEqual() function tests strict equality between the actual and expected parameters.

Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads