ভূমিকা
assert.rejects ফাংশন পাস করা অ্যাসিংক ফাংশন 'asyncfn'-এর প্রমিসের জন্য অপেক্ষা করে। যদি asyncfn ফাংশন হয়, তবে এটি অবিলম্বে কল করে এবং এর রিটার্নড প্রমিস কমপ্লিট হওয়ার জন্য অপেক্ষা করে। তারপর প্রমিসটি রিজেক্ট হয়েছে কিনা চেক করে।
সিনট্যাক্স
assert.rejects(asyncfn, [error], [message])
প্যারামিটার
value — একটি অ্যাসিংক ফাংশন যা সিঙ্ক্রোনাসলি এরর থ্রো করবে।
error — এই প্যারামিটার ক্লাস, রেগুলার এক্সপ্রেশন, ভ্যালিডেশন ফাংশন বা একটি অবজেক্ট ধারণ করতে পারে যেখানে প্রতিটি প্রোপার্টি টেস্ট করা হবে। (অপশনাল)
message — ইউজার-ডিফাইনড মেসেজ। অপশনাল।
Assert মডিউল ইনস্টল
npm install assert
বিল্ট-ইন মডিউল। npm version assert দিয়ে ভার্শন চেক করুন।
মডিউল ইমপোর্ট
const assert = require("assert").strict;উদাহরণ ১
assertRejects.js ফাইল তৈরি করে node 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") }); })();আউটপুট
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 ... (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.
উদাহরণ ২
// 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") }); })();আউটপুট
C:\home\node>> node assertRejects.js This is a reject demo success