diff --git a/package.json b/package.json index 18c60c2..ac699a7 100644 --- a/package.json +++ b/package.json @@ -80,20 +80,25 @@ } }, "./proxy": { + "browser": "./dist/proxy-stub.mjs", + "bun": "./dist/proxy-stub.mjs", + "deno": "./dist/proxy-stub.mjs", + "edge-light": "./dist/proxy-stub.mjs", + "edge-routine": "./dist/proxy-stub.mjs", + "lagon": "./dist/proxy-stub.mjs", + "netlify": "./dist/proxy-stub.mjs", + "react-native": "./dist/proxy-stub.mjs", + "wintercg": "./dist/proxy-stub.mjs", + "worker": "./dist/proxy-stub.mjs", + "workerd": "./dist/proxy-stub.mjs", "node": { "types": "./lib/proxy.d.ts", "default": "./dist/proxy.cjs" }, - "default": { - "import": { - "types": "./lib/proxy.d.ts", - "default": "./dist/proxy-stub.mjs" - }, - "require": { - "types": "./lib/proxy.d.ts", - "default": "./dist/proxy-stub.cjs" - } - } + "types": "./lib/proxy.d.ts", + "import": "./dist/proxy-stub.mjs", + "require": "./dist/proxy-stub.cjs", + "default": "./dist/proxy-stub.mjs" } }, "main": "./lib/index.cjs", diff --git a/test/proxy.test.ts b/test/proxy.test.ts new file mode 100644 index 0000000..40afae6 --- /dev/null +++ b/test/proxy.test.ts @@ -0,0 +1,82 @@ +import { createRequire } from "node:module"; +import { describe, expect, it } from "vitest"; +import pkg from "../package.json"; +import * as proxyStub from "../src/proxy-stub"; + +const require = createRequire(import.meta.url); +const proxyNode = require("../dist/proxy.cjs"); + +describe("node-fetch-native/proxy:exports", () => { + const runtimeConditions = [ + "browser", + "bun", + "deno", + "edge-light", + "edge-routine", + "lagon", + "netlify", + "react-native", + "wintercg", + "worker", + "workerd", + ]; + + it("exports map defines non-node runtime conditions pointing to proxy-stub", () => { + const proxyExport = pkg.exports["./proxy"]; + expect(proxyExport).toBeDefined(); + + for (const condition of runtimeConditions) { + expect( + proxyExport[condition as keyof typeof proxyExport], + `condition ${condition} should point to proxy stub`, + ).toBe("./dist/proxy-stub.mjs"); + } + }); + + it("exports map defines node condition pointing to proxy.cjs", () => { + const proxyExport = pkg.exports["./proxy"]; + expect(proxyExport.node).toEqual({ + types: "./lib/proxy.d.ts", + default: "./dist/proxy.cjs", + }); + }); + + it("exports map defines fallback conditions", () => { + const proxyExport = pkg.exports["./proxy"]; + expect(proxyExport.import).toBe("./dist/proxy-stub.mjs"); + expect(proxyExport.require).toBe("./dist/proxy-stub.cjs"); + expect(proxyExport.default).toBe("./dist/proxy-stub.mjs"); + expect(proxyExport.types).toBe("./lib/proxy.d.ts"); + }); +}); + +describe("node-fetch-native/proxy:stub", () => { + it("exports createProxy returning undefined agent and dispatcher", () => { + const proxy = proxyStub.createProxy(); + expect(proxy).toEqual({ + agent: undefined, + dispatcher: undefined, + }); + }); + + it("exports createFetch returning global fetch", () => { + expect(proxyStub.createFetch()).toBe(globalThis.fetch); + }); + + it("exports fetch matching global fetch", () => { + expect(proxyStub.fetch).toBe(globalThis.fetch); + }); +}); + +describe("node-fetch-native/proxy:node", () => { + it("exports expected symbols in node proxy cjs", () => { + expect(typeof proxyNode.createProxy).toBe("function"); + expect(typeof proxyNode.createFetch).toBe("function"); + expect(typeof proxyNode.fetch).toBe("function"); + }); + + it("creates agent when no options provided", () => { + const proxy = proxyNode.createProxy(); + expect(proxy).toBeDefined(); + }); +});