ভূমিকা
assert.ok ফাংশন ভ্যালুটি true কিনা তা টেস্ট করে। ভ্যালু true না হলে অ্যাসারশন এরর থ্রো করে।
সিনট্যাক্স
assert.ok(value, [message])
প্যারামিটার
value — ইনপুট ভ্যালু যা assert.ok() চেক করবে।
message — ইউজার-ডিফাইনড মেসেজ। অপশনাল।
Assert মডিউল ইনস্টল
npm install assert
বিল্ট-ইন মডিউল। npm version assert দিয়ে ভার্শন চেক করুন।
মডিউল ইমপোর্ট
const assert = require("assert").strict;উদাহরণ ১
assertOK.js ফাইল তৈরি করে node assertOk.js রান করুন।
// Importing the module const assert = require('assert').strict; try { //Checking the type of value assert.ok(typeof 21 === 'number'); console.log("NO ERROR!") } catch(error) { console.log("Error: ", error) }আউটপুট
C:\home\node>> node assertOk.js NO ERROR!
উদাহরণ ২
// Importing the module const assert = require('assert').strict; try { assert.ok(typeof 21 === 'string'); console.log("NO ERROR!") } catch(error) { console.log("Error: ", error) }আউটপুট
C:\home\node>> node assertOk.js Error: { AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(typeof 21 === 'string') at Object. (/home/node/test/assert.js:6:9) ... }