diff --git a/packages/wrangler/e2e/dev-env.test.ts b/packages/wrangler/e2e/dev-env.test.ts index a5bbcd893553..7235e59b6c22 100644 --- a/packages/wrangler/e2e/dev-env.test.ts +++ b/packages/wrangler/e2e/dev-env.test.ts @@ -27,72 +27,72 @@ describe("switching runtimes", () => { `, "index.mjs": dedent/*javascript*/ ` import { setTimeout } from "timers/promises"; - import { unstable_DevEnv as DevEnv } from "${WRANGLER_IMPORT}"; + import { unstable_startWorker as startWorker } from "${WRANGLER_IMPORT}"; const firstRemote = process.argv[2] === "remote"; - const devEnv = new DevEnv(); + let worker; + try { + worker = await startWorker({ + name: "worker", + entrypoint: "index.ts", + compatibilityFlags: ["nodejs_compat"], + compatibilityDate: "2023-10-01", + bindings: { + REMOTE: { + type: "json", + value: firstRemote, + }, + ORDER: { + type: "plain_text", + value: "1", + }, + }, + dev: { + remote: firstRemote, + auth: { + accountId: process.env.CLOUDFLARE_ACCOUNT_ID, + apiToken: process.env.CLOUDFLARE_API_TOKEN, + }, + }, + }); - let config = { - name: "worker", - entrypoint: "index.ts", - compatibilityFlags: ["nodejs_compat"], - compatibilityDate: "2023-10-01", - bindings: { - REMOTE: { - type: "json", - value: firstRemote, - }, - ORDER: { - type: "plain_text", - value: "1", - }, - }, - dev: { - remote: firstRemote, - auth: { - accountId: process.env.CLOUDFLARE_ACCOUNT_ID, - apiToken: process.env.CLOUDFLARE_API_TOKEN, - }, - }, - }; - void devEnv.config.set(config); - - const { url } = await devEnv.proxy.ready.promise; - console.log(await fetch(url).then((r) => r.text())); - - void devEnv.config.patch({ - bindings: { - REMOTE: { - type: "json", - value: !firstRemote, - }, - ORDER: { - type: "plain_text", - value: "2", - }, - }, - dev: { - ...config.dev, - remote: !firstRemote, - }, - }); + const url = await worker.url; + console.log(await fetch(url).then((r) => r.text())); - // Give the config some time to propagate - await setTimeout(500); + await worker.patchConfig({ + bindings: { + REMOTE: { + type: "json", + value: !firstRemote, + }, + ORDER: { + type: "plain_text", + value: "2", + }, + }, + dev: { + ...worker.config.dev, + remote: !firstRemote, + }, + }); - console.log(await fetch(url).then((r) => r.text())); + // Give the config some time to propagate + await setTimeout(500); - await devEnv.teardown(); - process.exit(0); - `, + console.log(await fetch(url).then((r) => r.text())); + } finally { + await worker?.dispose(); + process.exit(0); + } + `, "package.json": dedent` - { - "name": "ai-app", - "version": "0.0.0", - "private": true - } - `, + { + "name": "ai-app", + "version": "0.0.0", + "private": true + } + `, }); }); it("can switch from local to remote", async () => { diff --git a/packages/wrangler/e2e/startWorker.test.ts b/packages/wrangler/e2e/startWorker.test.ts index e188444a65a0..e053a5e02b1f 100644 --- a/packages/wrangler/e2e/startWorker.test.ts +++ b/packages/wrangler/e2e/startWorker.test.ts @@ -30,16 +30,15 @@ function waitForMessageContaining(ws: WebSocket, value: string): Promise { describe.each(OPTIONS)("DevEnv", ({ remote }) => { let helper: WranglerE2ETestHelper; let wrangler: Wrangler; - let DevEnv: Wrangler["unstable_DevEnv"]; + let startWorker: Wrangler["unstable_startWorker"]; beforeEach(async () => { helper = new WranglerE2ETestHelper(); wrangler = await helper.importWrangler(); - DevEnv = wrangler.unstable_DevEnv; + startWorker = wrangler.unstable_startWorker; }); it("ProxyWorker buffers requests while runtime reloads", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); const script = dedent` export default { @@ -53,7 +52,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { "src/index.ts": script, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), dev: { remote }, @@ -71,8 +70,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { await expect(res.text()).resolves.toBe("body:2"); }); it("InspectorProxyWorker discovery endpoints + devtools websocket connection", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); const script = dedent` export default { @@ -88,7 +86,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { "src/index.ts": script, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -145,8 +143,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { await executionContextClearedPromise; }); it("InspectorProxyWorker rejects unauthorised requests", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); await helper.seed({ "src/index.ts": dedent` @@ -158,7 +155,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { `, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -185,8 +182,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { ws.close(); }); it("User worker exception", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); await helper.seed({ "src/index.ts": dedent` @@ -198,7 +194,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { `, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -265,8 +261,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { await expect(res.text()).resolves.toBe("body:3"); }); it("config.dev.{server,inspector} changes, restart the server instance", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); await helper.seed({ "src/index.ts": dedent` @@ -278,7 +273,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { `, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -317,8 +312,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { ).rejects.toThrowError("fetch failed"); }); it("liveReload", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); await helper.seed({ "src/index.ts": dedent` @@ -332,7 +326,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { `, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -394,8 +388,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { expect(resText).not.toEqual(expect.stringMatching(scriptRegex)); }); it("urlOverrides take effect in the UserWorker", async (t) => { - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); await helper.seed({ "src/index.ts": dedent` @@ -407,7 +400,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { `, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"), @@ -445,8 +438,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { // so that we can guarantee the race condition is hit // when workerd is eventually terminated - const devEnv = new DevEnv(); - t.onTestFinished(() => devEnv.teardown()); + t.onTestFinished(() => worker?.dispose()); const script = dedent` export default { @@ -466,7 +458,7 @@ describe.each(OPTIONS)("DevEnv", ({ remote }) => { "src/index.ts": script, }); - const worker = await devEnv.startWorker({ + const worker = await startWorker({ name: "test-worker", entrypoint: path.resolve(helper.tmpPath, "src/index.ts"),