Skip to content

Commit cb072d0

Browse files
fix specified environments being ignored for remote bindings in some cases
1 parent 9b61f44 commit cb072d0

File tree

16 files changed

+299
-35
lines changed

16 files changed

+299
-35
lines changed

.changeset/cruel-ears-heal.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
update `maybeStartOrUpdateRemoteProxySession` config argument (to allow callers to specify an environment)
6+
7+
Before this change `maybeStartOrUpdateRemoteProxySession` could be called with either the path to a wrangler config file or the configuration of a worker. The former override however did not allow the caller to specify an environment, so the `maybeStartOrUpdateRemoteProxySession` API has been updated so that in the wrangler config case an object (with the path and a potential environment) needs to be passed instead.
8+
9+
For example, before callers could invoke the function in the following way
10+
11+
```ts
12+
await maybeStartOrUpdateRemoteProxySession(configPath);
13+
```
14+
15+
note that there is no way to tell the function what environment to use when parsing the wrangle configuration.
16+
17+
Now callers will instead call the function in the following way:
18+
19+
```ts
20+
await maybeStartOrUpdateRemoteProxySession({
21+
path: configPath,
22+
environment: targetEnvironment,
23+
});
24+
```
25+
26+
note that now a target environment can be specified.

.changeset/giant-laws-play.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+
fix `getPlatformProxy` not taking into account the potentially specified environment for remote bindings

.changeset/stale-dogs-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
fix the potentially specified environment not being taken into account for remote bindings
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
fetch() {
3+
return new Response(
4+
"Hello from a remote Worker, defined for the staging environment, part of the getPlatformProxy remote bindings fixture!"
5+
);
6+
},
7+
};

fixtures/get-platform-proxy-remote-bindings/tests/index.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const execOptions = {
1212
env: { ...process.env, ...auth },
1313
} as const;
1414
const remoteWorkerName = `tmp-e2e-worker-test-remote-bindings-${randomUUID().split("-")[0]}`;
15+
const remoteStagingWorkerName = `tmp-e2e-staging-worker-test-remote-bindings-${randomUUID().split("-")[0]}`;
1516
const remoteKvName = `tmp-e2e-remote-kv-test-remote-bindings-${randomUUID().split("-")[0]}`;
1617

1718
if (auth) {
@@ -27,6 +28,19 @@ if (auth) {
2728
throw new Error(`Failed to deploy ${remoteWorkerName}`);
2829
}
2930

31+
const stagingDeployOut = execSync(
32+
`pnpm wrangler deploy remote-worker.staging.js --name ${remoteStagingWorkerName} --compatibility-date 2025-06-19`,
33+
execOptions
34+
);
35+
36+
if (
37+
!new RegExp(`Deployed\\s+${remoteStagingWorkerName}\\b`).test(
38+
stagingDeployOut
39+
)
40+
) {
41+
throw new Error(`Failed to deploy ${remoteStagingWorkerName}`);
42+
}
43+
3044
const kvAddOut = execSync(
3145
`pnpm wrangler kv namespace create ${remoteKvName}`,
3246
execOptions
@@ -66,6 +80,24 @@ if (auth) {
6680
experimental_remote: true,
6781
},
6882
],
83+
env: {
84+
staging: {
85+
services: [
86+
{
87+
binding: "MY_WORKER",
88+
service: remoteStagingWorkerName,
89+
experimental_remote: true,
90+
},
91+
],
92+
kv_namespaces: [
93+
{
94+
binding: "MY_KV",
95+
id: remoteKvId,
96+
experimental_remote: true,
97+
},
98+
],
99+
},
100+
},
69101
},
70102
undefined,
71103
2
@@ -76,6 +108,10 @@ if (auth) {
76108

77109
afterAll(() => {
78110
execSync(`pnpm wrangler delete --name ${remoteWorkerName}`, execOptions);
111+
execSync(
112+
`pnpm wrangler delete --name ${remoteStagingWorkerName}`,
113+
execOptions
114+
);
79115
execSync(
80116
`pnpm wrangler kv namespace delete --namespace-id=${remoteKvId}`,
81117
execOptions
@@ -107,6 +143,31 @@ if (auth) {
107143
await dispose();
108144
});
109145

146+
test("getPlatformProxy works with remote bindings specified in an environment", async () => {
147+
vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", auth.CLOUDFLARE_ACCOUNT_ID);
148+
vi.stubEnv("CLOUDFLARE_API_TOKEN", auth.CLOUDFLARE_API_TOKEN);
149+
const { env, dispose } = await getPlatformProxy<{
150+
MY_WORKER: Fetcher;
151+
MY_KV: KVNamespace;
152+
}>({
153+
configPath: "./.tmp/wrangler.json",
154+
experimental: { remoteBindings: true },
155+
environment: "staging",
156+
});
157+
158+
const workerText = await (
159+
await env.MY_WORKER.fetch("http://example.com")
160+
).text();
161+
expect(workerText).toEqual(
162+
"Hello from a remote Worker, defined for the staging environment, part of the getPlatformProxy remote bindings fixture!"
163+
);
164+
165+
const kvValue = await env.MY_KV.get("test-key");
166+
expect(kvValue).toEqual("remote-kv-value");
167+
168+
await dispose();
169+
});
170+
110171
test("getPlatformProxy does not work with remote bindings if the experimental remoteBindings flag is not turned on", async () => {
111172
const { env, dispose } = await getPlatformProxy<{
112173
MY_WORKER: Fetcher;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Fetcher } from "@cloudflare/workers-types/experimental";
2+
3+
declare module "cloudflare:test" {
4+
interface ProvidedEnv {
5+
MY_WORKER: Fetcher;
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
fetch() {
3+
return new Response(
4+
"Hello from a remote Worker, defined for the staging environment, part of the vitest-pool-workers remote bindings fixture!"
5+
);
6+
},
7+
};

fixtures/vitest-pool-workers-remote-bindings/run-tests.mjs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/**
22
* This fixture is particular since it needs to communicate with remote resources, namely
3-
* a remote worker.
3+
* two remote workers.
44
*
5-
* This script is used to deploy a remote worker and run the fixture using said worker.
6-
*
7-
* Alternatively you can simply deploy, using your account, the `./remote-worker.js` file as
8-
* a worker named `my-worker-test` and directly run the fixture using vitest.
5+
* This script is used to deploy the remote workers and run the fixture using said workers
6+
* with the appropriate vitest configurations.
97
*/
108
import { execSync } from "child_process";
119
import { randomUUID } from "crypto";
@@ -22,11 +20,17 @@ rmSync("./.tmp", { recursive: true, force: true });
2220
cpSync("./src", "./.tmp/src", { recursive: true });
2321
cpSync("./test", "./.tmp/test", { recursive: true });
2422
cpSync("./vitest.workers.config.ts", "./.tmp/vitest.workers.config.ts");
23+
cpSync(
24+
"./vitest.workers.config.staging.ts",
25+
"./.tmp/vitest.workers.config.staging.ts"
26+
);
2527

2628
const remoteWorkerName = `tmp-e2e-worker-test-remote-bindings-${randomUUID().split("-")[0]}`;
29+
const remoteStagingWorkerName = `tmp-e2e-staging-worker-test-remote-bindings-${randomUUID().split("-")[0]}`;
2730

2831
const wranglerJson = JSON.parse(readFileSync("./wrangler.json", "utf8"));
2932
wranglerJson.services[0].service = remoteWorkerName;
33+
wranglerJson.env.staging.services[0].service = remoteStagingWorkerName;
3034

3135
writeFileSync(
3236
"./.tmp/wrangler.json",
@@ -60,12 +64,49 @@ if (!new RegExp(`Deployed\\s+${remoteWorkerName}\\b`).test(`${deployOut}`)) {
6064
throw new Error(`Failed to deploy ${remoteWorkerName}`);
6165
}
6266

67+
writeFileSync(
68+
"./.tmp/remote-wrangler.staging.json",
69+
JSON.stringify(
70+
{
71+
name: remoteStagingWorkerName,
72+
main: "../remote-worker.staging.js",
73+
compatibility_date: "2025-06-01",
74+
},
75+
undefined,
76+
2
77+
),
78+
"utf8"
79+
);
80+
81+
const deployStagingOut = execSync(
82+
"pnpm wrangler deploy -c .tmp/remote-wrangler.staging.json",
83+
{
84+
stdio: "pipe",
85+
cwd: "./.tmp",
86+
env,
87+
}
88+
);
89+
if (
90+
!new RegExp(`Deployed\\s+${remoteStagingWorkerName}\\b`).test(
91+
`${deployStagingOut}`
92+
)
93+
) {
94+
throw new Error(`Failed to deploy ${remoteStagingWorkerName}`);
95+
}
96+
6397
try {
6498
execSync("pnpm test:vitest --config ./.tmp/vitest.workers.config.ts", {
6599
env,
66100
});
101+
execSync(
102+
"pnpm test:vitest --config ./.tmp/vitest.workers.config.staging.ts",
103+
{
104+
env,
105+
}
106+
);
67107
} finally {
68108
execSync(`pnpm wrangler delete --name ${remoteWorkerName}`, { env });
109+
execSync(`pnpm wrangler delete --name ${remoteStagingWorkerName}`, { env });
69110
rmSync("./.tmp", { recursive: true, force: true });
70111
}
71112

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
createExecutionContext,
3+
env,
4+
SELF,
5+
waitOnExecutionContext,
6+
} from "cloudflare:test";
7+
import { describe, expect, test } from "vitest";
8+
9+
describe("Vitest pool workers remote bindings with a staging environment", () => {
10+
test(
11+
"fetching unit-style from a remote service binding",
12+
{ timeout: 50_000 },
13+
async () => {
14+
const response = await env.MY_WORKER.fetch("http://example.com");
15+
const ctx = createExecutionContext();
16+
await waitOnExecutionContext(ctx);
17+
expect(await response.text()).toMatchInlineSnapshot(
18+
`"Hello from a remote Worker, defined for the staging environment, part of the vitest-pool-workers remote bindings fixture!"`
19+
);
20+
}
21+
);
22+
23+
test("fetching integration-style from the local worker (which uses remote bindings)", async () => {
24+
const response = await SELF.fetch("https://example.com");
25+
expect(await response.text()).toMatchInlineSnapshot(
26+
`"Response from remote worker: Hello from a remote Worker, defined for the staging environment, part of the vitest-pool-workers remote bindings fixture!"`
27+
);
28+
});
29+
});

fixtures/vitest-pool-workers-remote-bindings/test/index.spec.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@ import {
33
env,
44
SELF,
55
waitOnExecutionContext,
6-
// @ts-ignore
76
} from "cloudflare:test";
8-
import { describe, expect, it } from "vitest";
7+
import { describe, expect, test } from "vitest";
98

10-
describe("Hello World worker", () => {
11-
it(
12-
"responds with Hello World! (unit style)",
9+
describe("Vitest pool workers remote bindings", () => {
10+
test(
11+
"fetching unit-style from a remote service binding",
1312
{ timeout: 50_000 },
1413
async () => {
15-
const request = new Request("http://example.com");
14+
const response = await env.MY_WORKER.fetch("http://example.com");
1615
const ctx = createExecutionContext();
17-
debugger;
18-
const response = await env.MY_WORKER.fetch(request, env, ctx);
1916
await waitOnExecutionContext(ctx);
2017
expect(await response.text()).toMatchInlineSnapshot(
2118
`"Hello from a remote Worker part of the vitest-pool-workers remote bindings fixture!"`
2219
);
2320
}
2421
);
2522

26-
it("responds with Hello World! (integration style)", async () => {
23+
test("fetching integration-style from the local worker (which uses remote bindings)", async () => {
2724
const response = await SELF.fetch("https://example.com");
2825
expect(await response.text()).toMatchInlineSnapshot(
2926
`"Response from remote worker: Hello from a remote Worker part of the vitest-pool-workers remote bindings fixture!"`

0 commit comments

Comments
 (0)