zena:test

zena
import {…} from 'zena:test';

Test framework for structuring and running test suites.

Provides suite and test functions to define hierarchical test suites, executed through the Zena CLI test runner (zena test).

A test's body is synchronous, or async: an asynchronous body is awaited before the next test starts, and a failure anywhere in it, before or after an await, is the test's failure. Tests run one at a time, in the order they were registered.

Examples ​

zena
import { suite, test, TestContext } from 'zena:test';
import { equal } from 'zena:assert';
import { readFile } from 'zena:fs';

export let tests = suite('Math', () => {
  test('addition', (ctx: TestContext) => {
    equal(1 + 1, 2);
  });

  test('a file', async (ctx: TestContext) => {
    equal(await readFile('two.txt'), '2');
  });
});

Classes

TestContext

zena
class TestContext

Test context passed to each test function

Constructors
zena
new(name: String)
#
Properties
zena
name: String
#

TestResult

zena
class TestResult

Result of a single test execution

Constructors
zena
new()
#
Properties
zena
name: String
#
zena
passed: boolean
#
zena
error: String | null
#

SuiteResult

zena
class SuiteResult
Constructors
zena
new()
#
Properties
zena
name: String
#
zena
tests
#
zena
suites
#
zena
var passed
#
zena
var failed
#
Methods
zena
addTestResult(result: TestResult): void
#
zena
addSuiteResult(result: SuiteResult): void
#

Suite

zena
class Suite

A test suite containing tests and nested suites

Constructors
zena
new()
#
Properties
zena
name: String
#
zena
tests
#
zena
suites
#
zena
var hasOnly
#
Methods
zena
async run(): Future<SuiteResult>
#

Runs the suite's tests, then its nested suites, one at a time and in order, and returns what each did.

Functions

test

zena
function test(name: String, fn: (ctx: TestContext) => void): void

Register a test in the current suite

test

zena
function test(name: String, fn: (ctx: TestContext) => Future<void>): void

Register a test whose body is asynchronous. The next test starts once the body's future has settled.

testOnly

zena
function testOnly(name: String, fn: (ctx: TestContext) => void): void

Register a test that will be the only one to run (for debugging)

testOnly

zena
function testOnly(name: String, fn: (ctx: TestContext) => Future<void>): void

testSkip

zena
function testSkip(name: String, fn: (ctx: TestContext) => void): void

Register a test that will be skipped

testSkip

zena
function testSkip(name: String, fn: (ctx: TestContext) => Future<void>): void

suite

zena
function suite(name: String, fn: () => void): Suite

Create a test suite

runAndReport

zena
async function runAndReport(s: Suite, write: (s: String) => void): Future<i32>

Run a suite, print results, and return the number of failures. This is the standard entry point for test runners.

Usage in a test wrapper: import { tests } from './my_test.zena'; import { runAndReport } from 'zena:test'; export async function main(): Future { return await runAndReport(tests, console.log); }