|
1 | 1 | import { execSync } from "child_process"; |
| 2 | +import getPort from "get-port"; |
2 | 3 | import dedent from "ts-dedent"; |
3 | 4 | import { fetch } from "undici"; |
4 | 5 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
@@ -140,7 +141,7 @@ describe("unstable_dev()", () => { |
140 | 141 | }); |
141 | 142 |
|
142 | 143 | describe.each([ |
143 | | - { cmd: "wrangler dev --x-registry" }, |
| 144 | + { cmd: "wrangler dev" }, |
144 | 145 | { cmd: "wrangler dev --no-x-registry" }, |
145 | 146 | ])("dev registry $cmd", ({ cmd }) => { |
146 | 147 | let workerName: string; |
@@ -450,4 +451,121 @@ describe.each([ |
450 | 451 | ); |
451 | 452 | }); |
452 | 453 | }); |
| 454 | + |
| 455 | + describe("pages dev", () => { |
| 456 | + beforeEach(async () => { |
| 457 | + await baseSeed(a, { |
| 458 | + "wrangler.toml": dedent` |
| 459 | + name = "${workerName}" |
| 460 | + pages_build_output_dir = "dist" |
| 461 | + compatibility_date = "2023-01-01" |
| 462 | +
|
| 463 | + [[services]] |
| 464 | + binding = "BEE" |
| 465 | + service = '${workerName2}' |
| 466 | + `, |
| 467 | + "dist/_worker.js": dedent/* javascript */ `export default { |
| 468 | + fetch(req, env) { |
| 469 | + const url = new URL(req.url) |
| 470 | + if (url.pathname === "/service") { |
| 471 | + return env.BEE.fetch(req); |
| 472 | + } |
| 473 | + return new Response("Hello from Pages") |
| 474 | + }, |
| 475 | + };`, |
| 476 | + }); |
| 477 | + }); |
| 478 | + it("can fetch b", async () => { |
| 479 | + const worker = helper.runLongLived(cmd, { cwd: b }); |
| 480 | + |
| 481 | + const { url } = await worker.waitForReady(5_000); |
| 482 | + |
| 483 | + await expect(fetch(url).then((r) => r.text())).resolves.toBe( |
| 484 | + "hello world" |
| 485 | + ); |
| 486 | + }); |
| 487 | + |
| 488 | + it("can fetch a (pages project)", async () => { |
| 489 | + const port = await getPort(); |
| 490 | + const worker = helper.runLongLived( |
| 491 | + `${cmd.replace("wrangler dev", "wrangler pages dev")} --port ${port}`, |
| 492 | + { cwd: a } |
| 493 | + ); |
| 494 | + |
| 495 | + const { url } = await worker.waitForReady(5_000); |
| 496 | + |
| 497 | + await expect(fetch(url).then((r) => r.text())).resolves.toBe( |
| 498 | + "Hello from Pages" |
| 499 | + ); |
| 500 | + }); |
| 501 | + |
| 502 | + it("can fetch b through a (start b, start a)", async () => { |
| 503 | + const workerB = helper.runLongLived(cmd, { cwd: b }); |
| 504 | + // We don't need b's URL, but ensure that b starts up before a |
| 505 | + await workerB.waitForReady(5_000); |
| 506 | + |
| 507 | + const port = await getPort(); |
| 508 | + const workerA = helper.runLongLived( |
| 509 | + `${cmd.replace("wrangler dev", "wrangler pages dev")} --port ${port}`, |
| 510 | + { cwd: a } |
| 511 | + ); |
| 512 | + const { url } = await workerA.waitForReady(5_000); |
| 513 | + |
| 514 | + await vi.waitFor( |
| 515 | + async () => |
| 516 | + await expect(fetchText(`${url}/service`)).resolves.toBe( |
| 517 | + "hello world" |
| 518 | + ), |
| 519 | + { interval: 1000, timeout: 10_000 } |
| 520 | + ); |
| 521 | + |
| 522 | + expect(normalizeOutput(workerA.currentOutput)).toContain( |
| 523 | + "bindings connect to other `wrangler dev` processes running locally" |
| 524 | + ); |
| 525 | + }); |
| 526 | + |
| 527 | + it("can fetch b through a (start a, start b)", async () => { |
| 528 | + const port = await getPort(); |
| 529 | + const workerA = helper.runLongLived( |
| 530 | + `${cmd.replace("wrangler dev", "wrangler pages dev")} --port ${port}`, |
| 531 | + { cwd: a } |
| 532 | + ); |
| 533 | + const { url } = await workerA.waitForReady(5_000); |
| 534 | + |
| 535 | + const workerB = helper.runLongLived(cmd, { cwd: b }); |
| 536 | + await workerB.waitForReady(5_000); |
| 537 | + |
| 538 | + await vi.waitFor( |
| 539 | + async () => |
| 540 | + await expect(fetchText(`${url}/service`)).resolves.toBe( |
| 541 | + "hello world" |
| 542 | + ), |
| 543 | + { interval: 1000, timeout: 10_000 } |
| 544 | + ); |
| 545 | + }); |
| 546 | + |
| 547 | + it("can fetch b through a (start a, start b) w/o config file", async () => { |
| 548 | + await baseSeed(a, { |
| 549 | + "wrangler.toml": dedent` |
| 550 | + `, |
| 551 | + }); |
| 552 | + const port = await getPort(); |
| 553 | + const workerA = helper.runLongLived( |
| 554 | + `${cmd.replace("wrangler dev", "wrangler pages dev")} dist --service BEE=${workerName2} --port ${port}`, |
| 555 | + { cwd: a } |
| 556 | + ); |
| 557 | + const { url } = await workerA.waitForReady(5_000); |
| 558 | + |
| 559 | + const workerB = helper.runLongLived(cmd, { cwd: b }); |
| 560 | + await workerB.waitForReady(5_000); |
| 561 | + |
| 562 | + await vi.waitFor( |
| 563 | + async () => |
| 564 | + await expect(fetchText(`${url}/service`)).resolves.toBe( |
| 565 | + "hello world" |
| 566 | + ), |
| 567 | + { interval: 1000, timeout: 10_000 } |
| 568 | + ); |
| 569 | + }); |
| 570 | + }); |
453 | 571 | }); |
0 commit comments