diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index db3d25d7dd..4ee505d26c 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -474,7 +474,7 @@ pub const RuntimeTranspilerStore = struct { .virtual_source = null, .dont_bundle_twice = true, .allow_commonjs = true, - .inject_jest_globals = transpiler.options.rewrite_jest_for_tests and is_main, + .inject_jest_globals = transpiler.options.rewrite_jest_for_tests, .set_breakpoint_on_first_line = vm.debugger != null and vm.debugger.?.set_breakpoint_on_first_line and is_main and @@ -1623,7 +1623,7 @@ pub const ModuleLoader = struct { .virtual_source = virtual_source, .dont_bundle_twice = true, .allow_commonjs = true, - .inject_jest_globals = jsc_vm.transpiler.options.rewrite_jest_for_tests and is_main, + .inject_jest_globals = jsc_vm.transpiler.options.rewrite_jest_for_tests, .keep_json_and_toml_as_one_statement = true, .allow_bytecode_cache = true, .set_breakpoint_on_first_line = is_main and diff --git a/test/regression/issue/12034/12034.fixture.ts b/test/regression/issue/12034/12034.fixture.ts new file mode 100644 index 0000000000..e9ba06467e --- /dev/null +++ b/test/regression/issue/12034/12034.fixture.ts @@ -0,0 +1,25 @@ +// A fixture that tests that Jest globals are injected into the global scope +// even when the file is NOT the entrypoint of the test. + +test.each([ + ["expect", expect], + ["test", test], + ["describe", describe], + ["it", it], + ["beforeEach", beforeEach], + ["afterEach", afterEach], + ["beforeAll", beforeAll], + ["afterAll", afterAll], + ["jest", jest], +])("that %s is defined", (_, global) => { + expect(global).toBeDefined(); +}); + +expect.extend({ + toBeOne(actual) { + return { + pass: actual === 1, + message: () => `expected ${actual} to be 1`, + }; + }, +}); diff --git a/test/regression/issue/12034/12034.test.js b/test/regression/issue/12034/12034.test.js new file mode 100644 index 0000000000..344a5e04b0 --- /dev/null +++ b/test/regression/issue/12034/12034.test.js @@ -0,0 +1,8 @@ +// This fixture tests that Jest global variables are injected into the global scope +// even when the file is NOT the entrypoint of the test. +import "./12034.fixture"; + +test("that an imported file can use Jest globals", () => { + expect(1).toBeOne(); + expect(2).not.toBeOne(); +});