Computer >> Máy Tính >  >> Lập trình >> Javascript

Hàm khẳng định.rejects () trong Node.js

Mô-đun khẳng định cung cấp một loạt các chức năng khác nhau được sử dụng để xác nhận chức năng. Hàm khẳng định.rejects sẽ đợi hàm không đồng bộ 'asyncfn' được truyền vào. Nếu asyncfn là một hàm, nó sẽ ngay lập tức gọi hàm này và sẽ đợi hoàn thành lời hứa trả về của nó. Sau đó, nó sẽ kiểm tra lời hứa đó để bị từ chối.

Cú pháp

assert.rejects(asyncfn, [error], [message])

Tham số

Các thông số trên được mô tả như bên dưới -

  • giá trị - Đây là một hàm không đồng bộ sẽ đưa ra các lỗi một cách đồng bộ.

  • lỗi - Tham số này có thể giữ chức năng Class, Regular Expression, Validation hoặc một đối tượng mà mỗi thuộc tính sẽ được kiểm tra. (Tham số tùy chọn)

  • tin nhắn - Đây là một tham số tùy chọn. Đây là thông báo do người dùng xác định được in khi chức năng được thực thi.

Cài đặt Mô-đun Assert

npm install assert

Mô-đun khẳng định là một mô-đun Node.js có sẵn, vì vậy bạn cũng có thể bỏ qua bước này. Bạn có thể kiểm tra phiên bản xác nhận bằng cách sử dụng lệnh sau để tải mô-đun xác nhận mới nhất.

npm version assert

Nhập mô-đun trong chức năng của bạn

const assert = require("assert").strict;

Ví dụ

Tạo một tệp với tên - khẳng địnhRejects.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, hãy sử dụng lệnh dưới đây để chạy mã này.

node assertRejects.js

khẳng địnhRejects.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ụ

Hãy xem thêm một ví dụ.

// 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