• 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
廣告