• Node.js Video Tutorials

Node.js - assert.rejects() 函数



assert 模块提供了一组断言函数,用于验证不变式。assert.rejects() 函数是 Node.js assert 模块的内置函数。

Node.js assert.rejects() 函数等待传入的 Promise 或传入的异步函数返回的 Promise,并检查该函数是否被拒绝。

语法

以下是 Node.js assert.rejects() 函数 的语法:

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

参数

此函数接受三个参数。下面描述了这些参数。

  • asyncFn − (必需)此参数包含一个异步函数,该函数将同步抛出错误。

  • error − (可选)此参数可以包含类、正则表达式、验证函数或一个对象,其中每个属性都将进行测试。

  • message − (可选)可以将字符串作为输入传递给此参数。

返回值

如果异步函数抛出错误或未能返回 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'
   }
);

输出

当我们编译并运行代码时,该函数将不会返回一个带有错误对象的拒绝的 Promise。

// 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;
      }
   );
})();

输出

当我们编译并运行代码时,该函数返回一个带有错误对象的拒绝的 Promise,因为异步函数正在抛出错误。因为 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.

示例

在以下示例中,我们传递了一个 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;
      }
   );
})();

输出

当我们编译并运行代码时,该函数不会返回一个带有错误对象的拒绝的 Promise,因为异步函数没有抛出错误。因为 46 == 46。

// Returns nothing
nodejs_assert_module.htm
广告

© . All rights reserved.