Bree

Tests

A rule that type checks can still be wrong: > where it should be >=, a limit in the wrong unit, a country missing from a table. Tests catch those, and Bree runs them everywhere the rules are loaded.

Writing tests

Tests go in files ending in .test.luau, usually beside the rule: free_shipping.test.luau for free_shipping.luau. A test file requires the rules and modules it tests, with the same relative paths rules use:

--!strict
local free_shipping = require("./free_shipping")

test("US orders ship free from $50", function()
	expect(free_shipping({ order_total_cents = 5000, country = "US" })).toEqual({ free = true })
end)

test("Canadian orders under $75 pay for shipping", function()
	expect(free_shipping({ order_total_cents = 7499, country = "CA" }).free).toBe(false)
end)

test("a negative total is rejected", function()
	expect(function()
		free_shipping({ order_total_cents = -1, country = "US" })
	end).toBeInvalid("order_total_cents")
end)
Passes when
test(name, body) declares a test; body is a function
expect(value).toBe(expected) value == expected
expect(value).toEqual(expected) value has the same contents as expected, tables compared field by field
expect(fn).toBeInvalid(field) calling fn makes a rule reject its input at field, with invalid or because the input does not match the rule's Input type

Calling a rule from a test goes through the same checks as a call from your application: the input is checked against Input and the output against Output. A test that passes a fractional number for an int field fails, as the application's call would.

Write a test for each boundary: exactly at a limit and one past it. Those are the cases a > for >= swap gets wrong.

Tests are type checked

Test files are checked in strict mode like rules are, against the real types of the rules they call. A test cannot pass an input the rule does not accept or expect an output it cannot return:

rules/free_shipping.test.luau:5: Expected this to be '"CA" | "US"', but got '"MX"'
rules/free_shipping.test.luau:5: Expected this to be 'boolean', but got 'string'

Where tests run

When
bree check on demand, and in CI
cargo build bree::rules! runs every test while the crate compiles
go generate bree-gen runs every test before it writes Go code
every engine when an application loads the rules, and on every reload

The last row matters if you load rules from a directory at run time (Reloading): a change whose tests fail is refused, and the application keeps the rules it has.

Reading a failure

Each failure names the file and line of the test, the test's name, and what went wrong:

rules/more.test.luau:5: test "toBe on a field" failed
    expected false, got true
rules/more.test.luau:11: test "rejects zero" failed
    expected invalid input at order_total_cents, but the rule made a decision
rules/more.test.luau:17: test "wrong field" failed
    expected invalid input at country, got invalid input at order_total_cents: must not be negative
rules/more.test.luau:21: test "fractional cents" failed
    invalid_input: order_total_cents: must be a whole number, got 10.5

toEqual failures print both values in Luau syntax, keys sorted:

rules/shipment_eligibility.test.luau:29: test "a parcel exactly at the weight limit ships" failed
    expected { eligible = true, reason = "eligible" }
         got { eligible = false, reason = "overweight" }

Tests run in the same sandbox as rules, under the same step, memory and time limits.

Edit this page on GitHub