| 1 | import { createMiniflareFromRolldown } from "../../../../cloudflare-test-tools/src/miniflare/miniflare.ts"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | import { buildFixture } from "./utils/build-fixture.ts"; |
| 4 | |
| 5 | describe("process.env", async () => { |
| 6 | const built = await buildFixture({ |
| 7 | fixture: "process-env/index.ts", |
| 8 | }); |
| 9 | |
| 10 | it("responds to fetch /", async () => { |
| 11 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 12 | compatibilityDate: "2025-07-01", |
| 13 | }); |
| 14 | expect(await miniflare.fetchText("/")).toBe("ok"); |
| 15 | }); |
| 16 | |
| 17 | it("replaces process.env.NODE_ENV with 'production'", async () => { |
| 18 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 19 | compatibilityDate: "2025-07-01", |
| 20 | }); |
| 21 | expect(await miniflare.fetchText("/node-env")).toBe("production"); |
| 22 | }); |
| 23 | |
| 24 | it("replaces global.process.env.NODE_ENV with 'production'", async () => { |
| 25 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 26 | compatibilityDate: "2025-07-01", |
| 27 | }); |
| 28 | expect(await miniflare.fetchText("/global-node-env")).toBe("production"); |
| 29 | }); |
| 30 | |
| 31 | it("replaces globalThis.process.env.NODE_ENV with 'production'", async () => { |
| 32 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 33 | compatibilityDate: "2025-07-01", |
| 34 | }); |
| 35 | expect(await miniflare.fetchText("/globalthis-node-env")).toBe( |
| 36 | "production", |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | it("does not inject a global process object when only process.env is stubbed", async () => { |
| 41 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 42 | compatibilityDate: "2025-07-01", |
| 43 | }); |
| 44 | expect(await miniflare.fetchText("/typeof-process")).toBe("undefined"); |
| 45 | }); |
| 46 | |
| 47 | it("defines navigator.userAgent for supported compatibility dates", async () => { |
| 48 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 49 | compatibilityDate: "2025-07-01", |
| 50 | }); |
| 51 | expect(await miniflare.fetchText("/user-agent")).toBe("Cloudflare-Workers"); |
| 52 | }); |
| 53 | }); |