Skip to content

Commit 22f8089

Browse files
feat: support multiple finally calls within a single test (#753)
1 parent 5ec5d92 commit 22f8089

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

busted/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ local function init(busted)
2929
if not element.env then element.env = {} end
3030

3131
block.rejectAll(element)
32-
element.env.finally = function(fn) finally = fn end
32+
element.env.finally = function(fn)
33+
local old_finally = finally or function() end
34+
finally = function()
35+
fn()
36+
old_finally()
37+
end
38+
end
3339
element.env.pending = busted.pending
3440

3541
local pass, ancestor = block.execAll('before_each', parent, true)

spec/core_spec.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ describe('finally callback is called in case of success', function()
229229
end)
230230
end)
231231

232+
describe('multiple finally callbacks', function()
233+
local order = {}
234+
local f1 = spy.new(function() table.insert(order, "f1") end)
235+
local f2 = spy.new(function() table.insert(order, "f2") end)
236+
local f3 = spy.new(function() table.insert(order, "f3") end)
237+
238+
it('pushes all finally blocks', function()
239+
finally(f1)
240+
finally(f2)
241+
finally(f3)
242+
assert.is_true(true)
243+
end)
244+
245+
it('ensures all finally blocks were called', function()
246+
assert.spy(f1).was_called(1)
247+
assert.spy(f2).was_called(1)
248+
assert.spy(f3).was_called(1)
249+
assert.are_same({ "f3", "f2", "f1" }, order)
250+
end)
251+
end)
252+
232253
describe('tests environment', function()
233254
global = 'global' --luacheck: ignore
234255

0 commit comments

Comments
 (0)