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

Add suite & case hooks support #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/TestBootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function TestBootstrap:run(roots, reporter, otherOptions)
local plan = TestPlanner.createPlan(modules, testNamePattern, extraEnvironment)
local afterPlan = tick()

local results = TestRunner.runPlan(plan)
local results = TestRunner.runPlan(plan, otherOptions)
local afterRun = tick()

reporter.report(results)
Expand Down
21 changes: 17 additions & 4 deletions src/TestRunner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
Runs the given TestPlan and returns a TestResults object representing the
results of the run.
]]
function TestRunner.runPlan(plan)
function TestRunner.runPlan(plan, options)
local session = TestSession.new(plan)
local lifecycleHooks = LifecycleHooks.new()

Expand All @@ -42,7 +42,7 @@ function TestRunner.runPlan(plan)

session.hasFocusNodes = #exclusiveNodes > 0

TestRunner.runPlanNode(session, plan, lifecycleHooks)
TestRunner.runPlanNode(session, plan, lifecycleHooks, options)

return session:finalize()
end
Expand All @@ -51,7 +51,8 @@ end
Run the given test plan node and its descendants, using the given test
session to store all of the results.
]]
function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
function TestRunner.runPlanNode(session, planNode, lifecycleHooks, options)
options = options or {}
local function runCallback(callback, messagePrefix)
local success = true
local errorMessage
Expand Down Expand Up @@ -113,7 +114,13 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
end
end

if options.onEnterCase then
options.onEnterCase(childPlanNode.phrase)
end
local testSuccess, testErrorMessage = runCallback(childPlanNode.callback)
if options.onLeaveCase then
options.onLeaveCase(childPlanNode.phrase, not testSuccess and testErrorMessage or nil)
end

for _, hook in ipairs(lifecycleHooks:getAfterEachHooks()) do
local success, errorMessage = runCallback(hook, "afterEach hook: ")
Expand Down Expand Up @@ -161,7 +168,13 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
session:popNode()
elseif childPlanNode.type == TestEnum.NodeType.Describe then
session:pushNode(childPlanNode)
TestRunner.runPlanNode(session, childPlanNode, lifecycleHooks)
if options.onEnterSuite then
options.onEnterSuite(childPlanNode.phrase)
end
TestRunner.runPlanNode(session, childPlanNode, lifecycleHooks, options)
if options.onLeaveSuite then
options.onLeaveSuite(childPlanNode.phrase)
end

-- Did we have an error trying build a test plan?
if childPlanNode.loadError then
Expand Down
54 changes: 54 additions & 0 deletions tests/e2e/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,58 @@ return {
testNamePattern = "specificFileName",
})
end,

["suiteAndCaseHooks (onEnterSuite, onEnterCase, onLeaveCase, onLeaveSuite)"] = function()
local events = {}
local function eventAppender(eventType)
return function(...)
table.insert(events, {eventType, ...})
end
end

TestEZ.TestBootstrap:run({
script:FindFirstChild("suiteAndCaseHooks"),
},
noOptReporter,
{
onEnterSuite = eventAppender("onEnterSuite"),
onLeaveSuite = eventAppender("onLeaveSuite"),
onEnterCase = eventAppender("onEnterCase"),
onLeaveCase = eventAppender("onLeaveCase")
})

assert(#events == 10)

assert(events[1][1] == "onEnterSuite")
assert(events[1][2] == "suiteAndCaseHooks")

assert(events[2][1] == "onEnterSuite")
assert(events[2][2] == "My suite")

assert(events[3][1] == "onEnterCase")
assert(events[3][2] == "My nested case")

assert(events[4][1] == "onLeaveCase")
assert(events[4][2] == "My nested case")

assert(events[5][1] == "onLeaveSuite")
assert(events[5][2] == "My suite")

assert(events[6][1] == "onEnterCase")
assert(events[6][2] == "My case")

assert(events[7][1] == "onLeaveCase")
assert(events[7][2] == "My case")

assert(events[8][1] == "onEnterCase")
assert(events[8][2] == "My failing case")

assert(events[9][1] == "onLeaveCase")
assert(events[9][2] == "My failing case")
-- The error from the failing case
assert(type(events[9][3]) == "string")

assert(events[10][1] == "onLeaveSuite")
assert(events[10][2] == "suiteAndCaseHooks")
end,
}
13 changes: 13 additions & 0 deletions tests/e2e/suiteAndCaseHooks/init.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return function()
describe("My suite", function()
it("My nested case", function()
end)
end)

it("My case", function()
end)

it("My failing case", function()
error("My failure")
end)
end