Docs · Standard library

@testing

@testing holds the functions a test file is written with: describe and it for structure, expect and its matchers for checks, and beforeEach and afterEach for setup. vyi test runs the files and prints the report. Guide 16, Testing teaches them, and the CLI reference covers vyi test.

Vyi
import { describe, it, expect } from "@testing"
import { beforeEach, afterEach, failures } from "@testing"
import { Expectation } from "@testing"

Exports

NameKindSummary
describefunctiona named group of tests
itfunctionone named test
expectfunctionopens a check on a value
Expectation(T)typewhat expect returns; the matchers are its methods
toBe, toEqual, toBeTrue, toBeFalsemethods on Expectation(T)the matchers
beforeEach, afterEachfunctionsa closure run before or after each following test
failuresfunctionthe number of failed checks so far

Test files

A test file's name ends in .tests.vyi. It holds declarations and top-level calls such as describe(...) and it(...), and no main. vyi test adds a main that makes the top-level calls in the order they are written and then exits with failures(), capped at 255. A test file that declares its own main is run as it is.

Everything runs in one program, in order: there is no isolation between tests, and a file-level mut variable keeps its value from one test to the next.

Vyi · test file
import { describe, it, expect } from "@testing"

fn discount(total: i32) i32 {
    if total >= 100 { return 10 }
    return 0
}

describe("discount", () => {
    it("is 10 from 100", () => {
        expect(discount(150)).toBe(10)
    })
    it("is 0 below 100", () => {
        expect(discount(99)).toBe(0)
    })
})

describe and it

FunctionSignatureDoes
describefn describe(name: [*con]u8, body: fn())runs body at once, one level deeper; the tests inside are reported under name
itfn it(name: [*con]u8, body: fn())runs the beforeEach closure, body, and the afterEach closure, then reports the test
  • A test passes when no check inside body failed. A failed check does not stop the test; the rest of body runs.
  • A failed test points at its last failed check, as file:line:column of the expect call.
  • describe calls nest to any depth. The report names a test by its path, as in discount > is 10 from 100.
  • body is a closure with no parameters and no result (guide 04).
  • A check made outside any it counts in failures() but belongs to no test. When it fails, the file fails with no failing test to show.
  • A panic stops the whole file: the tests after it do not run, and the file is reported as failed.

expect and the matchers

fn expect(actual: _) Expectation(typeof actual) takes a value of any type. Expectation(T) is struct { pub actual: T }, and the matchers are its methods:

MatcherSignaturePasses when
toBefn (e: Expectation(T)) toBe(expected: T)actual == expected
toEqualfn (e: Expectation(T)) toEqual(expected: T)actual == expected; the same check as toBe
toBeTruefn (e: Expectation(T)) toBeTrue()actual == true; T must be bool
toBeFalsefn (e: Expectation(T)) toBeFalse()actual == false; T must be bool
  • expected has the same type as actual. A literal takes that type, so expect(i64(5)).toBe(5) compiles; expect(5).toBe(i64(5)) is reported as argument 1 has type i64, expected i32.
  • T needs ==. Numbers, bool, char, [*con]u8 (by content), String, enums, optionals, and results have it. A struct needs an @eq method (operators); without one, toBe is reported inside @testing as `operator == is not implemented for type Point`.
  • toBeTrue() on a non-bool value is reported inside @testing as `this operand of == has type bool, where i32 is expected (for an i32`).
  • A failure does not print the values. Only the location is reported.
Vyi · test file
import { it, expect } from "@testing"

enum StockError { OutOfStock }

fn reserve(inStock: i32, wanted: i32) StockError!i32 {
    if wanted > inStock { return Err(OutOfStock) }
    return Ok(inStock - wanted)
}

it("reserves what is in stock", () => {
    expect(reserve(5, 2)).toBe(Ok(3))
    expect(reserve(1, 2)).toEqual(Err(OutOfStock))
    expect(reserve(5, 5) == Ok(0)).toBeTrue()
})

beforeEach and afterEach

FunctionSignatureDoes
beforeEachfn beforeEach(body: fn())sets the closure that every following it runs before its body
afterEachfn afterEach(body: fn())sets the closure that every following it runs after its body
  • There is one of each at a time. A second beforeEach replaces the first; it does not add to it. A beforeEach in a nested describe replaces the outer one for the tests in that group.
  • A hook ends with its describe. When a describe returns, the hooks that were set before it apply again. Tests in a nested describe use the enclosing group's hooks until the group sets its own.
  • A hook set at the top level applies to every following test outside a group that sets its own.
  • There is no beforeAll or afterAll: write that code before or after the calls it surrounds.
Vyi · test file
import { describe, it, expect, beforeEach } from "@testing"

mut cartItems = 0

describe("cart", () => {
    beforeEach(() => { cartItems = 0 })

    it("adds an item", () => {
        cartItems += 1
        expect(cartItems).toBe(1)
    })

    it("starts empty", () => {
        expect(cartItems).toBe(0)
    })
})

failures

fn failures() i32 returns how many checks have failed so far in the program, across every test. The main that vyi test adds exits with it. A test file with its own main returns it to report failures the same way:

Vyi · test file
import { it, expect, failures } from "@testing"

fn main() !u8 {
    it("adds", () => {
        expect(2 + 3).toBe(5)
    })
    return Ok(u8(failures()))
}

See also: guide 16, Testing, the CLI reference for vyi test, and @trace, which @testing uses to find a failed check's location.