Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
import assert from "node:assert/strict";
assert.equal(1 + 1, 2);
assert.deepEqual({ a: 1 }, { a: 1 });
API Symbols
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function ok(value: unknown, message?: string | Error): asserts value;

Assert that value is truthy (Boolean(value) === true).

Parameters
NameTypeDescription
valueunknownValue tested for truthiness.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
asserts value
Exceptions
  • AssertionError — when value is falsy.
functionSince v0.0.0-alpha.2vjs & vjs-lite
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).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedTExpected value.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
asserts actual is T
Exceptions
  • AssertionError — when !Object.is(actual, expected).
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function notEqual(actual: unknown, expected: unknown, message?: string | Error): void;

Assert strict inequality (!Object.is(actual, expected)).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not match under Object.is.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when Object.is(actual, expected).
functionSince v0.0.0-alpha.2vjs & vjs-lite
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).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedTExpected value.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
asserts actual is T
Exceptions
  • AssertionError — when !Object.is(actual, expected).
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function notStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void;

Assert strict inequality (!Object.is(actual, expected)).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not match under Object.is.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when Object.is(actual, expected).
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function deepEqual<T>(
  actual: unknown,
  expected: T,
  message?: string | Error,
): asserts actual is T;

Assert strict deep equality between actual and expected.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedTExpected value.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
asserts actual is T
Exceptions
  • AssertionError — when values are not deeply strictly equal.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function notDeepEqual(actual: unknown, expected: unknown, message?: string | Error): void;

Assert that actual and expected are not deeply strictly equal.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not deeply strictly equal.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when values are deeply strictly equal.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function deepStrictEqual<T>(
  actual: unknown,
  expected: T,
  message?: string | Error,
): asserts actual is T;

Assert strict deep equality between actual and expected.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedTExpected value.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
asserts actual is T
Exceptions
  • AssertionError — when values are not deeply strictly equal.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function notDeepStrictEqual(
  actual: unknown,
  expected: unknown,
  message?: string | Error,
): void;

Assert that actual and expected are not deeply strictly equal.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not deeply strictly equal.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when values are deeply strictly equal.
functionSince v0.0.0-alpha.2vjs & vjs-lite
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.

Overload 1 — throws(fn, error?, message?)
NameTypeDescription
fn() => unknownFunction expected to throw.
error optionalAssertPredicateOptional RegExp, constructor, predicate function, or property pattern.
message optionalstring | ErrorOptional failure message or Error instance.
void
Overload 2 — throws(fn, message)

Assert that synchronous fn throws an error, failing with message.

NameTypeDescription
fn() => unknownFunction expected to throw.
messagestring | ErrorFailure message when fn does not throw.
void
Exceptions
  • AssertionError — when fn does not throw or the error does not match.
functionSince v0.0.0-alpha.2vjs & vjs-lite
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.

Overload 1 — doesNotThrow(fn, error?, message?)
NameTypeDescription
fn() => unknownFunction expected to return without throwing.
error optionalAssertPredicateOptional error class or RegExp that must not be thrown.
message optionalstring | ErrorOptional failure message or Error instance.
void
Overload 2 — doesNotThrow(fn, message)

Assert that synchronous fn does not throw an error, failing with message.

NameTypeDescription
fn() => unknownFunction expected to return without throwing.
messagestring | ErrorFailure message when fn throws.
void
Exceptions
  • AssertionError — when fn throws a matching error.
functionSince v0.0.0-alpha.2vjs & vjs-lite
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.

Overload 1 — rejects(fn, error?, message?)
NameTypeDescription
fn(() => Promise<unknown>) | Promise<unknown>Async function or Promise expected to reject.
error optionalAssertPredicateOptional RegExp, constructor, predicate function, or property pattern.
message optionalstring | ErrorOptional failure message or Error instance.
Promise<void> — Promise that resolves when rejection matches or rejects with AssertionError.
Overload 2 — rejects(fn, message)

Assert that fn rejects, failing with message.

NameTypeDescription
fn(() => Promise<unknown>) | Promise<unknown>Async function or Promise expected to reject.
messagestring | ErrorFailure message when fn fulfills.
Promise<void> — Promise that resolves on rejection or rejects with AssertionError.
functionSince v0.0.0-alpha.2vjs & vjs-lite
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.

Overload 1 — doesNotReject(fn, error?, message?)
NameTypeDescription
fn(() => Promise<unknown>) | Promise<unknown>Async function or Promise expected to fulfill.
error optionalAssertPredicateOptional error class or RegExp that must not be rejected with.
message optionalstring | ErrorOptional failure message or Error instance.
Promise<void> — Promise that resolves when fn fulfills or rejects with AssertionError.
Overload 2 — doesNotReject(fn, message)

Assert that fn does not reject, failing with message.

NameTypeDescription
fn(() => Promise<unknown>) | Promise<unknown>Async function or Promise expected to fulfill.
messagestring | ErrorFailure message when fn rejects.
Promise<void> — Promise that resolves when fn fulfills or rejects with AssertionError.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function match(string: string, regexp: RegExp, message?: string | Error): void;

Assert that string matches the regular expression regexp.

Parameters
NameTypeDescription
stringstringString to test.
regexpRegExpRegular expression expected to match.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when string is not a string or does not match regexp.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function doesNotMatch(string: string, regexp: RegExp, message?: string | Error): void;

Assert that string does not match the regular expression regexp.

Parameters
NameTypeDescription
stringstringString to test.
regexpRegExpRegular expression expected not to match.
message optionalstring | ErrorOptional failure message or Error instance.
Returns
void
Exceptions
  • AssertionError — when string matches regexp.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function ifError(value: unknown): asserts value is null | undefined;

Throw value if value is not undefined and not null.

Parameters
NameTypeDescription
valueunknownError value to check.
Returns
asserts value is null | undefined
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function fail(message?: string | Error): never;

Unconditionally throw an AssertionError (or message if it is an Error).

Parameters
NameTypeDescription
message optionalstring | ErrorOptional error message or Error instance.
Returns
never — Never returns normally.
Exceptions
  • AssertionError — always (or the passed Error).
classSince v0.0.0-alpha.2vjs & vjs-lite
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
constructor(options?: AssertionErrorOptions);

Construct an AssertionError.

Parameters
NameTypeDescription
options optionalAssertionErrorOptionsOptional failure metadata.
Properties
PropertyTypeDescriptionSince
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 unknownObserved value, when applicable.v0.0.0-alpha.2
expected unknownExpected value, when applicable.v0.0.0-alpha.2
operator stringAssertion operator string.v0.0.0-alpha.2
generatedMessage booleanWhether the message was auto-generated rather than user-provided.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface AssertionErrorOptions {
  message?: string;
  actual?: unknown;
  expected?: unknown;
  operator?: string;
  stackStartFn?: (...args: any[]) => unknown;
}

Options accepted by new AssertionError(options).

Properties
PropertyTypeDescriptionSince
message optionalstringCustom failure message (overrides generated diff message).v0.0.0-alpha.2
actual optionalunknownObserved value.v0.0.0-alpha.2
expected optionalunknownExpected value.v0.0.0-alpha.2
operator optionalstringAssertion operator name.v0.0.0-alpha.2
stackStartFn optional(...args: any[]) => unknownStack trace starting function excluded from stack frames.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
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.

Call Signature
(value: unknown, message?: string | Error): asserts value;

Assert that value is truthy.

Parameters
NameTypeDescription
valueunknownValue tested for truthiness.
message optionalstring | ErrorOptional failure message or Error instance to throw.
Returns
asserts value
Exceptions
  • AssertionError — when value is falsy.
Properties
PropertyTypeDescriptionSince
AssertionError readonlytypeof AssertionErrorAssertionError constructor.v0.0.0-alpha.2
ok readonlytypeof okAlias for assert(value, message).v0.0.0-alpha.2
equal readonlytypeof equalStrict equality assertion (Object.is).v0.0.0-alpha.2
notEqual readonlytypeof notEqualStrict inequality assertion (!Object.is).v0.0.0-alpha.2
strictEqual readonlytypeof strictEqualStrict equality assertion (Object.is).v0.0.0-alpha.2
notStrictEqual readonlytypeof notStrictEqualStrict inequality assertion (!Object.is).v0.0.0-alpha.2
deepEqual readonlytypeof deepEqualStrict deep equality assertion.v0.0.0-alpha.2
notDeepEqual readonlytypeof notDeepEqualStrict deep inequality assertion.v0.0.0-alpha.2
deepStrictEqual readonlytypeof deepStrictEqualStrict deep equality assertion.v0.0.0-alpha.2
notDeepStrictEqual readonlytypeof notDeepStrictEqualStrict deep inequality assertion.v0.0.0-alpha.2
throws readonlytypeof throwsSynchronous throw assertion.v0.0.0-alpha.2
doesNotThrow readonlytypeof doesNotThrowSynchronous non-throw assertion.v0.0.0-alpha.2
rejects readonlytypeof rejectsAsynchronous rejection assertion.v0.0.0-alpha.2
doesNotReject readonlytypeof doesNotRejectAsynchronous fulfillment assertion.v0.0.0-alpha.2
match readonlytypeof matchRegular expression match assertion.v0.0.0-alpha.2
doesNotMatch readonlytypeof doesNotMatchRegular expression non-match assertion.v0.0.0-alpha.2
ifError readonlytypeof ifErrorThrow value if it is neither null nor undefined.v0.0.0-alpha.2
fail readonlytypeof failUnconditional assertion failure helper.v0.0.0-alpha.2
strict readonlyNodeStrictAssertSelf-reference to strict assertion mode.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs & vjs-lite
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.

constSince v0.0.0-alpha.2vjs & vjs-lite
export const strict: NodeStrictAssert;

Self-reference to the strict-mode assert namespace.

defaultSince v0.0.0-alpha.2vjs & vjs-lite
const _default: NodeStrictAssert;
export default _default;

Default export: callable strict-mode assert function with all assertion methods attached.