Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Commit 470e3da

Browse files
Stringify errors before processing (#124)
* Stringify errors before processing * Add missing 2s * Fix missed merge conflict * Add some tests of error response * Remove unused imports/functions * Update tests/errorTypes.lua Co-authored-by: benj <[email protected]>
1 parent c410a91 commit 470e3da

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* Limitations:
99
* `expect.extend` cannot be called from within `describe` blocks
1010
* Custom matcher names cannot overwrite pre-existing matchers, including default matchers and matchers introduces from previous `expect.extend` calls.
11+
* Change the way errors are collected to call tostring on them before further processing.
12+
* Luau allows non-string errors, but not concatenating non-strings or passing non-strings to `debug.traceback` as a message, so TestRunner needs to do that step. This is a temporary fix as the better solution would be to retain the error in object form for as long as possible to give the reporter more to work with.
13+
* This also makes a slight change to what's in the traceback to eliminate the unnecessary line mentioning the error collection function.
1114

1215
## 0.3.3 (2020-09-25)
1316
* Remove the lifecycle hooks from the session tree. This prevents the `[?]` spam from the reporter not recognizing these nodes.

src/TestPlan.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ function TestNode:expand()
196196
end
197197
setfenv(self.callback, callbackEnv)
198198

199-
local success, result = xpcall(self.callback, debug.traceback)
199+
local success, result = xpcall(self.callback, function(message)
200+
return debug.traceback(tostring(message), 2)
201+
end)
200202

201203
if not success then
202204
self.loadError = result

src/TestRunner.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
7474
end
7575

7676
success = false
77-
errorMessage = messagePrefix .. message .. "\n" .. debug.traceback()
77+
errorMessage = messagePrefix .. debug.traceback(tostring(message), 2)
7878
end
7979

8080
testEnvironment.expect = wrapExpectContextWithPublicApi(session:getExpectationContext())
@@ -86,7 +86,7 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
8686
callback(context)
8787
end,
8888
function(message)
89-
return messagePrefix .. message .. "\n" .. debug.traceback()
89+
return messagePrefix .. debug.traceback(tostring(message), 2)
9090
end
9191
)
9292

tests/errorTypes.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local TestEZ = require(script.Parent.Parent.TestEZ)
2+
3+
local function check(str, test)
4+
local plan = TestEZ.TestPlanner.createPlan({
5+
{
6+
-- This function environment hack is needed because the test
7+
-- function is not defined or required from within a test. This
8+
-- shouldn't come up in real tests.
9+
method = function()
10+
setfenv(test, getfenv())
11+
test()
12+
end,
13+
path = {"errorTypeTests"}
14+
}
15+
})
16+
17+
local results = TestEZ.TestRunner.runPlan(plan)
18+
19+
assert(#results.errors > 0, "Expected some errors, got none.")
20+
for _, err in ipairs(results.errors) do
21+
local find = string.find(err, str)
22+
assert(find, string.format("Expected errors containing [%s], found [%s]", str, err))
23+
end
24+
end
25+
26+
return {
27+
["Error message should show up in output"] = function()
28+
check("FOO", function()
29+
error("FOO")
30+
end)
31+
end,
32+
["Erroring with an object should mention the object"] = function()
33+
check("table:", function()
34+
error({})
35+
end)
36+
end,
37+
["Erroring with an object with __tostring should show the string"] = function()
38+
check("FOO", function()
39+
local obj = setmetatable({}, {__tostring=function()
40+
return "FOO"
41+
end})
42+
error(obj)
43+
end)
44+
end,
45+
}

0 commit comments

Comments
 (0)