diff --git a/test/js/bun/test/jest-doesnt-auto-import.js b/test/js/bun/test/jest-doesnt-auto-import.js deleted file mode 100644 index 4d4a02b37378..000000000000 --- a/test/js/bun/test/jest-doesnt-auto-import.js +++ /dev/null @@ -1,12 +0,0 @@ -export function getJestGlobals() { - return { - describe: typeof describe === "function" ? describe : undefined, - it: typeof it === "function" ? it : undefined, - test: typeof test === "function" ? test : undefined, - expect: typeof expect === "function" ? expect : undefined, - beforeAll: typeof beforeAll === "function" ? beforeAll : undefined, - beforeEach: typeof beforeEach === "function" ? beforeEach : undefined, - afterAll: typeof afterAll === "function" ? afterAll : undefined, - afterEach: typeof afterEach === "function" ? afterEach : undefined, - }; -} diff --git a/test/js/bun/test/test-auto-import-jest-globals.test.js b/test/js/bun/test/test-auto-import-jest-globals.test.js index 5baeae43e917..c52605cb2830 100644 --- a/test/js/bun/test/test-auto-import-jest-globals.test.js +++ b/test/js/bun/test/test-auto-import-jest-globals.test.js @@ -1,3 +1,5 @@ +import { bunEnv, bunExe, tempDir } from "harness"; + test("Jest auto imports", () => { expect(true).toBe(true); expect(typeof describe).toBe("function"); @@ -10,15 +12,39 @@ test("Jest auto imports", () => { expect(typeof afterEach).toBe("function"); }); -test("Jest's globals aren't available in every file", async () => { - const jestGlobals = await import("./jest-doesnt-auto-import.js"); +// Injection is a `bun test`-mode parser transform (#17734 made it apply to every +// file loaded by the test runner, not just the entrypoint). The only remaining +// scoping guarantee is that ordinary `bun ` / `bun -e` runs do NOT see it. +describe("Jest globals are not injected outside of `bun test`", () => { + const names = ["describe", "it", "test", "expect", "beforeAll", "beforeEach", "afterAll", "afterEach"]; + const source = `process.stdout.write(JSON.stringify({${names.map(n => `${n}: typeof ${n}`).join(", ")}}))`; + const expected = Object.fromEntries(names.map(n => [n, "undefined"])); + + test.concurrent("bun -e", async () => { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", source], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual(expected); + expect(exitCode).toBe(0); + }); - expect(typeof jestGlobals.describe).toBe("undefined"); - expect(typeof jestGlobals.it).toBe("undefined"); - expect(typeof jestGlobals.test).toBe("undefined"); - expect(typeof jestGlobals.expect).toBe("undefined"); - expect(typeof jestGlobals.beforeAll).toBe("undefined"); - expect(typeof jestGlobals.beforeEach).toBe("undefined"); - expect(typeof jestGlobals.afterAll).toBe("undefined"); - expect(typeof jestGlobals.afterEach).toBe("undefined"); + test.concurrent("bun ", async () => { + using dir = tempDir("jest-globals-not-injected", { "entry.js": source }); + await using proc = Bun.spawn({ + cmd: [bunExe(), "entry.js"], + env: bunEnv, + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual(expected); + expect(exitCode).toBe(0); + }); });