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

Commit 3a60c23

Browse files
authored
Merge pull request #149 from ConMur/run-afterEach-when-it-fails
Run code in the afterEach block after failure in an it block
2 parents 4bee8e3 + 6db50ae commit 3a60c23

File tree

8 files changed

+83
-7
lines changed

8 files changed

+83
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# TestEZ Changelog
22

33
## Unreleased Changes
4+
* `afterEach` blocks now run their code after `it` blocks fail or error
45

56

67
## 0.4.0 (2020-10-02)

docs/api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ afterEach(callback(context: table))
135135
```
136136

137137
Returns a function after each of the tests within its scope. This is useful if you want to cleanup some temporary state that is created by each test.
138+
It is always ran regardless of if the test failed or not.
138139

139140
```lua
140141
local DEFAULT_STATE = {

src/TestRunner.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,22 @@ function TestRunner.runPlanNode(session, planNode, lifecycleHooks)
113113
end
114114
end
115115

116-
do
117-
local success, errorMessage = runCallback(childPlanNode.callback)
118-
if not success then
119-
return false, errorMessage
120-
end
121-
end
116+
local testSuccess, testErrorMessage = runCallback(childPlanNode.callback)
122117

123118
for _, hook in ipairs(lifecycleHooks:getAfterEachHooks()) do
124119
local success, errorMessage = runCallback(hook, "afterEach hook: ")
125120
if not success then
121+
if not testSuccess then
122+
return false, testErrorMessage .. "\nWhile cleaning up the failed test another error was found:\n" .. errorMessage
123+
end
126124
return false, errorMessage
127125
end
128126
end
129127

128+
if not testSuccess then
129+
return false, testErrorMessage
130+
end
131+
130132
return true, nil
131133
end
132134

test/roblox-cli.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ else
1515
fi
1616

1717
rojo build test-place.project.json -o TestPlace.rbxlx
18-
roblox-cli run --load.place TestPlace.rbxlx --assetFolder "$CONTENT"
18+
roblox-cli run --load.place TestPlace.rbxlx --assetFolder "$CONTENT"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
return function()
2+
describe("When an error occurs in an afterEach block", function()
3+
it("Should pass", function()
4+
expect(true).to.equal(true)
5+
end)
6+
7+
afterEach(function()
8+
-- Cause a failure to be picked up by the test harness
9+
error("afterEach threw an error as expected")
10+
end)
11+
end)
12+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
return function()
2+
describe("When an error occurs in an it block", function()
3+
it("Should error", function()
4+
error("Failure in it block")
5+
end)
6+
7+
afterEach(function()
8+
-- Cause an error to be picked up by the test harness
9+
error("afterEach threw an error as expected")
10+
end)
11+
end)
12+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
return function()
2+
describe("When a failure occurs in an it block", function()
3+
it("Should fail", function()
4+
fail("Failure in it block")
5+
end)
6+
7+
afterEach(function()
8+
-- Cause an error to be picked up by the test harness
9+
error("afterEach threw an error as expected")
10+
end)
11+
end)
12+
end

tests/e2e/init.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@ return {
1414
end)
1515
end,
1616

17+
["run afterEach e2e with error in it block"] = function()
18+
TestEZ.run(script:FindFirstChild("afterEachAfterFailure"):FindFirstChild("errorInItBlock"), function(results)
19+
assert(#results.errors == 1, "Expected one error, got " .. tostring(#results.errors))
20+
21+
local afterEachError = string.find(results.errors[1], "afterEach threw an error as expected")
22+
assert(afterEachError ~= nil, "Expected afterEach to be reached after it throws an error")
23+
24+
local cleaningUpError = string.find(results.errors[1], "While cleaning up the failed test another error was found")
25+
assert(cleaningUpError ~= nil, "Expected 'While cleaning up...' msg to be shown when afterEach fails after it fails")
26+
end)
27+
end,
28+
29+
["run afterEach e2e with failure in it block"] = function()
30+
TestEZ.run(script:FindFirstChild("afterEachAfterFailure"):FindFirstChild("failureInItBlock"), function(results)
31+
assert(#results.errors == 1, "Expected one error, got " .. tostring(#results.errors))
32+
33+
local afterEachError = string.find(results.errors[1], "afterEach threw an error as expected")
34+
assert(afterEachError ~= nil, "Expected afterEach to be reached after it throws an error")
35+
36+
local cleaningUpError = string.find(results.errors[1], "While cleaning up the failed test another error was found")
37+
assert(cleaningUpError ~= nil, "Expected 'While cleaning up...' msg to be shown when afterEach fails after it fails")
38+
end)
39+
end,
40+
41+
["run afterEach e2e with failure in afterEach block"] = function()
42+
TestEZ.run(script:FindFirstChild("afterEachAfterFailure"):FindFirstChild("errorInAfterEachBlock"), function(results)
43+
assert(#results.errors == 1, "Expected one error, got " .. tostring(#results.errors))
44+
45+
local afterEachError = string.find(results.errors[1], "afterEach threw an error as expected")
46+
assert(afterEachError ~= nil, "Expected afterEach to be reached after it throws an error")
47+
48+
local cleaningUpError = string.find(results.errors[1], "While cleaning up the failed test another error was found")
49+
assert(cleaningUpError == nil, "Expected 'While cleaning up...' msg to not be shown when only afterEach fails")
50+
end)
51+
end,
52+
1753
["run lifecycle e2e w/ filter for describe block"] = function()
1854
TestEZ.TestBootstrap:run({
1955
script:FindFirstChild("lifecycle"):FindFirstChild("testNameFilterDescribeBlock"),

0 commit comments

Comments
 (0)