Fix Jest globals not being available in non-entrypoint files (#17734)

This commit is contained in:
Ashcon Partovi
2025-02-26 16:02:15 -08:00
committed by GitHub
parent a2d028462b
commit 1060558456
3 changed files with 35 additions and 2 deletions

View File

@@ -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

View File

@@ -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`,
};
},
});

View File

@@ -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();
});