node:assert/strict
Node-compatible strict-mode assertion utilities (node:assert/strict).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed. In strict mode, equal/notEqual and
deepEqual/notDeepEqual behave identically to strictEqual/notStrictEqual
and deepStrictEqual/notDeepStrictEqual.
import ... from "node:assert/strict"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import assert from "node:assert/strict";
assert.equal(1 + 1, 2);assert.deepEqual({ a: 1 }, { a: 1 });okv0.0.0-alpha.2value is truthy (Boolean(value) === true).equalv0.0.0-alpha.2Object.is(actual, expected).notEqualv0.0.0-alpha.2!Object.is(actual, expected)).strictEqualv0.0.0-alpha.2Object.is(actual, expected).notStrictEqualv0.0.0-alpha.2!Object.is(actual, expected)).deepEqualv0.0.0-alpha.2actual and expected.notDeepEqualv0.0.0-alpha.2actual and expected are not deeply strictly equal.deepStrictEqualv0.0.0-alpha.2actual and expected.notDeepStrictEqualv0.0.0-alpha.2actual and expected are not deeply strictly equal.throwsv0.0.0-alpha.2fn throws an error matching optional error predicate.doesNotThrowv0.0.0-alpha.2fn does not throw an error.rejectsv0.0.0-alpha.2fn (or Promise fn) rejects with an error matching optional error predicate.doesNotRejectv0.0.0-alpha.2fn (or Promise fn) does not reject.matchv0.0.0-alpha.2string matches the regular expression regexp.doesNotMatchv0.0.0-alpha.2string does not match the regular expression regexp.ifErrorv0.0.0-alpha.2value if value is not undefined and not null.failv0.0.0-alpha.2AssertionError (or message if it is an Error).Functions
Section titled “Functions”export function ok(value: unknown, message?: string | Error): asserts value;
Assert that value is truthy (Boolean(value) === true).
| Name | Type | Description |
|---|---|---|
value | unknown | Value tested for truthiness. |
message optional | string | Error | Optional failure message or Error instance. |
asserts valueAssertionError— whenvalueis falsy.
export function equal<T>(
actual: unknown,
expected: T,
message?: string | Error,
): asserts actual is T;
Assert strict equality as determined by Object.is(actual, expected).
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | T | Expected value. |
message optional | string | Error | Optional failure message or Error instance. |
asserts actual is TAssertionError— when!Object.is(actual, expected).
notEqual
Section titled “notEqual”export function notEqual(actual: unknown, expected: unknown, message?: string | Error): void;
Assert strict inequality (!Object.is(actual, expected)).
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | unknown | Value that actual must not match under Object.is. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— whenObject.is(actual, expected).
strictEqual
Section titled “strictEqual”export function strictEqual<T>(
actual: unknown,
expected: T,
message?: string | Error,
): asserts actual is T;
Assert strict equality as determined by Object.is(actual, expected).
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | T | Expected value. |
message optional | string | Error | Optional failure message or Error instance. |
asserts actual is TAssertionError— when!Object.is(actual, expected).
notStrictEqual
Section titled “notStrictEqual”export function notStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void;
Assert strict inequality (!Object.is(actual, expected)).
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | unknown | Value that actual must not match under Object.is. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— whenObject.is(actual, expected).
deepEqual
Section titled “deepEqual”export function deepEqual<T>(
actual: unknown,
expected: T,
message?: string | Error,
): asserts actual is T;
Assert strict deep equality between actual and expected.
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | T | Expected value. |
message optional | string | Error | Optional failure message or Error instance. |
asserts actual is TAssertionError— when values are not deeply strictly equal.
notDeepEqual
Section titled “notDeepEqual”export function notDeepEqual(actual: unknown, expected: unknown, message?: string | Error): void;
Assert that actual and expected are not deeply strictly equal.
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | unknown | Value that actual must not deeply strictly equal. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— when values are deeply strictly equal.
deepStrictEqual
Section titled “deepStrictEqual”export function deepStrictEqual<T>(
actual: unknown,
expected: T,
message?: string | Error,
): asserts actual is T;
Assert strict deep equality between actual and expected.
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | T | Expected value. |
message optional | string | Error | Optional failure message or Error instance. |
asserts actual is TAssertionError— when values are not deeply strictly equal.
notDeepStrictEqual
Section titled “notDeepStrictEqual”export function notDeepStrictEqual(
actual: unknown,
expected: unknown,
message?: string | Error,
): void;
Assert that actual and expected are not deeply strictly equal.
| Name | Type | Description |
|---|---|---|
actual | unknown | Observed value. |
expected | unknown | Value that actual must not deeply strictly equal. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— when values are deeply strictly equal.
throws
Section titled “throws”export function throws(fn: () => unknown, error?: AssertPredicate, message?: string | Error): void;
export function throws(fn: () => unknown, message: string | Error): void;
Assert that synchronous fn throws an error matching optional error predicate.
throws(fn, error?, message?)| Name | Type | Description |
|---|---|---|
fn | () => unknown | Function expected to throw. |
error optional | AssertPredicate | Optional RegExp, constructor, predicate function, or property pattern. |
message optional | string | Error | Optional failure message or Error instance. |
voidthrows(fn, message)Assert that synchronous fn throws an error, failing with message.
| Name | Type | Description |
|---|---|---|
fn | () => unknown | Function expected to throw. |
message | string | Error | Failure message when fn does not throw. |
voidAssertionError— whenfndoes not throw or the error does not match.
doesNotThrow
Section titled “doesNotThrow”export function doesNotThrow(
fn: () => unknown,
error?: AssertPredicate,
message?: string | Error,
): void;
export function doesNotThrow(fn: () => unknown, message: string | Error): void;
Assert that synchronous fn does not throw an error.
doesNotThrow(fn, error?, message?)| Name | Type | Description |
|---|---|---|
fn | () => unknown | Function expected to return without throwing. |
error optional | AssertPredicate | Optional error class or RegExp that must not be thrown. |
message optional | string | Error | Optional failure message or Error instance. |
voiddoesNotThrow(fn, message)Assert that synchronous fn does not throw an error, failing with message.
| Name | Type | Description |
|---|---|---|
fn | () => unknown | Function expected to return without throwing. |
message | string | Error | Failure message when fn throws. |
voidAssertionError— whenfnthrows a matching error.
rejects
Section titled “rejects”export function rejects(
fn: (() => Promise<unknown>) | Promise<unknown>,
error?: AssertPredicate,
message?: string | Error,
): Promise<void>;
export function rejects(
fn: (() => Promise<unknown>) | Promise<unknown>,
message: string | Error,
): Promise<void>;
Assert that fn (or Promise fn) rejects with an error matching optional error predicate.
rejects(fn, error?, message?)| Name | Type | Description |
|---|---|---|
fn | (() => Promise<unknown>) | Promise<unknown> | Async function or Promise expected to reject. |
error optional | AssertPredicate | Optional RegExp, constructor, predicate function, or property pattern. |
message optional | string | Error | Optional failure message or Error instance. |
Promise<void> — Promise that resolves when rejection matches or rejects with AssertionError.rejects(fn, message)Assert that fn rejects, failing with message.
| Name | Type | Description |
|---|---|---|
fn | (() => Promise<unknown>) | Promise<unknown> | Async function or Promise expected to reject. |
message | string | Error | Failure message when fn fulfills. |
Promise<void> — Promise that resolves on rejection or rejects with AssertionError.doesNotReject
Section titled “doesNotReject”export function doesNotReject(
fn: (() => Promise<unknown>) | Promise<unknown>,
error?: AssertPredicate,
message?: string | Error,
): Promise<void>;
export function doesNotReject(
fn: (() => Promise<unknown>) | Promise<unknown>,
message: string | Error,
): Promise<void>;
Assert that fn (or Promise fn) does not reject.
doesNotReject(fn, error?, message?)| Name | Type | Description |
|---|---|---|
fn | (() => Promise<unknown>) | Promise<unknown> | Async function or Promise expected to fulfill. |
error optional | AssertPredicate | Optional error class or RegExp that must not be rejected with. |
message optional | string | Error | Optional failure message or Error instance. |
doesNotReject(fn, message)Assert that fn does not reject, failing with message.
| Name | Type | Description |
|---|---|---|
fn | (() => Promise<unknown>) | Promise<unknown> | Async function or Promise expected to fulfill. |
message | string | Error | Failure message when fn rejects. |
export function match(string: string, regexp: RegExp, message?: string | Error): void;
Assert that string matches the regular expression regexp.
| Name | Type | Description |
|---|---|---|
string | string | String to test. |
regexp | RegExp | Regular expression expected to match. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— whenstringis not a string or does not matchregexp.
doesNotMatch
Section titled “doesNotMatch”export function doesNotMatch(string: string, regexp: RegExp, message?: string | Error): void;
Assert that string does not match the regular expression regexp.
| Name | Type | Description |
|---|---|---|
string | string | String to test. |
regexp | RegExp | Regular expression expected not to match. |
message optional | string | Error | Optional failure message or Error instance. |
voidAssertionError— whenstringmatchesregexp.
ifError
Section titled “ifError”export function ifError(value: unknown): asserts value is null | undefined;
Throw value if value is not undefined and not null.
| Name | Type | Description |
|---|---|---|
value | unknown | Error value to check. |
asserts value is null | undefinedexport function fail(message?: string | Error): never;
Unconditionally throw an AssertionError (or message if it is an Error).
| Name | Type | Description |
|---|---|---|
message optional | string | Error | Optional error message or Error instance. |
never — Never returns normally.AssertionError— always (or the passedError).
Classes
Section titled “Classes”AssertionError
Section titled “AssertionError”export class AssertionError extends Error {
override name: "AssertionError";
code: "ERR_ASSERTION";
actual: unknown;
expected: unknown;
operator: string;
generatedMessage: boolean;
constructor(options?: AssertionErrorOptions);
}
Error thrown when a node:assert/strict assertion fails.
constructor(options?: AssertionErrorOptions);
Construct an AssertionError.
| Name | Type | Description |
|---|---|---|
options optional | AssertionErrorOptions | Optional failure metadata. |
| Property | Type | Description | Since |
|---|---|---|---|
name | "AssertionError" | Error name ("AssertionError"). | v0.0.0-alpha.2 |
code | "ERR_ASSERTION" | Machine-readable error code ("ERR_ASSERTION"). | v0.0.0-alpha.2 |
actual | unknown | Observed value, when applicable. | v0.0.0-alpha.2 |
expected | unknown | Expected value, when applicable. | v0.0.0-alpha.2 |
operator | string | Assertion operator string. | v0.0.0-alpha.2 |
generatedMessage | boolean | Whether the message was auto-generated rather than user-provided. | v0.0.0-alpha.2 |
Interfaces
Section titled “Interfaces”AssertionErrorOptions
Section titled “AssertionErrorOptions”export interface AssertionErrorOptions {
message?: string;
actual?: unknown;
expected?: unknown;
operator?: string;
stackStartFn?: (...args: any[]) => unknown;
}
Options accepted by new AssertionError(options).
| Property | Type | Description | Since |
|---|---|---|---|
message optional | string | Custom failure message (overrides generated diff message). | v0.0.0-alpha.2 |
actual optional | unknown | Observed value. | v0.0.0-alpha.2 |
expected optional | unknown | Expected value. | v0.0.0-alpha.2 |
operator optional | string | Assertion operator name. | v0.0.0-alpha.2 |
stackStartFn optional | (...args: any[]) => unknown | Stack trace starting function excluded from stack frames. | v0.0.0-alpha.2 |
NodeStrictAssert
Section titled “NodeStrictAssert”export interface NodeStrictAssert {
(value: unknown, message?: string | Error): asserts value;
readonly AssertionError: typeof AssertionError;
readonly ok: typeof ok;
readonly equal: typeof equal;
readonly notEqual: typeof notEqual;
readonly strictEqual: typeof strictEqual;
readonly notStrictEqual: typeof notStrictEqual;
readonly deepEqual: typeof deepEqual;
readonly notDeepEqual: typeof notDeepEqual;
readonly deepStrictEqual: typeof deepStrictEqual;
readonly notDeepStrictEqual: typeof notDeepStrictEqual;
readonly throws: typeof throws;
readonly doesNotThrow: typeof doesNotThrow;
readonly rejects: typeof rejects;
readonly doesNotReject: typeof doesNotReject;
readonly match: typeof match;
readonly doesNotMatch: typeof doesNotMatch;
readonly ifError: typeof ifError;
readonly fail: typeof fail;
readonly strict: NodeStrictAssert;
}
Callable strict assert interface exposing all strict assertion helpers.
(value: unknown, message?: string | Error): asserts value;
Assert that value is truthy.
| Name | Type | Description |
|---|---|---|
value | unknown | Value tested for truthiness. |
message optional | string | Error | Optional failure message or Error instance to throw. |
asserts valueAssertionError— whenvalueis falsy.
| Property | Type | Description | Since |
|---|---|---|---|
AssertionError readonly | typeof AssertionError | AssertionError constructor. | v0.0.0-alpha.2 |
ok readonly | typeof ok | Alias for assert(value, message). | v0.0.0-alpha.2 |
equal readonly | typeof equal | Strict equality assertion (Object.is). | v0.0.0-alpha.2 |
notEqual readonly | typeof notEqual | Strict inequality assertion (!Object.is). | v0.0.0-alpha.2 |
strictEqual readonly | typeof strictEqual | Strict equality assertion (Object.is). | v0.0.0-alpha.2 |
notStrictEqual readonly | typeof notStrictEqual | Strict inequality assertion (!Object.is). | v0.0.0-alpha.2 |
deepEqual readonly | typeof deepEqual | Strict deep equality assertion. | v0.0.0-alpha.2 |
notDeepEqual readonly | typeof notDeepEqual | Strict deep inequality assertion. | v0.0.0-alpha.2 |
deepStrictEqual readonly | typeof deepStrictEqual | Strict deep equality assertion. | v0.0.0-alpha.2 |
notDeepStrictEqual readonly | typeof notDeepStrictEqual | Strict deep inequality assertion. | v0.0.0-alpha.2 |
throws readonly | typeof throws | Synchronous throw assertion. | v0.0.0-alpha.2 |
doesNotThrow readonly | typeof doesNotThrow | Synchronous non-throw assertion. | v0.0.0-alpha.2 |
rejects readonly | typeof rejects | Asynchronous rejection assertion. | v0.0.0-alpha.2 |
doesNotReject readonly | typeof doesNotReject | Asynchronous fulfillment assertion. | v0.0.0-alpha.2 |
match readonly | typeof match | Regular expression match assertion. | v0.0.0-alpha.2 |
doesNotMatch readonly | typeof doesNotMatch | Regular expression non-match assertion. | v0.0.0-alpha.2 |
ifError readonly | typeof ifError | Throw value if it is neither null nor undefined. | v0.0.0-alpha.2 |
fail readonly | typeof fail | Unconditional assertion failure helper. | v0.0.0-alpha.2 |
strict readonly | NodeStrictAssert | Self-reference to strict assertion mode. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”AssertPredicate
Section titled “AssertPredicate”export type AssertPredicate =
| RegExp
| (abstract new (...args: any[]) => any)
| ((thrown: unknown) => boolean)
| Record<string, unknown>;
Predicate or matcher accepted by throws, doesNotThrow, rejects, and doesNotReject.
Constants
Section titled “Constants”strict
Section titled “strict”export const strict: NodeStrictAssert;
Self-reference to the strict-mode assert namespace.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: NodeStrictAssert;
export default _default;
Default export: callable strict-mode assert function with all assertion methods attached.
