Skip to content

Test Module

The std/test module provides a minimal, opinionated testing framework with assertions and formatted reporting.

import { test } from "std/test"

test.assert(condition: any, message?: string): bool

Section titled “test.assert(condition: any, message?: string): bool”

Assert that a condition is truthy. Throws an error if the condition is false.

test.assert(1 + 1 == 2)
test.assert(value > 0, "Value must be positive")

test.assertEqual(actual: any, expected: any, message?: string): bool

Section titled “test.assertEqual(actual: any, expected: any, message?: string): bool”

Assert that two values are deeply equal. Throws an error if they differ.

test.assertEqual(add(2, 3), 5)
test.assertEqual(result, "hello", "Result should be hello")

test.assertNotEqual(actual: any, expected: any, message?: string): bool

Section titled “test.assertNotEqual(actual: any, expected: any, message?: string): bool”

Assert that two values are NOT equal. Throws an error if they are equal.

test.assertNotEqual(a, b, "Values should differ")

Returns the current time in milliseconds. Use this to measure test duration.

const start = test.now()
// ... run tests ...
const duration = test.now() - start

test.report(passed: int, failed: int, failures: []string, duration: int): int

Section titled “test.report(passed: int, failed: int, failures: []string, duration: int): int”

Print a formatted test report. Returns 0 if all tests passed, 1 if any failed.

const exitCode = test.report(passed, failed, failures, duration)

Output format:

══════════════════════════════════════
TEST RESULTS
──────────────────────────────────────
✓ Passed: 10
✗ Failed: 2
Duration: 45ms
──────────────────────────────────────
Failures:
1. Expected 5 but got 4
2. Value must be positive
══════════════════════════════════════
FunctionDescription
test.assert(condition, message?)Assert truthiness
test.assertEqual(actual, expected, message?)Assert deep equality
test.assertNotEqual(actual, expected, message?)Assert inequality
test.now()Current time in milliseconds
test.report(passed, failed, failures, duration)Print formatted test report
import { test } from "std/test"
var passed = 0
var failed = 0
var failures: []string = []
const start = test.now()
// Test 1: Addition
try {
test.assertEqual(1 + 1, 2)
passed++
} catch (e) {
failed++
failures = append(failures, e.message)
}
// Test 2: String concatenation
try {
test.assertEqual("hello" + " " + "world", "hello world")
passed++
} catch (e) {
failed++
failures = append(failures, e.message)
}
// Test 3: Array length
try {
const arr = [1, 2, 3]
test.assertEqual(len(arr), 3)
passed++
} catch (e) {
failed++
failures = append(failures, e.message)
}
// Test 4: Not equal
try {
test.assertNotEqual("a", "b")
passed++
} catch (e) {
failed++
failures = append(failures, e.message)
}
test.report(passed, failed, failures, test.now() - start)