
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 連線
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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
廣告