Mô tả
Module assert cung cấp nhiều chức năng khác nhau dùng cho khẳng định hàm. Hàm assert.rejects sẽ chờ promise của hàm async 'asyncfn' được truyền vào. Nếu asyncfn là một hàm, nó sẽ gọi ngay hàm này và chờ promise trả về hoàn thành. Sau đó nó sẽ kiểm tra promise đó bị reject.
Cú pháp
assert.rejects(asyncfn, [error], [message])
Tham số
value – Đây là một async function sẽ ném lỗi đồng bộ.
error – Tham số này có thể giữ Class, Regular Expression, Validation function hoặc một object mà mỗi property sẽ được test. (Tham số tùy chọn)
message – Tham số tùy chọn. Thông báo do người dùng định nghĩa được in khi hàm thực thi.
Cài đặt module Assert
npm install assert
Module assert là module tích hợp sẵn của Node.js, nên bạn có thể bỏ qua bước này. Bạn có thể kiểm tra phiên bản assert bằng lệnh sau để lấy module assert mới nhất.
npm version assert
Import module vào hàm
const assert = require("assert").strict;Ví dụ
Tạo file tên assertRejects.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:
node assertRejects.js
assertRejects.js
/// Importing the module const assert = require('assert').strict; (async () => { assert.strictEqual(21,20); await assert.rejects(async () => { throw new TypeError('Value passed is Incorrect !'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Incorrect value'); return true; }).then(() => { console.log("This is a reject demp"); }); })();Đầu ra
C:\home\node>> node assertRejects.js (node:259525) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B: + expected - actual - 21 + 20 at /home/node/test/assert.js:5:9 at Object. (/home/node/test/assert.js:18:3) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) (node:259525) 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:259525) [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.
Ví dụ khác
Xem thêm một ví dụ nữa.
// Importing the module const assert = require('assert').strict; (async () => { assert.strictEqual(21,21); await assert.rejects(async () => { throw new TypeError('Value passed is Incorrect !'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Value passed is Incorrect !'); return true; }).then(() => { console.log("This is a reject demo success"); }); })();Đầu ra
C:\home\node>> node assertRejects.js This is a reject demo success