Skip to content

Commit 4d91c94

Browse files
committed
[jest] wait for beforeAll() to complete
fixes #1688
1 parent be706fe commit 4d91c94

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/bun.js/test/jest.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,10 +1492,16 @@ pub const DescribeScope = struct {
14921492
for (hooks) |cb, i| {
14931493
if (cb.isEmpty()) continue;
14941494

1495-
const err = cb.call(ctx, &.{});
1496-
if (err.isAnyError(ctx)) {
1497-
return err;
1495+
const pending_test = Jest.runner.?.pending_test;
1496+
// forbid `expect()` within hooks
1497+
Jest.runner.?.pending_test = null;
1498+
var result = cb.call(ctx, &.{});
1499+
if (result.asPromise()) |promise| {
1500+
VirtualMachine.get().waitForPromise(promise);
1501+
result = promise.result(ctx.vm());
14981502
}
1503+
Jest.runner.?.pending_test = pending_test;
1504+
if (result.isAnyError(ctx)) return result;
14991505

15001506
if (comptime hook == .beforeAll or hook == .afterAll) {
15011507
hooks[i] = JSC.JSValue.zero;

test/bun.js/bun-test/jest-hooks.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,41 @@ describe("test jest hooks in bun-test", () => {
7373
expect(animal).toEqual("lion");
7474
});
7575
});
76+
77+
describe("test async hooks", async () => {
78+
let beforeAllCalled = 0;
79+
let beforeEachCalled = 0;
80+
let afterAllCalled = 0;
81+
let afterEachCalled = 0;
82+
83+
beforeAll(async () => {
84+
beforeAllCalled += await 1;
85+
});
86+
87+
beforeEach(async () => {
88+
beforeEachCalled += await 1;
89+
});
90+
91+
afterAll(async () => {
92+
afterAllCalled += await 1;
93+
});
94+
95+
afterEach(async () => {
96+
afterEachCalled += await 1;
97+
});
98+
99+
it("should run after beforeAll()", () => {
100+
expect(beforeAllCalled).toBe(1);
101+
expect(beforeEachCalled).toBe(1);
102+
expect(afterAllCalled).toBe(0);
103+
expect(afterEachCalled).toBe(0);
104+
});
105+
106+
it("should run after beforeEach()", () => {
107+
expect(beforeAllCalled).toBe(1);
108+
expect(beforeEachCalled).toBe(2);
109+
expect(afterAllCalled).toBe(0);
110+
expect(afterEachCalled).toBe(1);
111+
});
112+
});
76113
});

0 commit comments

Comments
 (0)