Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js assert.throws() Function

  • Difficulty Level : Medium
  • Last Updated : 06 Oct, 2021

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 a 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 which 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 assertion error of object type.



Installation of assert module:

npm install assert

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

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

npm version assert

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

Example 1:

index.js




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

Steps to run the program:

Run index.js file using 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:

index.js




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

Steps to run the program:

Run index.js file using below command:

node index.js

Output:

Invalid Number

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




My Personal Notes arrow_drop_up

Start Your Coding Journey Now!