diff --git a/src/TestBootstrap.lua b/src/TestBootstrap.lua index e3641a5..892024b 100644 --- a/src/TestBootstrap.lua +++ b/src/TestBootstrap.lua @@ -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) diff --git a/src/TestRunner.lua b/src/TestRunner.lua index 2ccff81..04191cc 100644 --- a/src/TestRunner.lua +++ b/src/TestRunner.lua @@ -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() @@ -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 @@ -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 @@ -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: ") @@ -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 diff --git a/tests/e2e/init.lua b/tests/e2e/init.lua index 13f5e34..8f0f716 100644 --- a/tests/e2e/init.lua +++ b/tests/e2e/init.lua @@ -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, } diff --git a/tests/e2e/suiteAndCaseHooks/init.spec.lua b/tests/e2e/suiteAndCaseHooks/init.spec.lua new file mode 100644 index 0000000..e312fe5 --- /dev/null +++ b/tests/e2e/suiteAndCaseHooks/init.spec.lua @@ -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 \ No newline at end of file