Skip to content

Commit f091909

Browse files
add various e2e and fixture tests for dotenv support
- `wrangler types` - `wrangler pages dev` - `wrangler dev` - nodejs app using `process.env`
1 parent 0e1e007 commit f091909

File tree

20 files changed

+223
-20
lines changed

20 files changed

+223
-20
lines changed

fixtures/nodejs-hybrid-app/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEV_VAR_FROM_DOT_ENV="dev-var-from-dot-env"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.env

fixtures/nodejs-hybrid-app/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default {
2222
return testGetRandomValues();
2323
case "/test-process":
2424
return testProcessBehavior();
25+
case "/env":
26+
return Response.json(env);
27+
case "/process-env":
28+
return Response.json(process.env);
2529
case "/query":
2630
return testPostgresLibrary(env, ctx);
2731
case "/test-x509-certificate":

fixtures/nodejs-hybrid-app/tests/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,26 @@ describe("nodejs compat", () => {
8888
const response = await fetch(`http://${ip}:${port}/test-http`);
8989
await expect(response.text()).resolves.toBe("OK");
9090
});
91+
92+
test("process.env contains vars", async ({ expect }) => {
93+
const { ip, port } = wrangler;
94+
const response = await fetch(`http://${ip}:${port}/process-env`);
95+
await expect(response.json()).resolves.toEqual(
96+
expect.objectContaining({
97+
DB_HOSTNAME: "hh-pgsql-public.ebi.ac.uk",
98+
DEV_VAR_FROM_DOT_ENV: "dev-var-from-dot-env",
99+
})
100+
);
101+
});
102+
103+
test("env contains vars", async ({ expect }) => {
104+
const { ip, port } = wrangler;
105+
const response = await fetch(`http://${ip}:${port}/env`);
106+
await expect(response.json()).resolves.toEqual(
107+
expect.objectContaining({
108+
DB_HOSTNAME: "hh-pgsql-public.ebi.ac.uk",
109+
DEV_VAR_FROM_DOT_ENV: "dev-var-from-dot-env",
110+
})
111+
);
112+
});
91113
});

fixtures/nodejs-hybrid-app/wrangler.jsonc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "nodejs-hybrid-app",
33
"main": "src/index.ts",
4-
// Setting compat date to 2024/09/23 means we don't need to use `nodejs_compat_v2`
5-
"compatibility_date": "2024-09-23",
4+
// Setting compat date after 2024/09/23 means we don't need to use `nodejs_compat_v2`
5+
// Setting compat date after 2025/04/01 means we don't need to use `nodejs_compat_populate_process_env`
6+
"compatibility_date": "2025-07-01",
67
"compatibility_flags": ["nodejs_compat"],
78
/*
89
These DB connection values are to a public database containing information about

fixtures/pages-workerjs-app/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=bar
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.env

fixtures/pages-workerjs-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"sideEffects": false,
55
"scripts": {
66
"check:type": "tsc",
7-
"dev": "wrangler pages dev ./workerjs-test --port 8792",
7+
"dev": "wrangler pages dev ./workerjs-test --port 8792 --compatibility-date=2025-07-15",
88
"test:ci": "vitest run",
99
"test:watch": "vitest",
1010
"type:tests": "tsc -p ./tests/tsconfig.json"

fixtures/pages-workerjs-app/tests/index.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ describe("Pages _worker.js", () => {
3535
const { ip, port, stop } = await runWranglerPagesDev(
3636
resolve(__dirname, ".."),
3737
"./workerjs-test",
38-
["--no-bundle=false", "--port=0", "--inspector-port=0"]
38+
[
39+
"--no-bundle=false",
40+
"--port=0",
41+
"--inspector-port=0",
42+
"--compatibility-date=2025-07-15",
43+
]
3944
);
4045
try {
4146
await expect(
@@ -52,7 +57,12 @@ describe("Pages _worker.js", () => {
5257
const { ip, port, stop } = await runWranglerPagesDev(
5358
resolve(__dirname, ".."),
5459
"./workerjs-test",
55-
["--bundle", "--port=0", "--inspector-port=0"]
60+
[
61+
"--bundle",
62+
"--port=0",
63+
"--inspector-port=0",
64+
"--compatibility-date=2025-07-15",
65+
]
5666
);
5767
try {
5868
await expect(
@@ -71,6 +81,7 @@ describe("Pages _worker.js", () => {
7181
await runWranglerPagesDev(resolve(__dirname, ".."), "./workerjs-test", [
7282
"--port=0",
7383
"--inspector-port=0",
84+
"--compatibility-date=2025-07-15",
7485
]);
7586
try {
7687
clearOutput();
@@ -139,6 +150,22 @@ describe("Pages _worker.js", () => {
139150
}
140151
});
141152

153+
// Serendipitously, this .env reading also works for `wrangler pages dev`.
154+
it("should read local dev vars from the .env file", async ({ expect }) => {
155+
const { ip, port, stop } = await runWranglerPagesDev(
156+
resolve(__dirname, ".."),
157+
"./workerjs-test",
158+
["--port=0", "--inspector-port=0", "--compatibility-date=2025-07-15"]
159+
);
160+
try {
161+
const response = await fetch(`http://${ip}:${port}/env`);
162+
const env = (await response.json()) as { FOO: string };
163+
expect(env.FOO).toBe("bar");
164+
} finally {
165+
await stop();
166+
}
167+
});
168+
142169
async function tryRename(
143170
basePath: string,
144171
from: string,
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import text from "./other-script";
22

33
export default {
4-
async fetch() {
5-
return new Response(text);
4+
async fetch(request, env) {
5+
if (request.url.endsWith("/env")) {
6+
return Response.json(env);
7+
} else {
8+
return new Response(text);
9+
}
610
},
711
};

0 commit comments

Comments
 (0)