Related Tags

node.js

# What is the doesNotThrow method of the assert module in Node.js?

Talha Ashar

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The doesNotThrow method of the assert module in Node.js asserts that a provided function does not throw an error. The provided function is executed whenever the doesNotThrow method is invoked.

To use the doesNotThrow method, you will need to install the assert module using the command prompt, as shown below:

npm install assert


After the installation is complete, you will need to import the assert module into the program, as shown below:

const assert = require('assert');


The prototype of the doesNotThrow method is:

doesNotThrow(fn[, error][, message])


## Parameters

The doesNotThrow method takes the following parameters:

• fn: The function to be executed whenever the doesNotThrow method is invoked.

• error: An optional parameter that may be either a Regular Expression or a function.

• message: An optional parameter that represents an error message.

## Return value

The doesNotThrow method invokes the function (fn). If an error is thrown, there are two possible outcomes:

1. If the type of the error is the same as that of the error parameter, then the doesNotThrow method throws an AssertionError.
2. If the error parameter was not provided or the error type does not match, the error propagates back to the caller.

## Example

The code below shows how the doesNotThrow method works in Node.js:

const assert = require('assert');console.log("Example 1:")// error types matchtry{  assert.doesNotThrow(    () => {      throw new TypeError("Assertion Error Thrown.")    },    TypeError,    "Error types match."  );}catch(error){  console.log(error.message)}console.log("\nExample 2:")// error types do not matchtry{  assert.doesNotThrow(    () => {      throw new TypeError("Type Error Thrown.")    },    SyntaxError,    "Error types do not match."  );}catch(error){  console.log(error.message)}

## Explanation

The code above uses two examples to demonstrate the behavior of the doesNotThrow method.

In the first call to the doesNotThrow method in line $7$, a TypeError is thrown. Since the error parameter is also a TypeError, an AssertionError is thrown and the provided message parameter is output.

In the call to the doesNotThrow method in line $25$, a TypeError is thrown again, but the error parameter holds SyntaxError instead of TypeError.

Since there is no matching error type, a TypeError is thrown instead of an AssertionError, and the message parameter is not output.

Note: You can view further examples on how to use the doesNotThrow method here.

RELATED TAGS

node.js

CONTRIBUTOR

Talha Ashar