Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont run all nested beforeAll hooks on test start #17938

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1132,15 +1132,6 @@ pub const DescribeScope = struct {
var i: TestRunner.Test.ID = 0;

if (this.shouldEvaluateScope()) {
if (this.runCallback(globalObject, .beforeAll)) |err| {
Copy link
Author

@francescov1 francescov1 Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main culprit, it goes up the parent chain and calls all beforeAll blocks and its own before starting the test

_ = globalObject.bunVM().uncaughtException(globalObject, err, true);
while (i < end) {
Jest.runner.?.reportFailure(i + this.test_id_start, source.path.text, tests[i].label, 0, 0, this);
i += 1;
}
this.deinit(globalObject);
return;
}
if (end == 0) {
var runner = allocator.create(TestRunnerTask) catch unreachable;
runner.* = .{
Expand Down Expand Up @@ -1375,6 +1366,12 @@ pub const TestRunnerTask = struct {
jsc_vm.onUnhandledRejectionCtx = this;
jsc_vm.onUnhandledRejection = onUnhandledRejection;

if (this.describe.runCallback(globalThis, .beforeAll)) |err| {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead we can trigger the beforeAll blocks right before the beforeEach blocks

_ = jsc_vm.uncaughtException(globalThis, err, true);
Jest.runner.?.reportFailure(test_id, this.source_file_path, test_.label, 0, 0, describe);
return false;
}

if (this.needs_before_each) {
this.needs_before_each = false;
const label = test_.label;
Expand Down
91 changes: 78 additions & 13 deletions test/js/bun/test/jest-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,86 @@ describe("describe scope", () => {
afterAll(() => hooks_run.push("describe afterAll"));
afterEach(() => hooks_run.push("describe afterEach"));

it("should run after beforeAll/beforeEach in the correct order", () => {
expect(hooks_run).toEqual(["global beforeAll", "describe beforeAll", "global beforeEach", "describe beforeEach"]);
describe("nested describe scope", () => {
beforeAll(() => hooks_run.push("nested describe beforeAll"));
beforeEach(() => hooks_run.push("nested describe beforeEach"));
afterAll(() => hooks_run.push("nested describe afterAll"));
afterEach(() => hooks_run.push("nested describe afterEach"));

it("should run after nested beforeAll/beforeEach in the correct order", () => {
// Entries from before first test
expect(hooks_run).toEqual([
"global beforeAll",
"describe beforeAll",
"nested describe beforeAll",
"global beforeEach",
"describe beforeEach",
"nested describe beforeEach",
]);
});

it("should run after nested afterEach/afterAll in the correct order", () => {
expect(hooks_run).toEqual([
// Entries from before first test
"global beforeAll",
"describe beforeAll",
"nested describe beforeAll",
"global beforeEach",
"describe beforeEach",
"nested describe beforeEach",

// Entries from after first test
"nested describe afterEach",
"describe afterEach",
"global afterEach",

// Entries from before second test
"global beforeEach",
"describe beforeEach",
"nested describe beforeEach",
]);
});
});

it("should run after afterEach/afterAll in the correct order", () => {
expect(hooks_run).toEqual([
"global beforeAll",
"describe beforeAll",
"global beforeEach",
"describe beforeEach",
"describe afterEach",
"global afterEach",
"global beforeEach",
"describe beforeEach",
]);
describe("nested describe scope 2", () => {
beforeAll(() => hooks_run.push("nested describe 2 beforeAll"));
beforeEach(() => hooks_run.push("nested describe 2 beforeEach"));
afterAll(() => hooks_run.push("nested describe 2 afterAll"));
afterEach(() => hooks_run.push("nested describe 2 afterEach"));

it("should run after beforeAll/beforeEach in the correct order", () => {
expect(hooks_run).toEqual([
// Entries from first test
"global beforeAll",
"describe beforeAll",
"nested describe beforeAll",
"global beforeEach",
"describe beforeEach",
"nested describe beforeEach",

// Entries from after first test
"nested describe afterEach",
"describe afterEach",
"global afterEach",

// Entries from before second test
"global beforeEach",
"describe beforeEach",
"nested describe beforeEach",

// Entries from after second test
"nested describe afterEach",
"describe afterEach",
"global afterEach",
"nested describe afterAll",

// Entries from before third test
"nested describe 2 beforeAll",
"global beforeEach",
"describe beforeEach",
"nested describe 2 beforeEach",
]);
});
});
});

Expand Down