Skip to main content

JestUtils

Utility methods for writing Jest specs.

Functions

afterThis

JestUtils.afterThis(jobMaidTask) → () → ()--

Cancels the queued cleanup. Safe to call more than once.

Queues a maid task to be cleaned up once the current test finishes. Tasks unwind in reverse of the order they were queued, so teardown mirrors setup the way it would if each task were cleaned up right after the code it undoes.

it("reads back what it wrote", function()
	local store = DataStore.new()
	JestUtils.afterThis(store)

	local handle = store:GetHandle()
	JestUtils.afterThis(handle)

	expect(handle:Read()).toEqual(nil)
end)

Returns a function that removes the task from the queue again, so an object that outlives its own registration can disown the cleanup instead of being destroyed twice.

local maid = Maid.new()
maid:GiveTask(JestUtils.afterThis(maid))

Every queued task runs even if an earlier one throws, and the failures are reported together against the test that queued them. Tasks queued while the stack is unwinding run before the ones still below them.

Show raw api
{
    "functions": [
        {
            "name": "afterThis",
            "desc": "Queues a maid task to be cleaned up once the current test finishes. Tasks unwind in reverse of the\norder they were queued, so teardown mirrors setup the way it would if each task were cleaned up\nright after the code it undoes.\n\n```lua\nit(\"reads back what it wrote\", function()\n\tlocal store = DataStore.new()\n\tJestUtils.afterThis(store)\n\n\tlocal handle = store:GetHandle()\n\tJestUtils.afterThis(handle)\n\n\texpect(handle:Read()).toEqual(nil)\nend)\n```\n\nReturns a function that removes the task from the queue again, so an object that outlives its own\nregistration can disown the cleanup instead of being destroyed twice.\n\n```lua\nlocal maid = Maid.new()\nmaid:GiveTask(JestUtils.afterThis(maid))\n```\n\nEvery queued task runs even if an earlier one throws, and the failures are reported together\nagainst the test that queued them. Tasks queued while the stack is unwinding run before the ones\nstill below them.",
            "params": [
                {
                    "name": "job",
                    "desc": "",
                    "lua_type": "MaidTask"
                }
            ],
            "returns": [
                {
                    "desc": "Cancels the queued cleanup. Safe to call more than once.",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 78,
                "path": "src/jest-utils/src/Shared/JestUtils.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "JestUtils",
    "desc": "Utility methods for writing Jest specs.",
    "source": {
        "line": 7,
        "path": "src/jest-utils/src/Shared/JestUtils.lua"
    }
}