Skip to content
Untitled Sandbox - Developer Documentation
docs/api/test

@usbx/test

Unit tests

local test = require("@usbx/test")

test.describe

function

Groups tests under a name. The body runs at once, declares tests, hooks and other groups, and must not yield.

test.describe(name: string, body: () -> ()) -> ()
example
test.describe("Inventory", function() end)

Use it forgrouping all the tests for your inventory module together under one name.

nameWhat the tests inside have in common.
bodyDeclares the tests and hooks of the group. It must not yield.

test.it

functionwaits

Declares a test. Its body runs later in its own thread and can yield, until its timeout (5 s of script time by default).

test.it(name: string, body: () -> (), options: TestOptions?) -> ()
example
test.it("adds two numbers", function() test.expect(1 + 1):to_be(2) end)

Use it forchecking one thing, like whether your shop module charges the right price.

nameWhat the test checks, as a sentence.
bodyThe test. It runs in its own thread and can yield.
optionsoptionalThe timeout, in seconds of script time.
Build a it calltimeout
  • timeoutdefault 5

    How long the test may take in seconds of script time, which must be above 0 and at most 60.

test.before_each

function

Runs a function before each test of this group and of the groups inside it, outer groups first.

test.before_each(hook: () -> ()) -> ()
example
test.before_each(function() print("starting a test") end)

Use it forsetting up fresh state, such as an empty inventory, before each test.

hookRuns before each test; it can yield.

test.after_each

function

Runs a function after each test of this group and of the groups inside it, also after a failed test, inner groups first.

test.after_each(hook: () -> ()) -> ()
example
test.after_each(function() print("a test ended") end)

Use it forcleaning up the parts a test spawned, even when it failed, so the next test starts clean.

hookRuns after each test, also a failed one.

test.expect

function

Starts checking a value: test.expect(value):to_be(expected).

test.expect(value: any) -> Expectation
example
test.expect(math.floor(2.5)):to_be(2)

Use it forchecking a result inside a test, like the damage a hit deals or the price of an item.

valueThe value to check.
↑ ↓ moveEnter openEsc close