|
1 | 1 | import { execSync } from "node:child_process"; |
| 2 | +import { randomUUID } from "node:crypto"; |
2 | 3 | import fs from "node:fs"; |
3 | 4 | import { readFile, writeFile } from "node:fs/promises"; |
4 | 5 | import os from "node:os"; |
5 | 6 | import { setTimeout } from "node:timers/promises"; |
6 | 7 | import { afterAll, beforeAll, describe, test, vi } from "vitest"; |
7 | | -import { fetchJson, runLongLived, seed, waitForReady } from "./helpers.js"; |
| 8 | +import { |
| 9 | + fetchJson, |
| 10 | + runCommand, |
| 11 | + runLongLived, |
| 12 | + seed, |
| 13 | + waitForReady, |
| 14 | +} from "./helpers.js"; |
8 | 15 |
|
9 | 16 | const isWindows = os.platform() === "win32"; |
10 | 17 | const commands = ["dev", "buildAndPreview"] as const; |
11 | 18 |
|
12 | | -// These tests focus on remote bindings which require an authed connection to the CF API |
13 | | -// They are skipped if you have not provided the necessary account id and api token. |
14 | | -describe |
15 | | - .skipIf( |
16 | | - isWindows || |
17 | | - !process.env.CLOUDFLARE_ACCOUNT_ID || |
18 | | - !process.env.CLOUDFLARE_API_TOKEN |
19 | | - ) |
20 | | - // Note: the reload test applies changes to the fixture files, so we do want the |
21 | | - // tests to run sequentially in order to avoid race conditions |
22 | | - .sequential("remote bindings tests", () => { |
23 | | - const remoteWorkerName = "tmp-e2e-vite-plugin-mixed-mode-remote-worker"; |
24 | | - const alternativeRemoteWorkerName = |
25 | | - "tmp-e2e-vite-plugin-mixed-mode-remote-worker-alt"; |
| 19 | +if ( |
| 20 | + isWindows || |
| 21 | + !process.env.CLOUDFLARE_ACCOUNT_ID || |
| 22 | + !process.env.CLOUDFLARE_API_TOKEN |
| 23 | +) { |
| 24 | + describe.skip( |
| 25 | + "Skipping remote bindings tests on Windows or without account credentials." |
| 26 | + ); |
| 27 | +} else { |
| 28 | + describe |
| 29 | + // Note: the reload test applies changes to the fixture files, so we do want the |
| 30 | + // tests to run sequentially in order to avoid race conditions |
| 31 | + .sequential("remote bindings tests", () => { |
| 32 | + const replacements = { |
| 33 | + "<<REMOTE_WORKER_PLACEHOLDER>>": `tmp-e2e-vite-remote-${randomUUID().split("-")[0]}`, |
| 34 | + "<<REMOTE_WORKER_PLACEHOLDER_ALT>>": `tmp-e2e-vite-remote-alt-${randomUUID().split("-")[0]}`, |
| 35 | + }; |
26 | 36 |
|
27 | | - const projectPath = seed("remote-bindings", "pnpm"); |
| 37 | + const projectPath = seed("remote-bindings", "pnpm", replacements); |
28 | 38 |
|
29 | | - beforeAll(() => { |
30 | | - const tmp = fs.mkdtempSync(`${os.tmpdir()}/vite-plugin-e2e-tmp`); |
31 | | - [ |
32 | | - { |
33 | | - name: remoteWorkerName, |
34 | | - content: |
35 | | - "export default { fetch() { return new Response('Hello from a remote worker'); } };", |
36 | | - }, |
37 | | - { |
38 | | - name: alternativeRemoteWorkerName, |
39 | | - content: |
40 | | - "export default { fetch() { return new Response('Hello from an alternative remote worker'); } };", |
41 | | - }, |
42 | | - ].forEach((worker) => { |
43 | | - fs.writeFileSync(`${tmp}/index.js`, worker.content); |
44 | | - execSync( |
45 | | - `npx wrangler deploy index.js --name ${worker.name} --compatibility-date 2025-01-01`, |
46 | | - { cwd: tmp } |
| 39 | + beforeAll(() => { |
| 40 | + runCommand(`npx wrangler deploy`, `${projectPath}/remote-worker`); |
| 41 | + runCommand(`npx wrangler deploy`, `${projectPath}/remote-worker-alt`); |
| 42 | + }, 35_000); |
| 43 | + |
| 44 | + afterAll(() => { |
| 45 | + runCommand( |
| 46 | + `npx wrangler delete --force`, |
| 47 | + `${projectPath}/remote-worker`, |
| 48 | + { canFail: true } |
| 49 | + ); |
| 50 | + runCommand( |
| 51 | + `npx wrangler delete --force`, |
| 52 | + `${projectPath}/remote-worker-alt`, |
| 53 | + { canFail: true } |
47 | 54 | ); |
48 | 55 | }); |
49 | | - }, 35_000); |
50 | 56 |
|
51 | | - afterAll(() => { |
52 | | - [remoteWorkerName, alternativeRemoteWorkerName].forEach((worker) => { |
53 | | - execSync(`npx wrangler delete --name ${worker}`); |
| 57 | + describe.each(commands)('with "%s" command', (command) => { |
| 58 | + test("can fetch from both local (/auxiliary) and remote workers", async ({ |
| 59 | + expect, |
| 60 | + }) => { |
| 61 | + const proc = await runLongLived("pnpm", command, projectPath); |
| 62 | + const url = await waitForReady(proc); |
| 63 | + expect(await fetchJson(url)).toEqual({ |
| 64 | + localWorkerResponse: { |
| 65 | + remoteWorkerResponse: "Hello from an alternative remote worker", |
| 66 | + }, |
| 67 | + remoteWorkerResponse: "Hello from a remote worker", |
| 68 | + }); |
| 69 | + }); |
54 | 70 | }); |
55 | | - }); |
56 | 71 |
|
57 | | - describe.each(commands)('with "%s" command', (command) => { |
58 | | - test("can fetch from both local (/auxiliary) and remote workers", async ({ |
59 | | - expect, |
60 | | - }) => { |
61 | | - const proc = await runLongLived("pnpm", command, projectPath); |
| 72 | + test("reflects changes applied during dev", async ({ expect }) => { |
| 73 | + const proc = await runLongLived("pnpm", "dev", projectPath); |
62 | 74 | const url = await waitForReady(proc); |
63 | 75 | expect(await fetchJson(url)).toEqual({ |
64 | 76 | localWorkerResponse: { |
65 | 77 | remoteWorkerResponse: "Hello from an alternative remote worker", |
66 | 78 | }, |
67 | 79 | remoteWorkerResponse: "Hello from a remote worker", |
68 | 80 | }); |
69 | | - }); |
70 | | - }); |
71 | 81 |
|
72 | | - test("reflects changes applied during dev", async ({ expect }) => { |
73 | | - const proc = await runLongLived("pnpm", "dev", projectPath); |
74 | | - const url = await waitForReady(proc); |
75 | | - expect(await fetchJson(url)).toEqual({ |
76 | | - localWorkerResponse: { |
77 | | - remoteWorkerResponse: "Hello from an alternative remote worker", |
78 | | - }, |
79 | | - remoteWorkerResponse: "Hello from a remote worker", |
80 | | - }); |
81 | | - |
82 | | - const entryWorkerPath = `${projectPath}/entry-worker/src/index.ts`; |
83 | | - const entryWorkerContent = await readFile(entryWorkerPath, "utf8"); |
| 82 | + const entryWorkerPath = `${projectPath}/entry-worker/src/index.ts`; |
| 83 | + const entryWorkerContent = await readFile(entryWorkerPath, "utf8"); |
84 | 84 |
|
85 | | - await writeFile( |
86 | | - entryWorkerPath, |
87 | | - entryWorkerContent |
88 | | - .replace( |
89 | | - "localWorkerResponse: await", |
90 | | - "localWorkerResponseJson: await" |
91 | | - ) |
92 | | - .replace( |
93 | | - "remoteWorkerResponse: await", |
94 | | - "remoteWorkerResponseText: await" |
95 | | - ), |
96 | | - "utf8" |
97 | | - ); |
| 85 | + await writeFile( |
| 86 | + entryWorkerPath, |
| 87 | + entryWorkerContent |
| 88 | + .replace( |
| 89 | + "localWorkerResponse: await", |
| 90 | + "localWorkerResponseJson: await" |
| 91 | + ) |
| 92 | + .replace( |
| 93 | + "remoteWorkerResponse: await", |
| 94 | + "remoteWorkerResponseText: await" |
| 95 | + ), |
| 96 | + "utf8" |
| 97 | + ); |
98 | 98 |
|
99 | | - await setTimeout(500); |
| 99 | + await setTimeout(500); |
100 | 100 |
|
101 | | - await vi.waitFor( |
102 | | - async () => { |
103 | | - expect(await fetchJson(url)).toEqual({ |
104 | | - localWorkerResponseJson: { |
105 | | - remoteWorkerResponse: "Hello from an alternative remote worker", |
106 | | - }, |
107 | | - remoteWorkerResponseText: "Hello from a remote worker", |
108 | | - }); |
109 | | - }, |
110 | | - { timeout: 5_000, interval: 250 } |
111 | | - ); |
| 101 | + await vi.waitFor( |
| 102 | + async () => { |
| 103 | + expect(await fetchJson(url)).toEqual({ |
| 104 | + localWorkerResponseJson: { |
| 105 | + remoteWorkerResponse: "Hello from an alternative remote worker", |
| 106 | + }, |
| 107 | + remoteWorkerResponseText: "Hello from a remote worker", |
| 108 | + }); |
| 109 | + }, |
| 110 | + { timeout: 5_000, interval: 250 } |
| 111 | + ); |
112 | 112 |
|
113 | | - await writeFile(entryWorkerPath, entryWorkerContent, "utf8"); |
| 113 | + await writeFile(entryWorkerPath, entryWorkerContent, "utf8"); |
114 | 114 |
|
115 | | - await vi.waitFor( |
116 | | - async () => { |
117 | | - expect(await fetchJson(url)).toEqual({ |
118 | | - localWorkerResponse: { |
119 | | - remoteWorkerResponse: "Hello from an alternative remote worker", |
120 | | - }, |
121 | | - remoteWorkerResponse: "Hello from a remote worker", |
122 | | - }); |
123 | | - }, |
124 | | - { timeout: 5_000, interval: 250 } |
125 | | - ); |
| 115 | + await vi.waitFor( |
| 116 | + async () => { |
| 117 | + expect(await fetchJson(url)).toEqual({ |
| 118 | + localWorkerResponse: { |
| 119 | + remoteWorkerResponse: "Hello from an alternative remote worker", |
| 120 | + }, |
| 121 | + remoteWorkerResponse: "Hello from a remote worker", |
| 122 | + }); |
| 123 | + }, |
| 124 | + { timeout: 5_000, interval: 250 } |
| 125 | + ); |
| 126 | + }); |
126 | 127 | }); |
127 | | - }); |
| 128 | +} |
0 commit comments