Skip to content

Commit 1934ced

Browse files
authored
Add errors assertion (#444)
This is a super common assertion and something I wanted to use in a project I was working on so figure'd I would add it. Not sure what the larger vision is for testing API, but hopefully this fits somewhere. Can also add an optional `expectedError` param
1 parent 93c35ac commit 1934ced

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

examples/testing.luau

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ test.suite("MySuite", function(suite)
5050
assert.tableeq(table1, table2)
5151
assert.tableeq(table1, { a = 1 })
5252
end)
53+
54+
suite:case("expect_error", function(assert)
55+
local function foo(v: boolean)
56+
if v then
57+
error("error")
58+
end
59+
end
60+
61+
assert.errors(function() -- demonstrates success (callback throws err)
62+
foo(true)
63+
end)
64+
assert.errors(function() -- demonstrates failure (callback doesn't throw)
65+
foo(false)
66+
end)
67+
end)
5368
end)
5469

5570
test.run()

lute/std/libs/assert.luau

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function assertions.neq<T>(lhs: T, rhs: T)
1212
end
1313
end
1414

15+
function assertions.errors(callback: () -> ())
16+
local success = pcall(callback)
17+
if success then
18+
error({ msg = `{callback} did not throw error.` })
19+
end
20+
end
21+
1522
-- Helper to format values for error messages
1623
local function formatValue(val: any): string
1724
if type(val) == "nil" then

0 commit comments

Comments
 (0)