Skip to content

Commit 78dde9b

Browse files
authored
Add before_each, after_each for async tests. (#350)
Also add sample tests using them.
1 parent 13f9959 commit 78dde9b

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

lua/plenary/async/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ exports.tests.add_globals = function()
3636
a.describe = exports.tests.describe
3737
-- must prefix with a or stack overflow
3838
a.it = exports.tests.it
39+
a.before_each = exports.tests.before_each
40+
a.after_each = exports.tests.after_each
3941
end
4042

4143
exports.tests.add_to_env = function()
@@ -50,6 +52,8 @@ exports.tests.add_to_env = function()
5052
env.a.describe = exports.tests.describe
5153
-- must prefix with a or stack overflow
5254
env.a.it = exports.tests.it
55+
a.before_each = exports.tests.before_each
56+
a.after_each = exports.tests.after_each
5357

5458
setfenv(2, env)
5559
end

lua/plenary/async/tests.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ M.it = function(s, async_func)
1010
it(s, util.will_block(async_func))
1111
end
1212

13+
M.before_each = function(async_func)
14+
before_each(util.will_block(async_func))
15+
end
16+
17+
M.after_each = function(async_func)
18+
after_each(util.will_block(async_func))
19+
end
20+
1321
return M

tests/plenary/async/test_spec.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require("plenary.async").tests.add_to_env()
2+
3+
a.describe("a.before_each", function()
4+
local counter = 0
5+
6+
local set_counter_to_one = a.wrap(function(callback)
7+
a.util.sleep(5)
8+
counter = 1
9+
end, 1)
10+
11+
a.before_each(a.void(function()
12+
set_counter_to_one()
13+
end))
14+
15+
a.it("should run in async context", function()
16+
counter = counter + 1
17+
assert.are.same(counter, 2)
18+
end)
19+
20+
a.it("should run for all tests", function()
21+
counter = counter + 2
22+
assert.are.same(counter, 3)
23+
end)
24+
end)
25+
26+
a.describe("a.after_each", function()
27+
local counter = 0
28+
29+
local set_counter_to_one = a.wrap(function(callback)
30+
a.util.sleep(5)
31+
counter = 1
32+
end, 1)
33+
34+
a.after_each(a.void(function()
35+
set_counter_to_one()
36+
end))
37+
38+
a.it("should not run before first test", function()
39+
counter = counter + 1
40+
assert.are.same(counter, 1)
41+
end)
42+
43+
a.it("should run before the second test", function()
44+
counter = counter + 2
45+
assert.are.same(counter, 3)
46+
end)
47+
48+
a.it("should run before the third test", function()
49+
counter = counter + 3
50+
assert.are.same(counter, 4)
51+
end)
52+
end)

0 commit comments

Comments
 (0)