Skip to content

Tests ​

A test is a block at the top level of any file: test "name" { … }. Its body may await. Tests are compiled only by weft test; they never ship.

lib/todos.weft
weft
import { parse } from "std/json"
import { assertEq, assertPanics } from "std/testing"

export type Todo = { id: string, title: string, done: boolean }

export fn open(todos: Todo[]): Todo[] {
  return todos.filter(t => !t.done)
}

export fn read(text: string): Todo[] {
  let j = try parse(text)
  return try j as Todo[]
}

test "open leaves out done todos" {
  let todos = [Todo { id: "a", title: "x", done: false }, Todo { id: "b", title: "y", done: true }]
  assertEq(open(todos).length, 1)
  assertPanics(() => { let first = open(todos)[1] }, "RangeError")
}

test "read takes a list" {
  let todos = try read(`[{ "id": "a", "title": "x", "done": false }]`)
  assertEq(todos[0].title, "x")
}

The helpers come from std/testing: assert(cond, message?), assertEq(actual, expected) (structural, with a diff) and assertPanics(f, name?), which checks that f panics, with that name when one is given. A failed assertion is a panic named AssertionError, and it fails the test. A test's body may pass errors on with try: if read fails, the test fails and reports the error.

std/testing is test-only. A file with tests may import it, but only the tests may use it:

rejected by the compiler (E1019)
weft
import { assert } from "std/testing"

export fn half(n: number): number {
  assert(n % 2 == 0)
  return n / 2
}

test "half" {
  assert(half(4) == 2)
}

Shipped code checks with if and fail, for what a caller should handle (Errors).

Components ​

mount(Component, props) mounts a component on the deterministic test host and returns a handle. Its methods run synchronously: each does its action as one turn, then runs every turn that is ready before it returns.

components/Counter.weft
weft
import { setTimeout } from "std/time"

let mut n = 0
fn add() { setTimeout(() => n += 1, 10) }
---
<Pressable testId="add" onpress={add}><Text>{n}</Text></Pressable>
lib/counter.weft
weft
import { assertEq, mount } from "std/testing"
import Counter from "../components/Counter"

test "the counter counts late" {
  let view = mount(Counter, {})
  view.click("add")
  view.advance(9)
  assertEq(view.text(), "0")
  view.advance(1)
  assertEq(view.text(), "1")
}

Elements are found by their test id (testId="add"). view.type(id, text) types into a text field, view.advance(ms) moves the clock, view.resolveTask(call, value) and view.rejectTask(call, message) answer the host calls the component made (named like "store.get", or a server function's name), view.pending() lists them, and view.unmount() unmounts it. rejectTask fails the call: a lib call with a HostError { name: "Error", message }, a server function with a NetworkError { message }, so a test can check how the component handles a failure.

Replacing modules ​

A module that holds a resource (a database connection) can be swapped for the whole test run in weft.toml:

toml
[test.replace]
"lib/server/db" = "lib/server/db.test"

Every importer then gets lib/server/db.test, which must export everything lib/server/db does.

Running them ​

sh
weft test hello              # every test of the project
weft test hello --name sum   # the tests whose name has "sum"
weft test hello --list       # list them without running
text
ok    lib/summary.weft:8: the summary counts open notes
1 passed, 0 failed

weft test checks the project as test builds compile it (std/testing there, weft.toml's [test.replace] applied) and runs its tests on the VM's deterministic test host. A failing test shows what was expected, what came, and the line:

text
FAIL  lib/sum.weft:1: sums
      lib/sum.weft:2: AssertionError: assertEq failed: expected 3, got 2
        value: expected 3, got 2
         2 |   assertEq([1, 2].length, 3)
0 passed, 1 failed

Weft is in development. The reference is the spec as it stands today.