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.doesNotThrow sẽ khẳng định rằng hàm không ném ra lỗi. Nó sẽ gọi ngay 'fn' khi assert.doesNotThrow được gọi.
Cú pháp
assert.doesNotThrow(fn, [error], [message])
Tham số
fn – Hàm này sẽ được gọi khi assert.doesNotThrow thực thi. Nó sẽ gọi hàm này thay vì ném lỗi.
error – Tham số chứa biểu thức chính quy cho lỗi xác định. Là 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 doesNotThrow.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:
node doesNotThrow.js
doesNotThrow.js
// Importing the module const assert = require('assert').strict; try { // Calling the doesNotThrow method to stop throwing error assert.doesNotThrow(() => { throw new TypeError('Wrong value received. Please check !'); }); } catch(error) { console.log("Error:", error); }Đầu ra
C:\home\node>> node doesNotThrow.js Error: { AssertionError [ERR_ASSERTION]: Got unwanted exception. Actual message: "Wrong value received. Please check !" at Object. (/home/node/test/assert.js:6:9) 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) generatedMessage: false, name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', actual: TypeError: Wrong value received. Please check ! at assert.doesNotThrow (/home/node/test/assert.js:8:9) at getActual (assert.js:567:5) at Function.doesNotThrow (assert.js:687:32) at Object. (/home/node/test/assert.js:6:9) 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), expected: undefined, operator: 'doesNotThrow' }Ví dụ khác
Xem thêm một ví dụ nữa.
// Importing the module const assert = require('assert').strict; try { assert.doesNotThrow(() => { throw new TypeError('Value passed is wrong !!'); }, /abcd/, 'Whoops'); } catch(error) { console.log("Error:", error); }Đầu ra
C:\home\node>> node doesNotThrow.js Error: TypeError: Value passed is wrong !! at assert.doesNotThrow (/home/node/test/assert.js:7:9) at getActual (assert.js:567:5) at Function.doesNotThrow (assert.js:687:32) at Object. (/home/node/test/assert.js:5:9) 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)