diff --git a/src/jsc/bindings/BunPlugin.cpp b/src/jsc/bindings/BunPlugin.cpp index 5773394aed30..cc8c2d228554 100644 --- a/src/jsc/bindings/BunPlugin.cpp +++ b/src/jsc/bindings/BunPlugin.cpp @@ -302,12 +302,13 @@ static inline JSC::EncodedJSValue setupBunPlugin(JSC::JSGlobalObject* globalObje auto targetValue = obj->getIfPropertyExists(globalObject, Identifier::fromString(vm, "target"_s)); RETURN_IF_EXCEPTION(throwScope, {}); if (targetValue) { - if (auto* targetJSString = targetValue.toStringOrNull(globalObject)) { - String targetString = targetJSString->value(globalObject); - if (!(targetString == "node"_s || targetString == "bun"_s || targetString == "browser"_s)) { - JSC::throwTypeError(globalObject, throwScope, "plugin target must be one of 'node', 'bun' or 'browser'"_s); - return {}; - } + auto* targetJSString = targetValue.toString(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); + String targetString = targetJSString->value(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); + if (!(targetString == "node"_s || targetString == "bun"_s || targetString == "browser"_s)) { + JSC::throwTypeError(globalObject, throwScope, "plugin target must be one of 'node', 'bun' or 'browser'"_s); + return {}; } } diff --git a/test/js/bun/plugin/plugins.test.ts b/test/js/bun/plugin/plugins.test.ts index 1ccd453b2246..f1ad19fc49dd 100644 --- a/test/js/bun/plugin/plugins.test.ts +++ b/test/js/bun/plugin/plugins.test.ts @@ -1,6 +1,6 @@ /// import { plugin } from "bun"; -import { describe, expect, it } from "bun:test"; +import { describe, expect, it, jest } from "bun:test"; import { resolve } from "path"; declare global { @@ -366,6 +366,23 @@ describe("errors", () => { }).toThrow("plugin target must be one of 'node', 'bun' or 'browser'"); }); + it("handles a 'target' whose toString throws", () => { + const setup = jest.fn(); + const opts = { + setup, + target: { + toString() { + throw new Error("target toString error"); + }, + }, + }; + + expect(() => { + plugin(opts as any); + }).toThrow("target toString error"); + expect(setup).not.toHaveBeenCalled(); + }); + it("invalid loaders throw", () => { const invalidLoaders = ["blah", "blah2", "blah3", "blah4"]; const inputs = ["body { background: red; }", "

hi

", '{"hi": "there"}', "hi"];