Skip to content

types

luau
local types = require("@std/test/types")

WARNING

These APIs are still open to future evolution. In new major versions, they may change in backwards incompatible ways.

Summary

EntryDescription
Asserts
CaseIndexEntryAn index entry mapping a test case name to all anonymous instances and suite instances with that name.
FailedTestA test case that failed, paired with the failure that caused it.
FailureDescribes why a test failed:
HookThe name of a lifecycle hook that can run around test cases:
NegatedAssertsThe assertion functions passed to each test case. Each assertion returns a Failure on failure, or nil on success.
TestA test function that receives the assertion helpers and runs one test case.
TestCaseA named test case pairing a name with its test function.
TestEnvironmentThe full set of discovered tests: anonymous cases, suites, and lookup indexes by name.
TestReporterA function that receives a TestRunResult and reports it (e.g. prints to stdout).
TestRunResultThe aggregate results of a test run: total, passed, failed counts and the list of failures.
TestRunnerA function that runs a TestEnvironment and returns the results.
TestRunnerOptions
TestSuiteA named collection of test cases with optional lifecycle hooks.

Types

Asserts

luau
type Asserts = {
	eq: <T>(T, T, string?) -> Failure?,
	neq: <T>(T, T, string?) -> Failure?,
	throws: <T...>((T...) -> ...unknown, T...) -> Failure?,
	throwsWith: <T...>(any, (T...) -> ...unknown, T...) -> Failure?,
	strContains: (string, string, number?, string?) -> Failure?,
	strNotContains: (string, string, string?) -> Failure?,
	that: (unknown, string?) -> Failure?,
	never: NegatedAsserts,
}

CaseIndexEntry

An index entry mapping a test case name to all anonymous instances and suite instances with that name.

luau
type CaseIndexEntry = {
	anonymous: { TestCase },
	suites: { [string]: { TestCase } },
}

FailedTest

A test case that failed, paired with the failure that caused it.

luau
type FailedTest = {
	test: string, -- name of the test case that failed
	failure: Failure,
}

Failure

Describes why a test failed:

  • "assertion": An assertion (e.g. t.eq) failed. Includes the assertion name, message, filename, and line number.

  • "lifecycle": A lifecycle hook threw an error. Includes the hook name, message, filename, and line number.

  • "runtime_error": The test body threw an unhandled error. Includes the message and stack trace.

luau
type Failure =

Hook

The name of a lifecycle hook that can run around test cases:

  • "beforeEach": Runs before each test case in the suite.

  • "beforeAll": Runs once before all test cases in the suite.

  • "afterEach": Runs after each test case in the suite.

  • "afterAll": Runs once after all test cases in the suite.

luau
type Hook = "beforeEach" | "beforeAll" | "afterEach" | "afterAll"

NegatedAsserts

The assertion functions passed to each test case. Each assertion returns a Failure on failure, or nil on success.

luau
type NegatedAsserts = {
	eq: <T>(T, T, string?) -> Failure?,
	throws: <T...>((T...) -> ...unknown, T...) -> Failure?,
	throwsWith: <T...>(any, (T...) -> ...unknown, T...) -> Failure?,
	strContains: (string, string, number?, string?) -> Failure?,
	that: (unknown, string?) -> Failure?,
}

Test

A test function that receives the assertion helpers and runs one test case.

luau
type Test = (Asserts) -> ()

TestCase

A named test case pairing a name with its test function.

luau
type TestCase = { name: string, case: Test }

TestEnvironment

The full set of discovered tests: anonymous cases, suites, and lookup indexes by name.

luau
type TestEnvironment = {
	anonymous: { TestCase },
	suites: { TestSuite },
	suiteindex: { [string]: TestSuite },
	caseindex: { [string]: CaseIndexEntry },
}

TestReporter

A function that receives a TestRunResult and reports it (e.g. prints to stdout).

luau
type TestReporter = (TestRunResult) -> ()

TestRunResult

The aggregate results of a test run: total, passed, failed counts and the list of failures.

luau
type TestRunResult = {
	failures: { FailedTest },
	total: number,
	failed: number,
	passed: number,
}

TestRunner

A function that runs a TestEnvironment and returns the results.

luau
type TestRunner = (TestEnvironment, TestRunnerOptions?) -> TestRunResult

TestRunnerOptions

luau
type TestRunnerOptions = {
	verbose: boolean,
}

TestSuite

A named collection of test cases with optional lifecycle hooks.

luau
type TestSuite = {
	name: string,
	cases: { TestCase },
	_beforeEach: (() -> ())?,
	_beforeAll: (() -> ())?,
	_afterEach: (() -> ())?,
	_afterAll: (() -> ())?,
	case: (self: TestSuite, string, Test) -> (),
	beforeEach: (self: TestSuite, () -> ()) -> (),
	beforeAll: (self: TestSuite, () -> ()) -> (),
	afterEach: (self: TestSuite, () -> ()) -> (),
	afterAll: (self: TestSuite, () -> ()) -> (),
}