assert 模块提供了一组用于验证不变量的 assert 函数。assert.rejects() 函数是 Node.js 的 assert 模块的内置函数。
Node.js assert.rejects() 函数等待传入的 promise 或传入的异步函数返回的 promise,并检查函数是否被拒绝。
语法
以下是 assert.rejects() 函数的语法Node.js -
assert.rejects(asyncFn [, error][, message]);
参数
此函数接受三个参数。下面将对此进行描述。
- asyncFn − (必需) 此参数包含一个异步函数,该函数将同步抛出错误。
- error − (可选)此参数可以保存类、正则表达式、验证函数或将测试每个属性的对象。
- message − (可选)字符串可以作为输入传递到此参数中。
返回值
如果 async 函数抛出错误或无法返回 promise,则 assert.rejects() 函数将返回一个带有错误对象的被拒绝 promise。
例在以下示例中,我们将传递一个 asyncFn 并检查 Node.js assert.rejects() 函数的每个属性。
const assert = require('assert');
assert.rejects(
async () => {
throw new TypeError('This is an Error');
},
{
name: 'TypeError',
message: 'This is an Error'
}
);
输出
当我们编译并运行代码时,该函数不会返回带有错误对象的被拒绝承诺。
// Returns nothing
例
在以下示例中,我们将传递 asyncFn 并检查函数的每个属性。
const assert = require('assert').strict;
(async () => {
assert.strictEqual(45, 46)
await assert.rejects(
async () => {
throw new TypeError('This is an Error!');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'This is an Error!');
return true;
}
);
})();
输出
当我们编译并运行代码时,该函数返回一个被拒绝的承诺和一个错误对象,因为异步函数抛出了一个错误。因为 45 !== 46.
(node:7151) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Input A
expected to strictly equal input B:
+ expected - actual- 45
+ 46
at /home/cg/root/639c3b5c17b34/main.js:4:10
at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:15:3)
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)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
(node:7151) 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: 1)
(node:7151) [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.
expected to strictly equal input B:
+ expected - actual- 45
+ 46
at /home/cg/root/639c3b5c17b34/main.js:4:10
at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:15:3)
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)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
(node:7151) 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: 1)
(node:7151) [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.
例
在以下示例中,我们将传递一个 asyncFn,在 error 参数中,我们将检查函数的每个属性。
const assert = require('assert').strict;
(async () => {
assert.strictEqual(46, 46)
await assert.rejects(
async () => {
throw new TypeError('This is an Error!');
},
(error) => {
assert.strictEqual(error.name, 'TypeError');
assert.strictEqual(error.message, 'This is an Error!');
return true;
}
);
})();
输出
当我们编译并运行代码时,该函数不会返回带有错误对象的被拒绝的承诺,因为异步函数不会抛出错误。因为 46 == 46。
// Returns nothing