diff --git a/fixtures/node-app-pages/tests/index.test.ts b/fixtures/node-app-pages/tests/index.test.ts index 5b9df6f26e00..e9c1d1d2458a 100644 --- a/fixtures/node-app-pages/tests/index.test.ts +++ b/fixtures/node-app-pages/tests/index.test.ts @@ -12,12 +12,14 @@ describe("Pages Dev", () => { "public", ["--node-compat", "--port=0"] ); - const response = await fetch(`http://${ip}:${port}/stripe`); + try { + const response = await fetch(`http://${ip}:${port}/stripe`); - await expect(response.text()).resolves.toContain( - `"PATH":"path/to/some-file","STRIPE_OBJECT"` - ); - - await stop(); + await expect(response.text()).resolves.toContain( + `"PATH":"path/to/some-file","STRIPE_OBJECT"` + ); + } finally { + await stop(); + } }); }); diff --git a/fixtures/pages-functions-app/tests/index.test.ts b/fixtures/pages-functions-app/tests/index.test.ts index 6076f513e3e6..3c3ad7a6e045 100644 --- a/fixtures/pages-functions-app/tests/index.test.ts +++ b/fixtures/pages-functions-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Functions", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -20,7 +20,7 @@ describe("Pages Functions", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("renders static pages", async ({ expect }) => { diff --git a/fixtures/pages-functions-wasm-app/tests/index.test.ts b/fixtures/pages-functions-wasm-app/tests/index.test.ts index 9de41e8b75b7..4ab617ea67ec 100644 --- a/fixtures/pages-functions-wasm-app/tests/index.test.ts +++ b/fixtures/pages-functions-wasm-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Functions with wasm module imports", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages Functions with wasm module imports", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("should render static pages", async ({ expect }) => { diff --git a/fixtures/pages-functions-with-routes-app/tests/index.test.ts b/fixtures/pages-functions-with-routes-app/tests/index.test.ts index d24a5cd83c6a..a27f3acac751 100644 --- a/fixtures/pages-functions-with-routes-app/tests/index.test.ts +++ b/fixtures/pages-functions-with-routes-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Functions with custom _routes.json", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages Functions with custom _routes.json", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("should render static pages", async ({ expect }) => { diff --git a/fixtures/pages-simple-assets/tests/index.test.ts b/fixtures/pages-simple-assets/tests/index.test.ts index d51e4b2fa835..8971777f0770 100644 --- a/fixtures/pages-simple-assets/tests/index.test.ts +++ b/fixtures/pages-simple-assets/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, afterAll, beforeAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Functions", async () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages Functions", async () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("renders static pages", async ({ expect }) => { diff --git a/fixtures/pages-workerjs-and-functions-app/tests/index.test.ts b/fixtures/pages-workerjs-and-functions-app/tests/index.test.ts index 96d8fe49ffb4..0c57ef2a841f 100644 --- a/fixtures/pages-workerjs-and-functions-app/tests/index.test.ts +++ b/fixtures/pages-workerjs-and-functions-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages project with `_worker.js` and `/functions` directory", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages project with `_worker.js` and `/functions` directory", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("renders static pages", async ({ expect }) => { diff --git a/fixtures/pages-workerjs-app/tests/index.test.ts b/fixtures/pages-workerjs-app/tests/index.test.ts index 1eb40a4927f3..cefc89e00471 100644 --- a/fixtures/pages-workerjs-app/tests/index.test.ts +++ b/fixtures/pages-workerjs-app/tests/index.test.ts @@ -35,10 +35,13 @@ describe("Pages _worker.js", () => { "./workerjs-test", ["--no-bundle=false", "--port=0"] ); - await expect( - fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) - ).resolves.toContain("test"); - await stop(); + try { + await expect( + fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) + ).resolves.toContain("test"); + } finally { + await stop(); + } }); it("should not throw an error when the _worker.js file imports something if --bundle is true", async ({ @@ -49,9 +52,12 @@ describe("Pages _worker.js", () => { "./workerjs-test", ["--bundle", "--port=0"] ); - await expect( - fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) - ).resolves.toContain("test"); - await stop(); + try { + await expect( + fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) + ).resolves.toContain("test"); + } finally { + await stop(); + } }); }); diff --git a/fixtures/pages-workerjs-directory/tests/index.test.ts b/fixtures/pages-workerjs-directory/tests/index.test.ts index 94780a1b7632..02df3426086d 100644 --- a/fixtures/pages-workerjs-directory/tests/index.test.ts +++ b/fixtures/pages-workerjs-directory/tests/index.test.ts @@ -24,36 +24,41 @@ describe("Pages _worker.js/ directory", () => { "--r2=R2_REF=other_r2", ] ); - await expect( - fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) - ).resolves.toContain("Hello, world!"); - await expect( - fetch(`http://${ip}:${port}/wasm`).then((resp) => resp.text()) - ).resolves.toContain("3"); - await expect( - fetch(`http://${ip}:${port}/static-js`).then((resp) => resp.text()) - ).resolves.toEqual("static import text (via js): 'js static'"); - await expect( - fetch(`http://${ip}:${port}/static-mjs`).then((resp) => resp.text()) - ).resolves.toEqual("static import text (via mjs): 'mjs static'"); - await expect( - fetch(`http://${ip}:${port}/other-script.js`).then((resp) => resp.text()) - ).resolves.toContain("other-script-test"); - await expect( - fetch(`http://${ip}:${port}/other-other-script.mjs`).then((resp) => - resp.text() - ) - ).resolves.toContain("other-other-script-test"); - await expect( - fetch(`http://${ip}:${port}/d1`).then((resp) => resp.text()) - ).resolves.toContain('{"1":1}'); - await expect( - fetch(`http://${ip}:${port}/kv`).then((resp) => resp.text()) - ).resolves.toContain("saved"); - await expect( - fetch(`http://${ip}:${port}/r2`).then((resp) => resp.text()) - ).resolves.toContain("saved"); - await stop(); + try { + await expect( + fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) + ).resolves.toContain("Hello, world!"); + await expect( + fetch(`http://${ip}:${port}/wasm`).then((resp) => resp.text()) + ).resolves.toContain("3"); + await expect( + fetch(`http://${ip}:${port}/static-js`).then((resp) => resp.text()) + ).resolves.toEqual("static import text (via js): 'js static'"); + await expect( + fetch(`http://${ip}:${port}/static-mjs`).then((resp) => resp.text()) + ).resolves.toEqual("static import text (via mjs): 'mjs static'"); + await expect( + fetch(`http://${ip}:${port}/other-script.js`).then((resp) => + resp.text() + ) + ).resolves.toContain("other-script-test"); + await expect( + fetch(`http://${ip}:${port}/other-other-script.mjs`).then((resp) => + resp.text() + ) + ).resolves.toContain("other-other-script-test"); + await expect( + fetch(`http://${ip}:${port}/d1`).then((resp) => resp.text()) + ).resolves.toContain('{"1":1}'); + await expect( + fetch(`http://${ip}:${port}/kv`).then((resp) => resp.text()) + ).resolves.toContain("saved"); + await expect( + fetch(`http://${ip}:${port}/r2`).then((resp) => resp.text()) + ).resolves.toContain("saved"); + } finally { + await stop(); + } expect(existsSync(join(tmpDir, "./v3/d1/D1"))).toBeTruthy(); expect(existsSync(join(tmpDir, "./v3/d1/elsewhere"))).toBeTruthy(); diff --git a/fixtures/pages-workerjs-wasm-app/tests/index.test.ts b/fixtures/pages-workerjs-wasm-app/tests/index.test.ts index ef1c505acde6..a92423b8f163 100644 --- a/fixtures/pages-workerjs-wasm-app/tests/index.test.ts +++ b/fixtures/pages-workerjs-wasm-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Advanced Mode with wasm module imports", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages Advanced Mode with wasm module imports", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("should render static pages", async ({ expect }) => { diff --git a/fixtures/pages-workerjs-with-routes-app/tests/index.test.ts b/fixtures/pages-workerjs-with-routes-app/tests/index.test.ts index de4638cbf67f..141b91585420 100644 --- a/fixtures/pages-workerjs-with-routes-app/tests/index.test.ts +++ b/fixtures/pages-workerjs-with-routes-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; describe("Pages Advanced Mode with custom _routes.json", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerPagesDev( @@ -15,7 +15,7 @@ describe("Pages Advanced Mode with custom _routes.json", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("renders static pages", async ({ expect }) => { diff --git a/fixtures/remix-pages-app/tests/index.test.ts b/fixtures/remix-pages-app/tests/index.test.ts index 2859c3e89c9e..1ce0e61971f2 100644 --- a/fixtures/remix-pages-app/tests/index.test.ts +++ b/fixtures/remix-pages-app/tests/index.test.ts @@ -9,7 +9,7 @@ const isWindows = process.platform === "win32"; describe("Remix", () => { let ip: string; let port: number; - let stop: () => void; + let stop: (() => Promise) | undefined; beforeAll(async () => { spawnSync("npm", ["run", "build"], { @@ -23,7 +23,9 @@ describe("Remix", () => { )); }); - afterAll(async () => await stop()); + afterAll(async () => { + await stop?.(); + }); it("renders", async ({ expect }) => { const response = await fetch(`http://${ip}:${port}/`); diff --git a/fixtures/shared/src/run-wrangler-long-lived.ts b/fixtures/shared/src/run-wrangler-long-lived.ts index 9136998c59ab..70487f9f5e75 100644 --- a/fixtures/shared/src/run-wrangler-long-lived.ts +++ b/fixtures/shared/src/run-wrangler-long-lived.ts @@ -29,25 +29,46 @@ export async function runWranglerDev(cwd: string, options: string[]) { } async function runLongLivedWrangler(command: string[], cwd: string) { + let settledReadyPromise = false; let resolveReadyPromise: (value: { ip: string; port: number }) => void; + let rejectReadyPromise: (reason: unknown) => void; - const ready = new Promise<{ ip: string; port: number }>( - (resolve) => (resolveReadyPromise = resolve) - ); + const ready = new Promise<{ ip: string; port: number }>((resolve, reject) => { + resolveReadyPromise = resolve; + rejectReadyPromise = reject; + }); const wranglerProcess = fork( "../../packages/wrangler/bin/wrangler.js", command, { - stdio: ["ignore", "ignore", "ignore", "ipc"], - // If you have a test timing out unexpectedly, then try using this line to find out what is happening in Wrangler. - // stdio: ["inherit", "inherit", "inherit", "ipc"], + stdio: [/*stdin*/ "ignore", /*stdout*/ "pipe", /*stderr*/ "pipe", "ipc"], cwd, } ).on("message", (message) => { + if (settledReadyPromise) return; + settledReadyPromise = true; + clearTimeout(timeoutHandle); resolveReadyPromise(JSON.parse(message.toString())); }); + const chunks: Buffer[] = []; + wranglerProcess.stdout?.on("data", (chunk) => chunks.push(chunk)); + wranglerProcess.stderr?.on("data", (chunk) => chunks.push(chunk)); + + const timeoutHandle = setTimeout(() => { + if (settledReadyPromise) return; + settledReadyPromise = true; + const separator = "=".repeat(80); + const message = [ + "Timed out starting long-lived Wrangler:", + separator, + Buffer.concat(chunks).toString(), + separator, + ].join("\n"); + rejectReadyPromise(new Error(message)); + }, 10_000); + async function stop() { return new Promise((resolve, reject) => { wranglerProcess.once("exit", (code) => { diff --git a/fixtures/worker-app/tests/index.test.ts b/fixtures/worker-app/tests/index.test.ts index 8606ad033dce..679c0f7d2977 100644 --- a/fixtures/worker-app/tests/index.test.ts +++ b/fixtures/worker-app/tests/index.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeAll, afterAll } from "vitest"; import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; describe("'wrangler dev' correctly renders pages", () => { - let ip: string, port: number, stop: () => Promise; + let ip: string, port: number, stop: (() => Promise) | undefined; beforeAll(async () => { ({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ @@ -14,7 +14,7 @@ describe("'wrangler dev' correctly renders pages", () => { }); afterAll(async () => { - await stop(); + await stop?.(); }); it("renders ", async ({ expect }) => {