انتقل إلى المحتوى

مرجع واجهة ⁦node:assert⁩

مساعدات تأكيد متوافقة مع ⁦Node⁩

في هذه الصفحة
Open in ChatGPTOpen in Claude

تصدير الوحدة قابل للاستدعاء، وهو الدالة نفسها ⁦assert.ok⁩:

import assert from "node:assert";

assert(1 + 1 === 2);
assert.strictEqual(1 + 1, 2);
assert.deepStrictEqual({ values: [1, 2] }, { values: [1, 2] });
await assert.rejects(async () => {
  throw new TypeError("nope");
}, TypeError);

العمليات

تشمل التأكيدات المدعومة:

  • ⁦ok⁩ و⁦fail⁩ و⁦ifError⁩
  • ⁦equal⁩ و⁦notEqual⁩ و⁦strictEqual⁩ و⁦notStrictEqual⁩
  • ⁦deepEqual⁩ و⁦notDeepEqual⁩ و⁦deepStrictEqual⁩ و⁦notDeepStrictEqual⁩ و ⁦partialDeepStrictEqual⁩
  • ⁦throws⁩ و⁦doesNotThrow⁩ و⁦rejects⁩ و⁦doesNotReject⁩
  • ⁦match⁩ و⁦doesNotMatch⁩

‏⁦assert.strict⁩ متغيّر قابل للاستدعاء بالوضع الصارم. وتدعم المقارنة العميقة الدورات والكائنات المدمجة الشائعة مثل المصفوفات والمصفوفات المنمّطة والتواريخ والتعابير النمطية والخرائط والمجموعات.

ترمي التأكيدات الفاشلة ⁦assert.AssertionError⁩ مع ضبط ⁦code⁩ على ⁦ERR_ASSERTION⁩ وحقول تشمل ⁦actual⁩ و⁦expected⁩ و⁦operator⁩. ويُبلغ ⁦Elide⁩ عن أسماء العمليات مثل ⁦strictEqual⁩ في ⁦operator⁩، بدلًا من رموز مثل ⁦===⁩.

راجع توثيق ⁦assert⁩ في ⁦Node.js⁩ لصيغ الاستدعاء القياسية.

Reference

Status: Partial · 7 of 13 suite tests pass (53.8%) · since 1.1.0

Exports

ExportKindStatusNotes
ok()functionFullTests if value is truthy
equal()functionFullTests shallow equality between actual and expected using ==
notEqual()functionFullTests shallow inequality between actual and expected using !=
strictEqual()functionFullTests strict equality between actual and expected using ===
notStrictEqual()functionFullTests strict inequality between actual and expected using !==
deepEqual()functionFullTests for deep equality between actual and expected using abstract equality (==) for leaf comparisons
deepStrictEqual()functionFullTests for deep strict equality between actual and expected using strict equality (===) for leaf…
notDeepEqual()functionFullTests that actual is NOT deeply equal to expected (abstract equality)
notDeepStrictEqual()functionFullTests that actual is NOT deeply strictly equal to expected
throws()functionFullExpects the function fn to throw an error
doesNotThrow()functionFullAsserts that the function fn does not throw an error
rejects()functionFullAwaits the asyncFn promise (or calls it if it’s a function) and expects it to reject
doesNotReject()functionFullAwaits the asyncFn promise (or calls it if it’s a function) and expects it NOT to reject
fail()functionFullThrows an AssertionError unconditionally
ifError()functionFullIf value is truthy (not null/undefined/false), throws it
match()functionFullExpects string to match the regular expression regexp
doesNotMatch()functionFullExpects string NOT to match the regular expression regexp

Not implemented

Assert, assert/strict.Assert.

Tests: node-api node:assert: 7 pass, 6 fail, 0 error, 1 skipped. Failing: test-assert-async.js, test-assert-class-destructuring.js, test-assert-class.js, test-assert-deep.js, test-assert-esm-cjs-message-verify.js, test-assert.js.

هل كانت هذه الصفحة مفيدة؟