The Wayback Machine - https://web.archive.org/web/20240620060744/https://www.geeksforgeeks.org/node-js-assert-throws-function/
Open In App

Node.js assert.throws() Function

Last Updated : 31 Mar, 2023
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



Next Article

Similar Reads

Node.js assert.deepStrictEqual() Function
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:
2 min read
Node.js assert.notStrictEqual() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.notStrictEqual() function tests strict inequality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.notStrictEqual(actual, expected[, message]) Parameters
3 min read
Node.js assert.ok() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.ok() function tests if the value is true or not. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.ok(value[, message]) Parameters: This function accepts two parameters as mentioned above and described
2 min read
Node.js assert.doesNotMatch() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.doesNotMatch() function expects the string input not to match the regular expression. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.doesNotMatch(string, regexp[, message]) Parameters: This function
2 min read
Node.js assert.fail() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.fail() function throws an AssertionError with the provided the error message or with a default error message. Syntax: assert.fail([message]) Parameters: This function accepts following parameters as mentioned above and described below: message This paramete
2 min read
Node.js assert.equal() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.equal() function tests for equality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.equal(actual, expected[, message]) Parameters: This function accepts
2 min read
Node.js assert.deepEqual() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.deepEqual() function tests deep equality between the actual and the expected parameters. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.deepEqual(actual, expected[, message]) Parameters: This functio
2 min read
Node.js assert.match() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.match() function expects the string input to match the regular expression. If the condition is true it will not produce an output else an assertion error is raised. Syntax: assert.match(string, regexp[, message]) Parameters: This function accepts the follow
2 min read
Node.js assert.rejects() Function
The assert module provides a set of assertion functions for verifying invariants. The assert.rejects() function awaits the asyncFn promise or if the asyncFn is a function then it immediately calls the function and awaits the returned promise to complete and after that it will then check that the promise is rejected. Syntax: assert.rejects(asyncFn[,
3 min read
Node.js assert.strictEqual() Function
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 funct
3 min read
three90RightbarBannerImg