Compare commits

...

11 Commits

Author SHA1 Message Date
pfg
a723a13849 Merge branch 'main' into claude/fix-bun-test-globals-issue-4007 2025-10-14 14:19:41 -07:00
pfg
7b399e9435 Merge branch 'main' into claude/fix-bun-test-globals-issue-4007 2025-10-08 14:38:42 -07:00
pfg
3f96c6f03b don't do that. 2025-10-06 19:22:15 -07:00
pfg
5b22b8fefa Merge remote-tracking branch 'origin/main' into claude/fix-bun-test-globals-issue-4007 2025-10-06 18:58:24 -07:00
autofix-ci[bot]
65eb3abcb5 [autofix.ci] apply automated fixes 2025-08-26 22:04:16 +00:00
pfg
92b516ef6c Merge branch 'main' into claude/fix-bun-test-globals-issue-4007 2025-08-26 15:02:42 -07:00
pfg
2a8abd423b Merge branch 'main' into claude/fix-bun-test-globals-issue-4007 2025-08-14 14:22:57 -07:00
autofix-ci[bot]
2a841c2b51 [autofix.ci] apply automated fixes 2025-08-12 02:39:30 +00:00
Claude Bot
f7fe1a2385 refactor: always inject test globals regardless of test framework imports
Removed the logic that would skip injecting test globals when importing from
any test framework (@jest/globals, vitest, or bun:test). Now test globals are
always available when inject_jest_globals is enabled, providing a consistent
experience regardless of which test framework imports are used.

This allows mixing explicit imports with global usage patterns for all test
frameworks, not just bun:test.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-12 02:37:23 +00:00
autofix-ci[bot]
58a3b83449 [autofix.ci] apply automated fixes 2025-08-12 02:26:21 +00:00
Claude Bot
ba1973488e fix: allow test globals when importing from bun:test (#4007)
Previously, importing anything from bun:test would prevent all test globals
(test, expect, describe, etc.) from being injected into the global scope,
breaking common usage patterns.

Changed the transpiler logic to only skip global injection for @jest/globals
and vitest imports, but continue to inject globals when importing from bun:test.
This maintains compatibility with existing code that mixes explicit imports
with global usage.

Fixes issue where `import { mock } from "bun:test"` would cause ReferenceError
for test globals like `test` and `expect`.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-12 00:14:49 +00:00
6 changed files with 82 additions and 14 deletions

View File

@@ -1184,20 +1184,6 @@ pub const Parser = struct {
if (p.options.features.inject_jest_globals) outer: {
var jest: *Jest = &p.jest;
for (p.import_records.items) |*item| {
// skip if they did import it
if (strings.eqlComptime(item.path.text, "bun:test") or strings.eqlComptime(item.path.text, "@jest/globals") or strings.eqlComptime(item.path.text, "vitest")) {
if (p.options.features.runtime_transpiler_cache) |cache| {
// If we rewrote import paths, we need to disable the runtime transpiler cache
if (!strings.eqlComptime(item.path.text, "bun:test")) {
cache.input_hash = null;
}
}
break :outer;
}
}
// if they didn't use any of the jest globals, don't inject it, I guess.
const items_count = brk: {
var count: usize = 0;

View File

@@ -0,0 +1,21 @@
import { mock, spyOn } from "bun:test";
const mockTrue = mock(() => true);
const mockFalse = mock(() => false);
test("should work with multiple imports from bun:test", () => {
expect(mockTrue()).toEqual(true);
expect(mockFalse()).toEqual(false);
});
describe("spyOn should work", () => {
const obj = { foo: () => "original" };
beforeEach(() => {
spyOn(obj, "foo").mockReturnValue("mocked");
});
it("should spy on methods", () => {
expect(obj.foo()).toBe("mocked");
});
});

View File

@@ -0,0 +1,16 @@
import * as BunTest from "bun:test";
const mockTrue = BunTest.mock(() => true);
test("should work with namespace import from bun:test", () => {
expect(mockTrue()).toEqual(true);
});
describe("namespace import with globals should work", () => {
it("should have access to test globals", () => {
expect(typeof test).toBe("function");
expect(typeof describe).toBe("function");
expect(typeof it).toBe("function");
expect(typeof expect).toBe("function");
});
});

View File

@@ -0,0 +1,7 @@
import { mock } from "bun:test";
const mockTrue = mock(() => true);
test("should work with explicit import from bun:test", () => {
expect(mockTrue()).toEqual(true);
});

View File

@@ -0,0 +1,13 @@
import { expect as jestExpect, test as jestTest } from "@jest/globals";
// Test that bun's test globals are still available even with @jest/globals import
test("should have bun test globals available with @jest/globals import", () => {
expect(typeof test).toBe("function");
expect(typeof expect).toBe("function");
expect(typeof describe).toBe("function");
});
// Test that jest imports also work
jestTest("jest imports should work too", () => {
jestExpect(1 + 1).toBe(2);
});

View File

@@ -0,0 +1,25 @@
// Test mixing named imports with default globals usage
import { mock } from "bun:test";
const mockPath = mock(() => "/mocked/path");
test("should work with mixed imports", () => {
// Using imported mock
expect(mockPath()).toEqual("/mocked/path");
// Using global test functions
expect("a/b").toBe("a/b");
});
describe("mixed imports should not interfere", () => {
beforeAll(() => {
// This should work - beforeAll should be available as global
});
it("should have all test globals available", () => {
expect(typeof beforeAll).toBe("function");
expect(typeof afterAll).toBe("function");
expect(typeof beforeEach).toBe("function");
expect(typeof afterEach).toBe("function");
});
});