Skip to content
VaelJS Blog

vael:test

Sequential registration-based test runner and reporting API.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Note that TestOptions.timeout is only supported in vjs (vaeljs); specifying timeout in vjs-lite throws VAEL_TEST_NO_TIMER.

import ... from "vael:test"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-test (default) in vaeljs-lite-core
The { timeout } test option is supported in vjs (V8) only; vjs-lite rejects it with VAEL_TEST_NO_TIMER.
import assert from "vael:assert";
import test, { run } from "vael:test";
test("addition works", () => {
assert.strictEquals(2 + 2, 4);
});
const report = await run({ throwOnFailure: false });
console.log(report.passed, report.total);
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function test(name: string): void;
export function test(name: string, fn: () => unknown, options?: TestOptions): void;

Register a pending test case (fails the run because no callback was supplied).

Register a test case to execute on the next run().

Generator functions are rejected with VAEL_TEST_UNSUPPORTED.

Parameters
NameTypeDescription
namestringNon-blank test name.
fn optional() => unknownSynchronous or async test body.
options optionalTestOptionsOptional registration options ({ timeout }).
Returns
void
Exceptions
  • TypeError — on invalid name; on invalid arguments or unknown options.
  • Error — with code "VAEL_TEST_DURING_RUN" or "VAEL_TEST_LIMIT"; with code "VAEL_TEST_DURING_RUN", "VAEL_TEST_LIMIT", "VAEL_TEST_UNSUPPORTED", or "VAEL_TEST_NO_TIMER".
import assert from "vael:assert";
import { test } from "vael:test";
test("async work resolves", async () => {
const value = await Promise.resolve(123);
assert.strictEquals(value, 123);
});
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function run(options?: RunOptions): Promise<TestReport>;

Execute queued tests in registration order and return the cumulative TestReport.

Parameters
NameTypeDescription
options optionalRunOptionsOptional { throwOnFailure, settle } flags.
Returns
Promise<TestReport> — Promise resolving to the cumulative TestReport.
Exceptions
  • TypeError — on invalid options.
  • Error — with code = "VAEL_TEST_ALREADY_RUNNING" if re-entered.
  • TestFailureError — (code = "VAEL_TEST_FAILURES") when throwOnFailure is true and any failure, pending test, unhandled rejection, or unsettled work was recorded.
import { run } from "vael:test";
const report = await run({ throwOnFailure: false });
console.log(report.passed, report.failed);
classSince v0.0.0-alpha.0vjs & vjs-lite
export class TestFailureError extends Error {
  override name: "TestFailureError";
  code: "VAEL_TEST_FAILURES";
  failures: string[];
  constructor(message: string, failures: string[]);
}

Error thrown by run() when throwOnFailure is true and any test failed, was pending, or left unhandled/unsettled work.

Constructor
constructor(message: string, failures: string[]);

Construct a TestFailureError.

Parameters
NameTypeDescription
messagestringSummary failure message.
failuresstring[]Individual formatted failure strings.
Properties
PropertyTypeDescriptionSince
name "TestFailureError"Error name ("TestFailureError").v0.0.0-alpha.0
code "VAEL_TEST_FAILURES"Machine-readable error code ("VAEL_TEST_FAILURES").v0.0.0-alpha.0
failures string[]Formatted failure messages.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface TestOptions {
  timeout?: number;
}

Options accepted by test(name, fn, options).

Properties
PropertyTypeDescriptionSince
timeout optionalnumberPer-test timeout in milliseconds (1..=2_147_483_647, default 30_000 in vjs V8). Not supported in vjs-lite (throws VAEL_TEST_NO_TIMER because QuickJS has no host timers).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface RunOptions {
  throwOnFailure?: boolean;
  settle?: boolean;
}

Options accepted by run(options).

Properties
PropertyTypeDescriptionSince
throwOnFailure optionalbooleanThrow TestFailureError (code = "VAEL_TEST_FAILURES") if the cumulative report contains any failure, pending test, unhandled rejection, or unsettled event-loop work (default: true).v0.0.0-alpha.0
settle optionalbooleanRun a bounded event-loop settle drain after tests finish (default: false). Intended for the runtime's final pass after entry module evaluation.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface TestReport {
  total: number;
  passed: number;
  failed: number;
  pending: number;
  unhandled: number;
  unsettled: number;
  failures: string[];
}

Cumulative summary returned by run(options).

Properties
PropertyTypeDescriptionSince
total numberTotal number of registered tests executed.v0.0.0-alpha.0
passed numberCount of passing tests.v0.0.0-alpha.0
failed numberCount of failing tests.v0.0.0-alpha.0
pending numberCount of pending tests (registered without a callback).v0.0.0-alpha.0
unhandled numberCount of unhandled Promise rejections observed during the run.v0.0.0-alpha.0
unsettled numberCount of unsettled async test promises.v0.0.0-alpha.0
failures string[]Formatted failure messages for all failing/pending/unhandled items.v0.0.0-alpha.0
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type TestErrorCode =
  | "VAEL_TEST_FAILURES"
  | "VAEL_TEST_DURING_RUN"
  | "VAEL_TEST_ALREADY_RUNNING"
  | "VAEL_TEST_LIMIT"
  | "VAEL_TEST_UNSUPPORTED"
  | "VAEL_TEST_NO_TIMER"
  | "VAEL_TEST_PENDING"
  | "VAEL_TEST_CYCLE"
  | "VAEL_TEST_CHAIN"
  | "VAEL_TEST_OBSERVE";

Error codes thrown by vael:test operations on Error.code.

constSince v0.0.0-alpha.0vjs & vjs-lite
export const MAX_TESTS: 10000;

Maximum number of tests that can be registered in one runtime (10_000).

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

Default export: the test registration function.