Open In App

Node.js assert.throws() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The assert module provides a set of assertion functions for verifying invariants. The assert.throws() is used when the code throws an exception based on specific circumstances, to catch the error object for testing and comparison.

Syntax:

assert.throws(fn[, error][, message])

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

  • fn: This parameter is a function that does not throw an error.
  • error: This parameter is a regular expression or function. It is the specified error. It is an optional parameter.
  • 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

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

Example 1:

Javascript




// Requiring the module
const assert = require('assert');
 
const invalidNum = function () {
    throw console.log("Invalid Number")
};
 
const someFunc = function (a) {
    if (a > 10) {
        invalidNum();
    }
    else {
        console.log("Valid number");
    }
};
 
assert.throws(function () {
    someFunc(5);
});


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Valid number
assert.js:105
 throw new AssertionError(obj);
 ^
AssertionError [ERR_ASSERTION]: Missing expected exception.
   at Object.<anonymous> (F:\Blogs\GFG\Assert.throw\index.js:18:8)
   at Module._compile (internal/modules/cjs/loader.js:1063:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
   at Module.load (internal/modules/cjs/loader.js:928:32)
   at Function.Module._load (internal/modules/cjs/loader.js:769:14)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
   at internal/main/run_main_module.js:17:47 {
 generatedMessage: false,
 code: 'ERR_ASSERTION',
 actual: undefined,
 expected: undefined,
 operator: 'throws'
}

Example 2:

Javascript




// Requiring the module
const assert = require('assert');
 
const invalidNum = function () {
    throw console.log("Invalid Number")
};
 
const someFunc = function (a) {
    if (a > 10) {
        invalidNum();
    }
    else {
        console.log("Valid number");
    }
};
 
assert.throws(function () {
    someFunc(12);
});


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Invalid Number

Reference: https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message



Last Updated : 31 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads