Skip to content
VaelJS Blog

Testing with vael:test & vael:assert

VaelJS includes a built-in, zero-dependency test framework across both vjs and vjs-lite: vael:test for registering tests and vael:assert for structural, identity, and async rejection assertions.

1. The vjs test & vjs-lite test Isolation Model

Section titled “1. The vjs test & vjs-lite test Isolation Model”

Unlike runners that share module caches across files or guess test directories, vjs test and vjs-lite test enforce strict explicit isolation:

  1. Explicit files or globs required: Pass one or more file paths or shell-style globs ("tests/**/*.test.ts"). There is no default file discovery.
  2. Fresh runtime per file: Matching paths are sorted and deduplicated, and each file runs inside a brand-new V8 isolate (vjs) or QuickJS runtime (vjs-lite). Globals, module state, and queued tests never leak between files.
  3. Fail-closed exit codes:
    • Exit 0: Every selected file loaded cleanly, registered at least 1 test, and all tests passed with zero pending tests, unhandled rejections, or unsettled async work.
    • Exit 1: Any test failed, registered zero tests, left a pending test("name"), left an unhandled rejection, or left unsettled event-loop work.
    • Exit 2: Invalid CLI flags, missing explicit --config, or invalid glob pattern.
Terminal window
# Run all test files under tests/ in vjs
vjs test "tests/**/*.test.ts"
# Run portable tests in vjs-lite as well
vjs-lite test "tests/unit/*.test.ts"
# Pass runtime policy flags before or after `test`
vjs test --offline --frozen-lockfile "tests/**/*.test.ts"

Register synchronous or async tests with test(name, fn, options?). When invoked via vjs test / vjs-lite test, the CLI drains registered tests in registration order after the module evaluates. You can also call await run({ throwOnFailure: false }) explicitly when embedding or writing a standalone runner script.

import assert from "vael:assert";
import test from "vael:test";
test("synchronous arithmetic", () => {
assert.strictEquals(2 + 2, 4);
});
test("async promise resolution", async () => {
const value = await Promise.resolve("ok");
assert.strictEquals(value, "ok");
});
// Pending test marker (fails the run until implemented!)
// test("implement retry backoff");
  • Running a subset: Pass the specific file or glob to vjs test tests/auth.test.ts (each file is already isolated).
  • Per-test timeouts (vjs only): Pass { timeout: ms } (1..=2_147_483_647, default 30_000 ms in vjs). Because vjs-lite has no host timers, passing { timeout } in vjs-lite throws VAEL_TEST_NO_TIMER.
  • No generator tests: Generator functions (function* () {}) are rejected with VAEL_TEST_UNSUPPORTED.

vael:assert exports a callable assert function (also ok) with assertion helpers attached, plus named exports (equals, notEquals, strictEquals, notStrictEquals, throws, doesNotThrow, rejects, doesNotReject, fail, AssertionError).

Every failed assertion throws AssertionError with code === "VAEL_ASSERT" and operator.

import assert, { AssertionError, equals, rejects, strictEquals, throws } from "vael:assert";
import test from "vael:test";
test("truthiness and Object.is identity", () => {
assert(true, "expected truthy value");
assert.ok("non-empty");
// strictEquals uses Object.is semantics (NaN === NaN, -0 !== +0)
strictEquals(Number.NaN, Number.NaN);
assert.notStrictEquals(-0, 0);
});
test("structural equality (equals / notEquals)", () => {
// Prototypes must match; Map and Set compare regardless of insertion order
equals({ items: [1, 2], tags: new Set(["a", "b"]) }, { items: [1, 2], tags: new Set(["b", "a"]) });
assert.notEquals({ a: 1 }, { a: 2 });
});
test("synchronous throws and async rejections", async () => {
const synErr = throws(() => JSON.parse("{"), SyntaxError);
assert(synErr instanceof SyntaxError);
throws(() => {
throw new Error("invalid token");
}, /invalid token/);
assert.doesNotThrow(() => JSON.parse('{"ok":true}'));
const asyncErr = await rejects(async () => {
throw new AssertionError("boom", { operator: "custom" });
}, AssertionError);
strictEquals(asyncErr.code, "VAEL_ASSERT");
const resolved = await assert.doesNotReject(() => Promise.resolve(42));
strictEquals(resolved, 42);
});

4. Zero-Socket HTTP Integration Tests (router.fetch())

Section titled “4. Zero-Socket HTTP Integration Tests (router.fetch())”

In vjs, vael:http lets you test routes, middleware, headers, cookies, and JSON payloads directly in memory via router.fetch() without binding a TCP port:

import assert from "vael:assert";
import { createRouter, json, recover, requestId } from "vael:http";
import test from "vael:test";
function buildApp() {
const router = createRouter();
router.use(recover(), requestId());
router.get("/items/{id}", (req, ctx) => {
return json({ id: req.param("id"), requestId: ctx.state.requestId });
});
return router;
}
test("GET /items/{id} returns route param and request ID", async () => {
const app = buildApp();
const res = await app.fetch("/items/42", {
headers: { "x-request-id": "test-req-1" },
});
assert.strictEquals(res.status, 200);
assert.equals(res.json(), { id: "42", requestId: "test-req-1" });
});

5. Temporary Directory Fixtures (vael:fs + vael:os/dirs)

Section titled “5. Temporary Directory Fixtures (vael:fs + vael:os/dirs)”

Build isolated per-test temp directories using tempDir (vael:os/dirs), v7 (vael:uuid/v7), join (vael:path), and mkdir / remove (vael:fs). This pattern works identically in both vjs and vjs-lite:

import assert from "vael:assert";
import { mkdir, readTextFile, remove, writeTextFile } from "vael:fs";
import { tempDir } from "vael:os/dirs";
import { join } from "vael:path";
import test from "vael:test";
import { v7 } from "vael:uuid/v7";
function withTempDir(fn: (dir: string) => void): void {
const dir = join(tempDir(), `vael-test-${v7()}`);
mkdir(dir, { recursive: true });
try {
fn(dir);
} finally {
remove(dir, { recursive: true });
}
}
test("writes and reads config inside isolated temp dir", () => {
withTempDir((dir) => {
const file = join(dir, "config.json");
writeTextFile(file, '{"ok":true}');
assert.strictEquals(readTextFile(file), '{"ok":true}');
});
});

6. Running Shared Suites on Both vjs and vjs-lite

Section titled “6. Running Shared Suites on Both vjs and vjs-lite”

Keep unit tests that exercise portable modules (vael:fs, vael:path, vael:crypto sync, vael:exec sync, vael:fetch sync, vael:ansi, vael:fmt, vael:template, vael:archive sync, vael:ssh, and sync node:* modules) free of vjs-only APIs and { timeout } options, then run the same files in CI on both engines:

Terminal window
vjs test "tests/shared/**/*.test.ts"
vjs-lite test "tests/shared/**/*.test.ts"