Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bun.js/bindings/BunPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ extern "C" JSC_DEFINE_HOST_FUNCTION(JSMock__jsModuleMock, (JSC::JSGlobalObject *
return {};
}

if (!callframe->argument(0).isString()) {
scope.throwException(lexicalGlobalObject, JSC::createTypeError(lexicalGlobalObject, "mock(module, fn) requires a module name string"_s));
return {};
}

JSC::JSString* specifierString = callframe->argument(0).toString(globalObject);
RETURN_IF_EXCEPTION(scope, {});
WTF::String specifier = specifierString->value(globalObject);
Expand Down
14 changes: 14 additions & 0 deletions test/js/bun/test/mock/mock-module-non-string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, mock, test } from "bun:test";

test("mock.module throws TypeError for non-string first argument", () => {
expect(() => mock.module(SharedArrayBuffer, () => ({}))).toThrow("mock(module, fn) requires a module name string");
expect(() => mock.module({}, () => ({}))).toThrow("mock(module, fn) requires a module name string");
expect(() => mock.module(123, () => ({}))).toThrow("mock(module, fn) requires a module name string");
expect(() => mock.module(Symbol("test"), () => ({}))).toThrow("mock(module, fn) requires a module name string");
Comment thread
claude[bot] marked this conversation as resolved.
Comment thread
claude[bot] marked this conversation as resolved.
});

test("mock.module still works with valid string argument", async () => {
mock.module("mock-module-non-string-test-fixture", () => ({ default: 42 }));
const m = await import("mock-module-non-string-test-fixture");
expect(m.default).toBe(42);
});
Loading