Експорт модуля можна викликати, і це та сама функція, що й 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іifErrorequal,notEqual,strictEqualіnotStrictEqualdeepEqual,notDeepEqual,deepStrictEqual,notDeepStrictEqualіpartialDeepStrictEqualthrows,doesNotThrow,rejectsіdoesNotRejectmatchіdoesNotMatch
assert.strict — викликний варіант строгого режиму. Глибоке порівняння
підтримує цикли й поширені вбудовані типи, як-от масиви, типізовані масиви,
дати, регулярні вирази, map і set.
Невдалі перевірки кидають assert.AssertionError з code, рівним
ERR_ASSERTION, і полями, зокрема actual, expected і operator. Elide
повідомляє в operator назви операцій, як-от strictEqual, а не символи,
як-от ===.
Стандартні форми виклику описано в документації Node.js щодо assert.
Reference
Status: Partial · 7 of 13 suite tests pass (53.8%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
ok() | function | Full | Tests if value is truthy |
equal() | function | Full | Tests shallow equality between actual and expected using == |
notEqual() | function | Full | Tests shallow inequality between actual and expected using != |
strictEqual() | function | Full | Tests strict equality between actual and expected using === |
notStrictEqual() | function | Full | Tests strict inequality between actual and expected using !== |
deepEqual() | function | Full | Tests for deep equality between actual and expected using abstract equality (==) for leaf comparisons |
deepStrictEqual() | function | Full | Tests for deep strict equality between actual and expected using strict equality (===) for leaf… |
notDeepEqual() | function | Full | Tests that actual is NOT deeply equal to expected (abstract equality) |
notDeepStrictEqual() | function | Full | Tests that actual is NOT deeply strictly equal to expected |
throws() | function | Full | Expects the function fn to throw an error |
doesNotThrow() | function | Full | Asserts that the function fn does not throw an error |
rejects() | function | Full | Awaits the asyncFn promise (or calls it if it’s a function) and expects it to reject |
doesNotReject() | function | Full | Awaits the asyncFn promise (or calls it if it’s a function) and expects it NOT to reject |
fail() | function | Full | Throws an AssertionError unconditionally |
ifError() | function | Full | If value is truthy (not null/undefined/false), throws it |
match() | function | Full | Expects string to match the regular expression regexp |
doesNotMatch() | function | Full | Expects 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.