Guides · 16

Testing

Tests in Vyi are ordinary Vyi code. The @testing package gives you three functions, describe, it, and expect, and the vyi test command finds your test files, runs them, and prints a report.

A first test

A test file is a .vyi file whose name ends in .tests.vyi. Here is price.tests.vyi, with a function and two tests for it:

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

fn lineTotal(price: i32, quantity: i32) i32 {
    return price * quantity
}

it("multiplies price by quantity", () => {
    expect(lineTotal(5, 3)).toBe(15)
})

it("is zero for no items", () => {
    expect(lineTotal(5, 0)).toBe(0)
})
  • it(name, body) runs one test. The body is a closure with no parameters (guide 04).
  • expect(value) takes a value of any type. A matcher method on the result, such as toBe(15), checks it.
  • A test file has no main. vyi test writes one for it that makes the top-level calls in order.

Run vyi test in the folder that holds the file:

sh
vyi test
text
 ✓ price.tests.vyi (2 tests)
 ✓ multiplies price by quantity
 ✓ is zero for no items

 Test Files  1 passed (1)
      Tests  2 passed (2)

In a real project, lineTotal lives in its own file, and the test file next to it imports it with import { lineTotal } from "./price".

When a test fails

Change the first expectation to toBe(16) and run the tests again:

text
 ❯ price.tests.vyi (2 tests | 1 failed)
 × multiplies price by quantity
 ✓ is zero for no items

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  price.tests.vyi > multiplies price by quantity
 ❯ price.tests.vyi:8:5

 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)

price.tests.vyi:8:5 is the line and column of the expect that failed. A failed check doesn't stop the test: the rest of its body still runs, and the test fails if any check in it failed. When several checks fail, the report points at the last one.

Matchers

expect(value) has four matchers:

  • toBe(expected) passes when value == expected. It works for any type that has ==: numbers, bool, characters, strings, enums, optionals and results, and your own types once you give them an @eq method (guide 13).
  • toEqual(expected) compares with == too, the same as toBe.
  • toBeTrue() and toBeFalse() check a bool.

A result compares like any other value, so you test a failure by naming the Err you expect:

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("returns what is left", () => {
    expect(reserve(5, 2)).toBe(Ok(3))
})

it("fails when there is not enough", () => {
    expect(reserve(1, 2)).toBe(Err(OutOfStock))
})

Grouping tests with describe

describe(name, body) groups related tests. Groups can nest, and the report indents each test by how deeply it's nested:

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

fn shippingCost(total: i32) i32 {
    if total >= 50 { return 0 }
    return 5
}

describe("shippingCost", () => {
    it("charges 5 below 50", () => {
        expect(shippingCost(20)).toBe(5)
    })
    describe("free shipping", () => {
        it("starts at 50", () => {
            expect(shippingCost(50)).toBe(0)
        })
    })
})
text
 ✓ shipping.tests.vyi (2 tests)
   ✓ charges 5 below 50
     ✓ starts at 50

 Test Files  1 passed (1)
      Tests  2 passed (2)

When a test inside a group fails, the report names it by its full path, such as shippingCost > free shipping > starts at 50.

Resetting state with beforeEach

beforeEach(body) registers a closure that runs before every following test in the same describe. afterEach(body) registers one that runs after each test. Use them to put shared state back to a known starting point. The state lives in a file-level mut variable, so every test sees the same one:

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

mut itemCount = 0

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

    it("counts an added item", () => {
        itemCount += 1
        expect(itemCount).toBe(1)
    })

    it("starts each test empty", () => {
        expect(itemCount).toBe(0)
    })
})

Without the beforeEach, the second test would see the 1 the first test left behind. A hook belongs to its describe: when the group ends, the hooks from before it apply again, so a sibling group doesn't inherit them.

Running vyi test

vyi test searches the current folder and every folder below it for files whose names end in .tests.vyi. Name a folder to search there instead:

sh
vyi test src

Each test file is compiled and run as its own program. A file that doesn't compile, or that stops partway with a panic, counts as a failed file, and the report says so.

vyi test exits with the number of test files that failed, so 0 means every test passed. That makes it usable as a check in a build script or CI job.

Tests run on the default wasm target. Flags go before the folder name; to run the same tests as a native program, pass a target:

sh
vyi test --target x86-64-linux src

See also: guide 17 covers the rest of the vyi command.