Skip to content
VaelJS Blog

vael:assert

Structural equality, identity, error, and rejection assertions.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Exports a callable default assert function with assertion methods attached.

import ... from "vael:assert"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-assert (default) in vaeljs-lite-core
import assert from "vael:assert";
assert.strictEquals(1 + 1, 2);
assert.equals({ a: [1, 2] }, { a: [1, 2] });
assert.throws(() => {
throw new RangeError("bad index");
}, RangeError);
API Symbols
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function equals(actual: unknown, expected: unknown, message?: string): void;

Assert strict structural equality between actual and expected.

Prototypes must match; NaN equals NaN; -0 differs from 0; Map and Set ignore iteration order; cycles are handled.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownExpected value.
message optionalstringOptional failure message.
Returns
void
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when values differ.
import { equals } from "vael:assert";
equals(new Set([1, 2]), new Set([2, 1]));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function notEquals(actual: unknown, expected: unknown, message?: string): void;

Assert that actual and expected are not structurally equal.

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not structurally equal.
message optionalstringOptional failure message.
Returns
void
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when values are structurally equal.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function strictEquals(actual: unknown, expected: unknown, message?: string): void;

Assert identity equality under Object.is(actual, expected).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownExpected value.
message optionalstringOptional failure message.
Returns
void
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when Object.is(actual, expected) is false.
import { strictEquals } from "vael:assert";
strictEquals(Number.NaN, Number.NaN);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function notStrictEquals(actual: unknown, expected: unknown, message?: string): void;

Assert that actual and expected differ under Object.is(actual, expected).

Parameters
NameTypeDescription
actualunknownObserved value.
expectedunknownValue that actual must not match under Object.is.
message optionalstringOptional failure message.
Returns
void
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when Object.is(actual, expected) is true.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function fail(message?: string): never;

Unconditionally throw an AssertionError (code = "VAEL_ASSERT").

Parameters
NameTypeDescription
message optionalstringOptional failure message.
Returns
never — Never returns normally.
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — always.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function throws<E>(
  callback: () => unknown,
  expected: abstract new (...args: any[]) => E,
  message?: string,
): E;
export function throws(
  callback: () => unknown,
  expected?: RegExp | null,
  message?: string,
): unknown;

Assert that synchronous callback throws an instance of expected, returning the thrown value.

Returning a native Promise throws TypeError (use rejects instead).

Assert that synchronous callback throws (optionally matching expected RegExp), returning the thrown value.

Parameters
NameTypeDescription
callback() => unknownSynchronous function expected to throw.
expected optional(abstract new (...args: any[]) => E) | RegExp | nullConstructor function or class the thrown value must match via instanceof; Optional RegExp matched against the thrown value or error message.
message optionalstringOptional failure message.
Returns
  • E — The thrown error instance.
  • unknown — The thrown value.
Exceptions
  • TypeError — on invalid arguments or Promise-returning callback.
  • AssertionError — (code = "VAEL_ASSERT") when callback does not throw or mismatches.
import { throws } from "vael:assert";
const err = throws(() => JSON.parse("{"), SyntaxError);
console.log(err.name);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function doesNotThrow<T>(callback: () => T, message?: string): T;

Assert that synchronous callback returns without throwing, returning its result.

Returning a native Promise throws TypeError (use doesNotReject instead).

Parameters
NameTypeDescription
callback() => TSynchronous function expected not to throw.
message optionalstringOptional failure message.
Returns
T — The value returned by callback.
Exceptions
  • TypeError — on invalid arguments or Promise-returning callback.
  • AssertionError — (code = "VAEL_ASSERT") when callback throws.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function rejects<E>(
  callback: () => unknown,
  expected: abstract new (...args: any[]) => E,
  message?: string,
): Promise<E>;
export function rejects(
  callback: () => unknown,
  expected?: RegExp | null,
  message?: string,
): Promise<unknown>;

Assert that callback rejects (or throws synchronously) with an instance of expected, resolving with the rejection reason.

Assert that callback rejects (or throws synchronously), optionally matching expected RegExp, resolving with the rejection reason.

Parameters
NameTypeDescription
callback() => unknownFunction returning a Promise/thenable expected to reject.
expected optional(abstract new (...args: any[]) => E) | RegExp | nullConstructor function or class the rejection reason must match via instanceof; Optional RegExp matched against the rejection reason.
message optionalstringOptional failure message.
Returns
  • Promise<E> — Promise resolving to the rejection reason.
  • Promise<unknown> — Promise resolving to the rejection reason.
Exceptions
  • TypeError — when arguments are invalid.
  • AssertionError — (code = "VAEL_ASSERT") when callback fulfills, returns non-thenable, or mismatches.
import { rejects } from "vael:assert";
await rejects(async () => {
throw new TypeError("bad input");
}, TypeError);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function doesNotReject<T>(callback: () => PromiseLike<T>, message?: string): Promise<T>;

Assert that callback returns a Promise or thenable that fulfills without throwing or rejecting, resolving with its fulfillment value.

Parameters
NameTypeDescription
callback() => PromiseLike<T>Function returning a Promise/thenable expected to fulfill.
message optionalstringOptional failure message.
Returns
Promise<T> — Promise resolving to the fulfillment value of callback.
Exceptions
  • TypeError — when arguments are invalid.
  • AssertionError — (code = "VAEL_ASSERT") when callback throws, rejects, or returns non-thenable.
classSince v0.0.0-alpha.0vjs & vjs-lite
export class AssertionError extends Error {
  override name: "AssertionError";
  code: "VAEL_ASSERT";
  operator: string;
  actual?: unknown;
  expected?: unknown;
  constructor(message?: string, options?: AssertionErrorOptions);
}

Error thrown when a vael:assert assertion fails.

Carries code = "VAEL_ASSERT", name = "AssertionError", and operator.

Constructor
constructor(message?: string, options?: AssertionErrorOptions);

Construct an AssertionError.

Parameters
NameTypeDescription
message optionalstringOptional failure message.
options optionalAssertionErrorOptionsOptional operator, actual, and expected metadata.
Properties
PropertyTypeDescriptionSince
name "AssertionError"Error name ("AssertionError").v0.0.0-alpha.0
code "VAEL_ASSERT"Machine-readable error code ("VAEL_ASSERT").v0.0.0-alpha.0
operator stringAssertion operator name.v0.0.0-alpha.0
actual optionalunknownObserved value, if applicable.v0.0.0-alpha.0
expected optionalunknownExpected value or matcher, if applicable.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface AssertionErrorOptions {
  operator?: string;
  actual?: unknown;
  expected?: unknown;
}

Options accepted by new AssertionError(message, options).

Properties
PropertyTypeDescriptionSince
operator optionalstringAssertion operator name (defaults to "assert").v0.0.0-alpha.0
actual optionalunknownObserved value, if applicable.v0.0.0-alpha.0
expected optionalunknownExpected value or matcher, if applicable.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface VaelAssert {
  (value: unknown, message?: string): asserts value;
  readonly AssertionError: typeof AssertionError;
  readonly ok: VaelAssert;
  readonly equals: typeof equals;
  readonly notEquals: typeof notEquals;
  readonly strictEquals: typeof strictEquals;
  readonly notStrictEquals: typeof notStrictEquals;
  readonly fail: typeof fail;
  readonly throws: typeof throws;
  readonly doesNotThrow: typeof doesNotThrow;
  readonly rejects: typeof rejects;
  readonly doesNotReject: typeof doesNotReject;
}

Callable assert object exposing all assertion helpers as properties.

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

Assert that value is truthy.

Parameters
NameTypeDescription
valueunknownValue tested for truthiness.
message optionalstringOptional failure message.
Returns
asserts value
Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when value is falsy.
Properties
PropertyTypeDescriptionSince
AssertionError readonlytypeof AssertionErrorAssertionError constructor reference.v0.0.0-alpha.0
ok readonlyVaelAssertAlias for assert.v0.0.0-alpha.0
equals readonlytypeof equalsStructural equality assertion helper.v0.0.0-alpha.0
notEquals readonlytypeof notEqualsStructural inequality assertion helper.v0.0.0-alpha.0
strictEquals readonlytypeof strictEqualsObject.is identity equality assertion helper.v0.0.0-alpha.0
notStrictEquals readonlytypeof notStrictEqualsObject.is identity inequality assertion helper.v0.0.0-alpha.0
fail readonlytypeof failUnconditional failure helper.v0.0.0-alpha.0
throws readonlytypeof throwsSynchronous throw assertion helper.v0.0.0-alpha.0
doesNotThrow readonlytypeof doesNotThrowSynchronous non-throw assertion helper.v0.0.0-alpha.0
rejects readonlytypeof rejectsAsynchronous rejection assertion helper.v0.0.0-alpha.0
doesNotReject readonlytypeof doesNotRejectAsynchronous fulfillment assertion helper.v0.0.0-alpha.0
constSince v0.0.0-alpha.0vjs & vjs-lite
export const assert: VaelAssert;

Assert that value is truthy.

Exceptions
  • TypeError — when message is not a string or arity is wrong.
  • AssertionError — (code = "VAEL_ASSERT") when value is falsy.
import { assert } from "vael:assert";
assert(2 > 1, "math should hold");
constSince v0.0.0-alpha.0vjs & vjs-lite
export const ok: VaelAssert;

Alias for assert (identical function object).

defaultSince v0.0.0-alpha.0vjs & vjs-lite
export default assert;

Default export: the callable assert function with all assertion properties.