Files
bun.sh/test/regression/issue/440.test.ts
robobun 798b48c898 fix(runtime): exclude globalThis from auto-serve detection (#26107)
## Summary
- When a module exports `globalThis` (e.g., `module.exports =
globalThis`), Bun's auto-serve detection incorrectly triggered because
`globalThis.fetch` is the Fetch API function
- Scripts that export globalThis (like `core-js/es/global-this.js`)
would start a development server on port 3000 instead of exiting
normally
- Added explicit check to skip auto-serve when the default export is
`globalThis` itself

Fixes #440

## Test plan
- [x] Added test case `test/regression/issue/440.test.ts` that verifies:
  - `module.exports = globalThis` does not start a server
  - `export default globalThis` does not start a server
- [x] Verified test fails with system Bun (without fix)
- [x] Verified test passes with debug build (with fix)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:41:21 -08:00

47 lines
1.3 KiB
TypeScript

import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
test("exporting globalThis should not start a server", async () => {
using dir = tempDir("issue-440", {
"test.js": `module.exports = globalThis;`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test.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]);
// Should NOT contain server started message
expect(stderr).not.toContain("Started");
expect(stderr).not.toContain("server:");
expect(stdout).toBe("");
expect(exitCode).toBe(0);
});
test("default export of globalThis should not start a server", async () => {
using dir = tempDir("issue-440-esm", {
"test.js": `export default globalThis;`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test.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]);
// Should NOT contain server started message
expect(stderr).not.toContain("Started");
expect(stderr).not.toContain("server:");
expect(stdout).toBe("");
expect(exitCode).toBe(0);
});