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

Stringify errors before processing #124

Merged
merged 8 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Limitations:
* `expect.extend` cannot be called from within `describe` blocks
* Custom matcher names cannot overwrite pre-existing matchers, including default matchers and matchers introduces from previous `expect.extend` calls.
* Change the way errors are collected to call tostring on them before further processing.
* 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.
* This also makes a slight change to what's in the traceback to eliminate the unnecessary line mentioning the error collection function.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


## 0.3.3 (2020-09-25)
* Remove the lifecycle hooks from the session tree. This prevents the `[?]` spam from the reporter not recognizing these nodes.
Expand Down
4 changes: 3 additions & 1 deletion src/TestPlan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ function TestNode:expand()
end
setfenv(self.callback, callbackEnv)

local success, result = xpcall(self.callback, debug.traceback)
local success, result = xpcall(self.callback, function(message)
return debug.traceback(tostring(message), 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Might be good to later move this debug.traceback snippet into its own modules for the sake of making sure we keep it DRY, esp since we're passing in "magic" scope

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably several such utility functions that could be pulled out.

end)

if not success then
self.loadError = result
Expand Down
4 changes: 2 additions & 2 deletions src/TestRunner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
end

success = false
errorMessage = messagePrefix .. message .. "\n" .. debug.traceback()
errorMessage = messagePrefix .. debug.traceback(tostring(message), 2)
end

testEnvironment.expect = wrapExpectContextWithPublicApi(session:getExpectationContext())
Expand All @@ -86,7 +86,7 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
callback(context)
end,
function(message)
return messagePrefix .. message .. "\n" .. debug.traceback()
return messagePrefix .. debug.traceback(tostring(message), 2)
end
)

Expand Down
45 changes: 45 additions & 0 deletions tests/errorTypes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local TestEZ = require(script.Parent.Parent.TestEZ)

local function check(str, test)
local plan = TestEZ.TestPlanner.createPlan({
{
-- This function environment hack is needed because the test
-- function is not defined or required from within a test. This
-- shouldn't come up in real tests.
method = function()
setfenv(test, getfenv())
test()
end,
path = {'errorTypeTests'}
}
})

local results = TestEZ.TestRunner.runPlan(plan)

assert(#results.errors > 0, "Expected some errors, got none.")
for _, err in ipairs(results.errors) do
local find = string.find(err, str)
assert(find, string.format("Expected errors containing [%s], found [%s]", str, err))
end
end

return {
["Error message should show up in output"] = function()
check("FOO", function()
error("FOO")
end)
end,
["Erroring with an object should mention the object"] = function()
check("table:", function()
error({})
end)
end,
["Erroring with an object with __tostring should show the string"] = function()
check("FOO", function()
local obj = setmetatable({}, {__tostring=function()
return "FOO"
end})
error(obj)
end)
end,
}