diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/bundler/bun-build-api.test.ts | 23 | ||||
-rw-r--r-- | test/js/bun/plugin/module-plugins.ts | 38 | ||||
-rw-r--r-- | test/js/bun/plugin/plugins.test.ts | 78 |
3 files changed, 139 insertions, 0 deletions
diff --git a/test/bundler/bun-build-api.test.ts b/test/bundler/bun-build-api.test.ts index 3c2308d94..d1f1a10b8 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -290,4 +290,27 @@ describe("Bun.build", () => { // depends on the ws package in the test/node_modules. expect(content).toContain("var websocket = __toESM(require_websocket(), 1);"); }); + + test("module() throws error", async () => { + expect(() => + Bun.build({ + entrypoints: [join(import.meta.dir, "./fixtures/trivial/bundle-ws.ts")], + plugins: [ + { + name: "test", + setup: b => { + b.module("ad", () => { + return { + exports: { + hello: "world", + }, + loader: "object", + }; + }); + }, + }, + ], + }), + ).toThrow(); + }); }); diff --git a/test/js/bun/plugin/module-plugins.ts b/test/js/bun/plugin/module-plugins.ts new file mode 100644 index 000000000..d6034c5df --- /dev/null +++ b/test/js/bun/plugin/module-plugins.ts @@ -0,0 +1,38 @@ +import { plugin } from "bun"; +plugin({ + name: "i am virtual!", + setup(builder) { + builder.module("my-virtual-module-async", async () => { + // check + await Bun.sleep(1); + return { + exports: { + hello: "world", + }, + loader: "object", + }; + }); + + builder.module("my-virtual-module-sync", () => { + return { + exports: { + hello: "world", + }, + loader: "object", + }; + }); + + builder.onLoad({ filter: /.*/, namespace: "rejected-promise" }, async ({ path }) => { + throw new Error("Rejected Promise"); + }); + + builder.onResolve({ filter: /.*/, namespace: "rejected-promise2" }, ({ path }) => ({ + namespace: "rejected-promise2", + path, + })); + + builder.onLoad({ filter: /.*/, namespace: "rejected-promise2" }, ({ path }) => { + return Promise.reject(new Error("Rejected Promise")); + }); + }, +}); diff --git a/test/js/bun/plugin/plugins.test.ts b/test/js/bun/plugin/plugins.test.ts index c2827f600..2d3cfa1fa 100644 --- a/test/js/bun/plugin/plugins.test.ts +++ b/test/js/bun/plugin/plugins.test.ts @@ -185,6 +185,7 @@ plugin({ // This is to test that it works when imported from a separate file import "../../third_party/svelte"; +import "./module-plugins"; describe("require", () => { it("SSRs `<h1>Hello world!</h1>` with Svelte", () => { @@ -210,6 +211,83 @@ describe("require", () => { }); }); +describe("module", () => { + it("throws with require()", () => { + expect(() => require("my-virtual-module-async")).toThrow(); + }); + + it("async module works with async import", async () => { + // @ts-expect-error + const { hello } = await import("my-virtual-module-async"); + + expect(hello).toBe("world"); + delete require.cache["my-virtual-module-async"]; + }); + + it("sync module module works with require()", async () => { + const { hello } = require("my-virtual-module-sync"); + + expect(hello).toBe("world"); + delete require.cache["my-virtual-module-sync"]; + }); + + it("sync module module works with require.resolve()", async () => { + expect(require.resolve("my-virtual-module-sync")).toBe("my-virtual-module-sync"); + delete require.cache["my-virtual-module-sync"]; + }); + + it("sync module module works with import", async () => { + // @ts-expect-error + const { hello } = await import("my-virtual-module-sync"); + + expect(hello).toBe("world"); + delete require.cache["my-virtual-module-sync"]; + }); + + it("modules are overridable", async () => { + // @ts-expect-error + let { hello, there } = await import("my-virtual-module-sync"); + expect(there).toBeUndefined(); + expect(hello).toBe("world"); + + Bun.plugin({ + setup(builder) { + builder.module("my-virtual-module-sync", () => ({ + exports: { + there: true, + }, + loader: "object", + })); + }, + }); + + { + const { there, hello } = require("my-virtual-module-sync"); + expect(there).toBe(true); + expect(hello).toBeUndefined(); + } + + Bun.plugin({ + setup(builder) { + builder.module("my-virtual-module-sync", () => ({ + exports: { + yo: true, + }, + loader: "object", + })); + }, + }); + + { + // @ts-expect-error + const { there, hello, yo } = await import("my-virtual-module-sync"); + expect(yo).toBe(true); + expect(hello).toBeUndefined(); + expect(there).toBeUndefined(); + } + }); +}); + describe("dynamic import", () => { it("SSRs `<h1>Hello world!</h1>` with Svelte", async () => { const { default: App }: any = await import("./hello.svelte"); |