Skip to content

Commit dd5a2f6

Browse files
fix(busted) - before_each and after_each execute in unpredictable order - fixes #491 (#508)
* fix #491 where before_each and after_each execute in unpredictable order. Just changed the tables that store these functions to be indexed by the parent `describe`s index in the current_describe table which uses sequential integers. Then in the run_each function where these functions get executed, iterating thru them usning ipairs instead of pairs.
1 parent 267282a commit dd5a2f6

File tree

2 files changed

+97
-8
lines changed

2 files changed

+97
-8
lines changed

lua/plenary/busted.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ local pop_description = function()
5959
end
6060

6161
local add_new_each = function()
62-
current_before_each[current_description[#current_description]] = {}
63-
current_after_each[current_description[#current_description]] = {}
62+
current_before_each[#current_description] = {}
63+
current_after_each[#current_description] = {}
6464
end
6565

6666
local clear_last_each = function()
67-
current_before_each[current_description[#current_description]] = nil
68-
current_after_each[current_description[#current_description]] = nil
67+
current_before_each[#current_description] = nil
68+
current_after_each[#current_description] = nil
6969
end
7070

7171
local call_inner = function(desc, func)
@@ -140,11 +140,11 @@ mod.inner_describe = function(desc, func)
140140
end
141141

142142
mod.before_each = function(fn)
143-
table.insert(current_before_each[current_description[#current_description]], fn)
143+
table.insert(current_before_each[#current_description], fn)
144144
end
145145

146146
mod.after_each = function(fn)
147-
table.insert(current_after_each[current_description[#current_description]], fn)
147+
table.insert(current_after_each[#current_description], fn)
148148
end
149149

150150
mod.clear = function()
@@ -161,7 +161,7 @@ local indent = function(msg, spaces)
161161
end
162162

163163
local run_each = function(tbl)
164-
for _, v in pairs(tbl) do
164+
for _, v in ipairs(tbl) do
165165
for _, w in ipairs(v) do
166166
if type(w) == "function" then
167167
w()

tests/plenary/simple_busted_spec.lua

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ describe("before each", function()
6767
end)
6868
end)
6969

70+
describe("before_each ordering", function()
71+
local order = ""
72+
before_each(function()
73+
order = order .. "1,"
74+
end)
75+
before_each(function()
76+
order = order .. "2,"
77+
end)
78+
describe("nested 1 deep", function()
79+
before_each(function()
80+
order = order .. "3,"
81+
end)
82+
before_each(function()
83+
order = order .. "4,"
84+
end)
85+
describe("nested 2 deep", function()
86+
before_each(function()
87+
order = order .. "5,"
88+
end)
89+
it("runs before_each`s in order", function()
90+
eq("1,2,3,4,5,", order)
91+
end)
92+
end)
93+
end)
94+
describe("adjacent nested 1 deep", function()
95+
before_each(function()
96+
order = order .. "3a,"
97+
end)
98+
before_each(function()
99+
order = order .. "4a,"
100+
end)
101+
describe("nested 2 deep", function()
102+
before_each(function()
103+
order = order .. "5a,"
104+
end)
105+
it("runs before_each`s in order", function()
106+
eq("1,2,3,4,5,1,2,3a,4a,5a,", order)
107+
end)
108+
end)
109+
end)
110+
end)
111+
70112
describe("after each", function()
71113
local a = 2
72114
local b = 3
@@ -110,7 +152,54 @@ describe("after each", function()
110152
end)
111153
end)
112154

113-
describe("fourth top level describe test", function()
155+
describe("after_each ordering", function()
156+
local order = ""
157+
describe("1st describe having after_each", function()
158+
after_each(function()
159+
order = order .. "1,"
160+
end)
161+
after_each(function()
162+
order = order .. "2,"
163+
end)
164+
describe("nested 1 deep", function()
165+
after_each(function()
166+
order = order .. "3,"
167+
end)
168+
after_each(function()
169+
order = order .. "4,"
170+
end)
171+
describe("nested 2 deep", function()
172+
after_each(function()
173+
order = order .. "5,"
174+
end)
175+
it("a test to trigger the after_each`s", function()
176+
assert(true)
177+
end)
178+
end)
179+
end)
180+
describe("adjacent nested 1 deep", function()
181+
after_each(function()
182+
order = order .. "3a,"
183+
end)
184+
after_each(function()
185+
order = order .. "4a,"
186+
end)
187+
describe("nested 2 deep", function()
188+
after_each(function()
189+
order = order .. "5a,"
190+
end)
191+
it("a test to trigger the adjacent after_each`s", function()
192+
assert(true)
193+
end)
194+
end)
195+
end)
196+
end)
197+
it("ran after_each`s in order", function()
198+
eq("1,2,3,4,5,1,2,3a,4a,5a,", order)
199+
end)
200+
end)
201+
202+
describe("another top level describe test", function()
114203
it("should work", function()
115204
eq(1, 1)
116205
end)

0 commit comments

Comments
 (0)