Node.js - assert.doesNotReject() 函数



Node.js assert.doesNotReject() 函数是 Node.js 的 assert 模块的内置函数。它用于检查给定的承诺是否未被拒绝。

assert.doesNotReject() 函数中,如果提供的参数是 promise,它将等待 promise。如果提供的参数是一个函数,则立即调用它并等待返回承诺完成。此函数将检查提供的承诺是否被拒绝。此函数的行为与 assert.doesNotThrow() 相同。

assert.doesNotReject() 使用起来没有用,因为捕获错误并再次拒绝错误只有一点好处。相反,如果我们向不应被拒绝的特定代码路径添加适当的注释,并尽可能保持错误消息的表现力,那就更好了。

语法

以下是 assert.doesNotReject() 函数Node.js的语法 -


 assert.doesNotReject(asyncFn[, error][, message]);

参数

此函数接受三个参数。下面将对此进行描述。

  • asyncFn − (必需) 此参数包含一个异步函数,该函数将同步抛出错误。
  • error − (可选)此参数可以保存类、正则表达式、验证函数或将测试每个属性的对象。
  • message − (可选)此参数可以保存类、正则表达式、验证函数或将测试每个属性的对象。

返回值

函数 assert.doesNotReject() 会将被拒绝的承诺返回到输出中。

以下是 assert.doesNotReject() 函数的用法Node.js。


const assert = require('assert');
(async () => {
	 	await assert.doesNotReject(
	 	 	 async () => {
	 	 	 	 	throw new TypeError('Wrong value');
	 	 	 },
	 	 	 	 	TypeError
	 	);
}
)();

输出

以下是上述代码的输出 -

(node:40094) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Got unwanted rejection.
Actual message: "Wrong value"
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
(node:40094) 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(). (rejection id: 2)
(node:40094) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.

以下是 Node.js assert.doesNotReject() 函数在另一种情况下的用法。


const assert = require('assert');
(async () => {
	 	await assert.doesNotReject(
	 	 	 async () => {
	 	 	 	 	throw new TypeError('Error occured!!!');
	 	 	 },
	 	 	 	 	SyntaxError
	 	);
}
)();

输出

以下是上述代码的输出 -

(node:43403) UnhandledPromiseRejectionWarning: TypeError: Error occured!!!
at assert.doesNotReject (/home/cg/root/639c2bf348ea8/main.js:6:23)
at waitForActual (assert.js:518:21)
at Function.doesNotReject (assert.js:620:39)at /home/cg/root/639c2bf348ea8/main.js:4:22
at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:11:2)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
(node:43403) 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(). (rejection id: 2)
(node:43403) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.