Skip to content

Commit 931b53d

Browse files
authored
Add wrangler types support for importable env and process.env (#8478)
1 parent 1b2aa91 commit 931b53d

File tree

6 files changed

+401
-117
lines changed

6 files changed

+401
-117
lines changed

.changeset/green-shrimps-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Add `wrangler types` support for importable env and `process.env`

packages/wrangler/e2e/types.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ describe("types", () => {
7373
"utf8"
7474
);
7575
expect(file).toMatchInlineSnapshot(`
76-
"// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 7fbca0b39560512499078acfe5f450c0)
77-
interface Env {
78-
MY_VAR: "my-var-value";
76+
"// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 7915eccca244b8d5c107e358ee5929e8)
77+
declare namespace Cloudflare {
78+
interface Env {
79+
MY_VAR: "my-var-value";
80+
}
7981
}
82+
interface Env extends Cloudflare.Env {}
8083
"
8184
`);
8285
});
@@ -92,7 +95,7 @@ describe("types", () => {
9295
).split("\n");
9396

9497
expect(lines[0]).toMatchInlineSnapshot(
95-
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 7fbca0b39560512499078acfe5f450c0)"`
98+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 7915eccca244b8d5c107e358ee5929e8)"`
9699
);
97100
expect(lines[1]).match(
98101
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
@@ -119,7 +122,7 @@ describe("types", () => {
119122
).split("\n");
120123

121124
expect(lines[0]).toMatchInlineSnapshot(
122-
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: 8fcf1ed67a52a2d34d6d34c3068e89b8)"`
125+
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: 2f74a5a99f09ae4d994228b5bb959d24)"`
123126
);
124127
expect(lines[1]).match(
125128
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import assert from "node:assert";
2+
import { isProcessEnvPopulated } from "../process-env";
3+
4+
describe("isProcessEnvPopulated", () => {
5+
test("default", () => {
6+
expect(isProcessEnvPopulated(undefined, ["nodejs_compat"])).toBe(false);
7+
});
8+
9+
test("future date", () => {
10+
expect(isProcessEnvPopulated("2026-01-01", ["nodejs_compat"])).toBe(true);
11+
});
12+
13+
test("old date", () => {
14+
expect(isProcessEnvPopulated("2000-01-01", ["nodejs_compat"])).toBe(false);
15+
});
16+
17+
test("switch date", () => {
18+
expect(isProcessEnvPopulated("2025-04-01", ["nodejs_compat"])).toBe(true);
19+
});
20+
21+
test("old date, but with flag", () => {
22+
expect(
23+
isProcessEnvPopulated("2000-01-01", [
24+
"nodejs_compat",
25+
"nodejs_compat_populate_process_env",
26+
])
27+
).toBe(true);
28+
});
29+
30+
test("old date, with disable flag", () => {
31+
expect(
32+
isProcessEnvPopulated("2000-01-01", [
33+
"nodejs_compat",
34+
"nodejs_compat_do_not_populate_process_env",
35+
])
36+
).toBe(false);
37+
});
38+
39+
test("future date, but with disable flag", () => {
40+
expect(
41+
isProcessEnvPopulated("2026-01-01", [
42+
"nodejs_compat",
43+
"nodejs_compat_do_not_populate_process_env",
44+
])
45+
).toBe(false);
46+
});
47+
48+
test("future date, with enable flag", () => {
49+
expect(
50+
isProcessEnvPopulated("2026-01-01", [
51+
"nodejs_compat",
52+
"nodejs_compat_populate_process_env",
53+
])
54+
).toBe(true);
55+
});
56+
57+
test("future date without nodejs_compat", () => {
58+
expect(isProcessEnvPopulated("2026-01-01")).toBe(false);
59+
});
60+
61+
test("future date, with enable flag but without nodejs_compat", () => {
62+
expect(
63+
isProcessEnvPopulated("2026-01-01", [
64+
"nodejs_compat_populate_process_env",
65+
])
66+
).toBe(false);
67+
});
68+
69+
test("errors with disable and enable flags specified", () => {
70+
try {
71+
isProcessEnvPopulated("2024-01-01", [
72+
"nodejs_compat_populate_process_env",
73+
"nodejs_compat_do_not_populate_process_env",
74+
]);
75+
assert(false, "Unreachable");
76+
} catch (e) {
77+
expect(e).toMatchInlineSnapshot(
78+
`[Error: Can't both enable and disable a flag]`
79+
);
80+
}
81+
});
82+
});

0 commit comments

Comments
 (0)