Node.js assert.rejects() Function Last Updated : 07 Aug, 2020 Suggest changes Share Like Article Like Report 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[, error][, message]) Parameters: This function accepts following parameters as mentioned above and described below: asyncFn: This parameter is async function which throws an error synchronously. error: This parameter can be of type Class or a Regular expression or a validation function or an object where each property will be tested for. It is an optional parameter. message: This parameter will be the message provided by the AssertionError if the asyncFn fails to reject. It is an optional parameter. Return Value: This function returns assertion error of object type. Installation of assert module: You can visit the link to Install assert module. You can install this package by using this command. npm install assert Note: Installation is an optional step as it is inbuilt Node.js module. After installing the assert module, you can check your assert version in 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: Filename: index.js javascript // Requiring the module const assert = require('assert').strict; // Function call (async () => { assert.strictEqual(1,2) await assert.rejects( async () => { throw new TypeError('Wrong value'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Wrong value'); return true; } ).then(() => { console.log("Reject Demo") }); })(); Steps to run the program: The project structure will look like this: Run index.js file using below command: node index.js Output: (node:12704) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 2 at C:\Users\Lenovo\Downloads\Geeksforgeeks Internship\index.js:25:12 at Object. (C:\Users\Lenovo\Downloads\Geeksforgeeks Internship\NEW\Assert Function \index.js:38:3) 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 (node:12704) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) Example 2: Filename: index.js javascript // Requiring the module const assert = require('assert').strict; // Function call (async () => { assert.strictEqual(1,1) await assert.rejects( async () => { throw new TypeError('Wrong value'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Wrong value'); return true; } ).then(() => { console.log("Reject Demo Works Successfully") }); })(); Steps to run the program: The project structure will look like this: Run index.js file using below command: node index.js Output: Reject Demo Works Successfully Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_rejects_asyncfn_error_message Create Quiz G gouravhammad Follow 0 Article Tags : Web Technologies Node.js Node.js-Methods NodeJS-assert Explore Node.js Tutorial 3 min read Introduction & Installation NodeJS Introduction 3 min read Node.js Roadmap: A Complete Guide 6 min read How to Install Node.js on Linux 6 min read How to Install Node.js on Windows 5 min read How to Install NodeJS on MacOS 6 min read Node.js vs Browser - Top Differences That Every Developer Should Know 6 min read NodeJS REPL (READ, EVAL, PRINT, LOOP) 4 min read Explain V8 engine in Node.js 7 min read Node.js Web Application Architecture 3 min read NodeJS Event Loop 5 min read Node.js Modules , Buffer & StreamsNodeJS Modules 5 min read What are Buffers in Node.js ? 4 min read Node.js Streams 4 min read Node.js Asynchronous ProgrammingAsync Await in Node.js 3 min read Promises in NodeJS 7 min read How to Handle Errors in Node.js ? 4 min read Exception Handling in Node.js 3 min read Node.js NPMNodeJS NPM 6 min read Steps to Create and Publish NPM packages 7 min read Introduction to NPM scripts 2 min read Node.js package.json 4 min read What is package-lock.json ? 3 min read Node.js Deployments & CommunicationNode Debugging 2 min read How to Perform Testing in Node.js ? 2 min read Unit Testing of Node.js Application 5 min read NODE_ENV Variables and How to Use Them ? 2 min read Difference Between Development and Production in Node.js 3 min read Best Security Practices in Node.js 4 min read Deploying Node.js Applications 5 min read How to Build a Microservices Architecture with NodeJS 3 min read Node.js with WebAssembly 3 min read Resources & ToolsNode.js Web Server 6 min read Node Exercises, Practice Questions and Solutions 4 min read Node.js Projects 9 min read NodeJS Interview Questions and Answers 15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like