wrangler 0.0.0-e22f9d7 → 0.0.0-e452a04

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +31 -15
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +112 -28
  5. package/pages/functions/filepath-routing.ts +88 -131
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +19 -33
  8. package/src/__tests__/configuration.test.ts +2217 -0
  9. package/src/__tests__/dev.test.tsx +397 -59
  10. package/src/__tests__/dev2.test.tsx +152 -0
  11. package/src/__tests__/guess-worker-format.test.ts +58 -0
  12. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  13. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  14. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  15. package/src/__tests__/helpers/mock-account-id.ts +44 -0
  16. package/src/__tests__/helpers/mock-bin.ts +36 -0
  17. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +52 -16
  18. package/src/__tests__/helpers/mock-console.ts +70 -0
  19. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  20. package/src/__tests__/helpers/mock-kv.ts +40 -0
  21. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  22. package/src/__tests__/helpers/mock-user.ts +27 -0
  23. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  24. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  25. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  26. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  27. package/src/__tests__/https-options.test.ts +127 -0
  28. package/src/__tests__/index.test.ts +779 -75
  29. package/src/__tests__/jest.setup.ts +53 -6
  30. package/src/__tests__/kv.test.ts +734 -437
  31. package/src/__tests__/logout.test.ts +50 -0
  32. package/src/__tests__/package-manager.test.ts +206 -0
  33. package/src/__tests__/pages.test.ts +44 -0
  34. package/src/__tests__/parse.test.ts +322 -0
  35. package/src/__tests__/publish.test.ts +2252 -0
  36. package/src/__tests__/r2.test.ts +198 -0
  37. package/src/__tests__/route.test.ts +45 -0
  38. package/src/__tests__/secret.test.ts +425 -0
  39. package/src/__tests__/sentry.test.ts +146 -0
  40. package/src/__tests__/tail.test.ts +564 -0
  41. package/src/__tests__/whoami.test.tsx +86 -0
  42. package/src/bundle.ts +142 -0
  43. package/src/cfetch/index.ts +21 -19
  44. package/src/cfetch/internal.ts +84 -18
  45. package/src/cli.ts +10 -0
  46. package/src/config/README.md +107 -0
  47. package/src/config/config.ts +279 -0
  48. package/src/config/diagnostics.ts +80 -0
  49. package/src/config/environment.ts +305 -0
  50. package/src/config/index.ts +52 -0
  51. package/src/config/validation-helpers.ts +388 -0
  52. package/src/config/validation.ts +1027 -0
  53. package/src/{api/preview.ts → create-worker-preview.ts} +61 -23
  54. package/src/create-worker-upload-form.ts +217 -0
  55. package/src/dev/dev.tsx +357 -0
  56. package/src/dev/local.tsx +241 -0
  57. package/src/dev/remote.tsx +201 -0
  58. package/src/dev/use-esbuild.ts +100 -0
  59. package/src/dialogs.tsx +2 -2
  60. package/src/entry.ts +159 -0
  61. package/src/environment-variables.ts +33 -0
  62. package/src/https-options.ts +122 -0
  63. package/src/index.tsx +1145 -790
  64. package/src/inspect.ts +103 -25
  65. package/src/{kv.tsx → kv/index.tsx} +119 -56
  66. package/src/module-collection.ts +215 -26
  67. package/src/open-in-browser.ts +25 -0
  68. package/src/package-manager.ts +126 -0
  69. package/src/pages.tsx +49 -35
  70. package/src/parse.ts +183 -0
  71. package/src/paths.ts +26 -0
  72. package/src/proxy.ts +175 -39
  73. package/src/publish.ts +288 -300
  74. package/src/r2.ts +50 -0
  75. package/src/reporting.ts +131 -0
  76. package/src/selfsigned.d.ts +29 -0
  77. package/src/sites.tsx +181 -61
  78. package/src/tail/filters.ts +279 -0
  79. package/src/tail/index.ts +299 -0
  80. package/src/tail/printing.ts +62 -0
  81. package/src/user.tsx +141 -103
  82. package/src/whoami.tsx +69 -0
  83. package/src/{api/worker.ts → worker.ts} +45 -37
  84. package/templates/new-worker.js +15 -0
  85. package/templates/new-worker.ts +15 -0
  86. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  87. package/templates/tsconfig.json +105 -0
  88. package/wrangler-dist/cli.js +128965 -112680
  89. package/wrangler-dist/cli.js.map +3 -3
  90. package/src/__tests__/run-in-tmp.ts +0 -19
  91. package/src/__tests__/run-wrangler.ts +0 -32
  92. package/src/api/form_data.ts +0 -131
  93. package/src/config.ts +0 -487
  94. package/src/dev.tsx +0 -784
  95. package/src/tail.tsx +0 -73
@@ -1,67 +1,405 @@
1
- import { render } from "ink-testing-library";
1
+ import * as fs from "node:fs";
2
+ import { readFileSync } from "node:fs";
2
3
  import patchConsole from "patch-console";
3
- import React from "react";
4
- import Dev from "../dev";
5
- import type { DevProps } from "../dev";
4
+ import Dev from "../dev/dev";
5
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
6
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
7
+ import { mockConsoleMethods } from "./helpers/mock-console";
8
+ import { runInTempDir } from "./helpers/run-in-tmp";
9
+ import { runWrangler } from "./helpers/run-wrangler";
10
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
6
11
 
7
12
  describe("Dev component", () => {
8
- let restoreConsole;
9
- beforeEach(() => (restoreConsole = patchConsole(() => {})));
10
- afterEach(() => restoreConsole());
11
-
12
- it("should throw if format is service-worker and there is a public directory", () => {
13
- const { lastFrame } = renderDev({
14
- format: "service-worker",
15
- accountId: "some-account-id",
16
- public: "some/public/path",
17
- });
18
- expect(lastFrame().split("\n").slice(0, 2).join("\n"))
19
- .toMatchInlineSnapshot(`
20
- "Something went wrong:
21
- Error: You cannot use the service worker format with a \`public\` directory."
22
- `);
13
+ mockAccountId();
14
+ mockApiToken();
15
+ runInTempDir();
16
+ const std = mockConsoleMethods();
17
+ afterEach(() => {
18
+ (Dev as jest.Mock).mockClear();
19
+ patchConsole(() => {});
20
+ unsetAllMocks();
21
+ });
22
+
23
+ describe("entry-points", () => {
24
+ it("should error if there is no entry-point specified", async () => {
25
+ writeWranglerToml();
26
+
27
+ await expect(
28
+ runWrangler("dev")
29
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
30
+ `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
31
+ );
32
+
33
+ expect(std.out).toMatchInlineSnapshot(`""`);
34
+ expect(std.err).toMatchInlineSnapshot(`
35
+ "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
36
+
37
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
38
+ `);
39
+ });
40
+ });
41
+
42
+ describe("host", () => {
43
+ it("should resolve a host to its zone", async () => {
44
+ writeWranglerToml({
45
+ main: "index.js",
46
+ });
47
+ fs.writeFileSync("index.js", `export default {};`);
48
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
49
+ await runWrangler("dev --host some-host.com");
50
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
51
+ host: "some-host.com",
52
+ id: "some-zone-id",
53
+ });
54
+ });
55
+
56
+ it("should read wrangler.toml's dev.host", async () => {
57
+ writeWranglerToml({
58
+ main: "index.js",
59
+ dev: {
60
+ host: "some-host.com",
61
+ },
62
+ });
63
+ fs.writeFileSync("index.js", `export default {};`);
64
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
65
+ await runWrangler("dev");
66
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
67
+ "some-host.com"
68
+ );
69
+ });
70
+
71
+ it("should read --route", async () => {
72
+ writeWranglerToml({
73
+ main: "index.js",
74
+ });
75
+ fs.writeFileSync("index.js", `export default {};`);
76
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
77
+ await runWrangler("dev --route http://some-host.com/some/path/*");
78
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
79
+ "some-host.com"
80
+ );
81
+ });
82
+
83
+ it("should read wrangler.toml's routes", async () => {
84
+ writeWranglerToml({
85
+ main: "index.js",
86
+ routes: [
87
+ "http://some-host.com/some/path/*",
88
+ "http://some-other-host.com/path/*",
89
+ ],
90
+ });
91
+ fs.writeFileSync("index.js", `export default {};`);
92
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
93
+ await runWrangler("dev");
94
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
95
+ "some-host.com"
96
+ );
97
+ });
98
+
99
+ it("should read wrangler.toml's environment specific routes", async () => {
100
+ writeWranglerToml({
101
+ main: "index.js",
102
+ routes: [
103
+ "http://a-host.com/some/path/*",
104
+ "http://another-host.com/path/*",
105
+ ],
106
+ env: {
107
+ staging: {
108
+ routes: [
109
+ "http://some-host.com/some/path/*",
110
+ "http://some-other-host.com/path/*",
111
+ ],
112
+ },
113
+ },
114
+ });
115
+ fs.writeFileSync("index.js", `export default {};`);
116
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
117
+ await runWrangler("dev --env staging");
118
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
119
+ "some-host.com"
120
+ );
121
+ });
122
+
123
+ it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
124
+ writeWranglerToml({
125
+ main: "index.js",
126
+ });
127
+ fs.writeFileSync("index.js", `export default {};`);
128
+ mockGetZones("111.222.333.some-host.com", []);
129
+ mockGetZones("222.333.some-host.com", []);
130
+ mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
131
+ await runWrangler("dev --host 111.222.333.some-host.com");
132
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
133
+ host: "111.222.333.some-host.com",
134
+ id: "some-zone-id",
135
+ });
136
+ });
137
+
138
+ it("should, in order, use args.host/config.dev.host/args.routes/config.route/config.routes", async () => {
139
+ // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
140
+
141
+ fs.writeFileSync("index.js", `export default {};`);
142
+
143
+ // config.routes
144
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
145
+ writeWranglerToml({
146
+ main: "index.js",
147
+ routes: ["http://5.some-host.com/some/path/*"],
148
+ });
149
+ await runWrangler("dev");
150
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
151
+ host: "5.some-host.com",
152
+ id: "some-zone-id-5",
153
+ });
154
+ (Dev as jest.Mock).mockClear();
155
+
156
+ // config.route
157
+ mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
158
+ writeWranglerToml({
159
+ main: "index.js",
160
+ route: "https://4.some-host.com/some/path/*",
161
+ routes: ["http://5.some-host.com/some/path/*"],
162
+ });
163
+ await runWrangler("dev");
164
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
165
+ host: "4.some-host.com",
166
+ id: "some-zone-id-4",
167
+ });
168
+ (Dev as jest.Mock).mockClear();
169
+
170
+ // --routes
171
+ mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
172
+ writeWranglerToml({
173
+ main: "index.js",
174
+ route: "https://4.some-host.com/some/path/*",
175
+ routes: ["http://5.some-host.com/some/path/*"],
176
+ });
177
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
178
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
179
+ host: "3.some-host.com",
180
+ id: "some-zone-id-3",
181
+ });
182
+ (Dev as jest.Mock).mockClear();
183
+
184
+ // config.dev.host
185
+ mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
186
+ writeWranglerToml({
187
+ main: "index.js",
188
+ dev: {
189
+ host: `2.some-host.com`,
190
+ },
191
+ route: "4.some-host.com/some/path/*",
192
+ routes: ["5.some-host.com/some/path/*"],
193
+ });
194
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
195
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
196
+ host: "2.some-host.com",
197
+ id: "some-zone-id-2",
198
+ });
199
+ (Dev as jest.Mock).mockClear();
200
+
201
+ // --host
202
+ mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
203
+ writeWranglerToml({
204
+ main: "index.js",
205
+ dev: {
206
+ host: `2.some-host.com`,
207
+ },
208
+ route: "4.some-host.com/some/path/*",
209
+ routes: ["5.some-host.com/some/path/*"],
210
+ });
211
+ await runWrangler(
212
+ "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
213
+ );
214
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
215
+ host: "1.some-host.com",
216
+ id: "some-zone-id-1",
217
+ });
218
+ (Dev as jest.Mock).mockClear();
219
+ });
220
+
221
+ it("should error if a host can't resolve to a zone", async () => {
222
+ writeWranglerToml({
223
+ main: "index.js",
224
+ });
225
+ fs.writeFileSync("index.js", `export default {};`);
226
+ mockGetZones("some-host.com", []);
227
+ await expect(
228
+ runWrangler("dev --host some-host.com")
229
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
230
+ `"Could not find zone for some-host.com"`
231
+ );
232
+ });
233
+ });
234
+
235
+ describe("custom builds", () => {
236
+ it("should run a custom build before starting `dev`", async () => {
237
+ writeWranglerToml({
238
+ build: {
239
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
240
+ },
241
+ });
242
+
243
+ await runWrangler("dev index.js");
244
+
245
+ expect(readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
246
+ `"export default { fetch(){ return new Response(123) } }"`
247
+ );
248
+
249
+ // and the command would pass through
250
+ expect((Dev as jest.Mock).mock.calls[0][0].buildCommand).toEqual({
251
+ command:
252
+ "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
253
+ cwd: undefined,
254
+ watch_dir: undefined,
255
+ });
256
+ expect(std.out).toMatchInlineSnapshot(
257
+ `"running: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
258
+ );
259
+ expect(std.err).toMatchInlineSnapshot(`""`);
260
+ expect(std.warn).toMatchInlineSnapshot(`""`);
261
+ });
262
+
263
+ if (process.platform !== "win32") {
264
+ it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
265
+ writeWranglerToml({
266
+ build: {
267
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
268
+ },
269
+ });
270
+
271
+ await runWrangler("dev index.js");
272
+
273
+ expect(readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
274
+ "export default { fetch(){ return new Response(123) } }
275
+ "
276
+ `);
277
+
278
+ expect(std.out).toMatchInlineSnapshot(
279
+ `"running: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
280
+ );
281
+ expect(std.err).toMatchInlineSnapshot(`""`);
282
+ expect(std.warn).toMatchInlineSnapshot(`""`);
283
+ });
284
+ }
285
+
286
+ it("should throw an error if the entry doesn't exist after the build finishes", async () => {
287
+ writeWranglerToml({
288
+ main: "index.js",
289
+ build: {
290
+ command: `node -e "console.log('custom build');"`,
291
+ },
292
+ });
293
+
294
+ await expect(
295
+ runWrangler("dev")
296
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
297
+ `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\""`
298
+ );
299
+ expect(std.out).toMatchInlineSnapshot(
300
+ `"running: node -e \\"console.log('custom build');\\""`
301
+ );
302
+ expect(std.err).toMatchInlineSnapshot(`
303
+ "Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
304
+
305
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
306
+ `);
307
+ expect(std.warn).toMatchInlineSnapshot(`""`);
308
+ });
309
+ });
310
+
311
+ describe("upstream-protocol", () => {
312
+ it("should default upstream-protocol to `https`", async () => {
313
+ writeWranglerToml({
314
+ main: "index.js",
315
+ });
316
+ fs.writeFileSync("index.js", `export default {};`);
317
+ await runWrangler("dev");
318
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
319
+ "https"
320
+ );
321
+ expect(std.out).toMatchInlineSnapshot(`""`);
322
+ expect(std.warn).toMatchInlineSnapshot(`""`);
323
+ expect(std.err).toMatchInlineSnapshot(`""`);
324
+ });
325
+
326
+ it("should warn if `--upstream-protocol=http` is used", async () => {
327
+ writeWranglerToml({
328
+ main: "index.js",
329
+ });
330
+ fs.writeFileSync("index.js", `export default {};`);
331
+ await runWrangler("dev --upstream-protocol=http");
332
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
333
+ "http"
334
+ );
335
+ expect(std.out).toMatchInlineSnapshot(`""`);
336
+ expect(std.warn).toMatchInlineSnapshot(`
337
+ "Setting upstream-protocol to http is not currently implemented.
338
+ If this is required in your project, please add your use case to the following issue:
339
+ https://github.com/cloudflare/wrangler2/issues/583."
340
+ `);
341
+ expect(std.err).toMatchInlineSnapshot(`""`);
342
+ });
343
+ });
344
+
345
+ describe("local-protocol", () => {
346
+ it("should default local-protocol to `http`", async () => {
347
+ writeWranglerToml({
348
+ main: "index.js",
349
+ });
350
+ fs.writeFileSync("index.js", `export default {};`);
351
+ await runWrangler("dev");
352
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
353
+ expect(std.out).toMatchInlineSnapshot(`""`);
354
+ expect(std.warn).toMatchInlineSnapshot(`""`);
355
+ expect(std.err).toMatchInlineSnapshot(`""`);
356
+ });
357
+
358
+ it("should use to `local_protocol` from `wrangler.toml`, if available", async () => {
359
+ writeWranglerToml({
360
+ main: "index.js",
361
+ dev: {
362
+ local_protocol: "https",
363
+ },
364
+ });
365
+ fs.writeFileSync("index.js", `export default {};`);
366
+ await runWrangler("dev");
367
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
368
+ "https"
369
+ );
370
+ expect(std.out).toMatchInlineSnapshot(`""`);
371
+ expect(std.warn).toMatchInlineSnapshot(`""`);
372
+ expect(std.err).toMatchInlineSnapshot(`""`);
373
+ });
374
+
375
+ it("should use --local-protocol command line arg, if provided", async () => {
376
+ // Here we show that the command line overrides the wrangler.toml by
377
+ // setting the config to https, and then setting it back to http on the command line.
378
+ writeWranglerToml({
379
+ main: "index.js",
380
+ dev: {
381
+ local_protocol: "https",
382
+ },
383
+ });
384
+ fs.writeFileSync("index.js", `export default {};`);
385
+ await runWrangler("dev --local-protocol=http");
386
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
387
+ expect(std.out).toMatchInlineSnapshot(`""`);
388
+ expect(std.warn).toMatchInlineSnapshot(`""`);
389
+ expect(std.err).toMatchInlineSnapshot(`""`);
390
+ });
23
391
  });
24
392
  });
25
393
 
26
- /**
27
- * Helper function to make it easier to setup and render the `Dev` component.
28
- *
29
- * All the `Dev` props are optional here, with sensible defaults for testing.
30
- */
31
- function renderDev({
32
- name,
33
- entry = "some/entry.ts",
34
- port,
35
- format,
36
- accountId,
37
- initialMode = "remote",
38
- jsxFactory,
39
- jsxFragment,
40
- bindings = {},
41
- public: publicDir,
42
- site,
43
- compatibilityDate,
44
- compatibilityFlags,
45
- usageModel,
46
- buildCommand = {},
47
- }: Partial<DevProps>) {
48
- return render(
49
- <Dev
50
- name={name}
51
- entry={entry}
52
- port={port}
53
- buildCommand={buildCommand}
54
- format={format}
55
- initialMode={initialMode}
56
- jsxFactory={jsxFactory}
57
- jsxFragment={jsxFragment}
58
- accountId={accountId}
59
- site={site}
60
- public={publicDir}
61
- compatibilityDate={compatibilityDate}
62
- compatibilityFlags={compatibilityFlags}
63
- usageModel={usageModel}
64
- bindings={bindings}
65
- />
394
+ function mockGetZones(domain: string, zones: { id: string }[] = []) {
395
+ const removeMock = setMockResponse(
396
+ "/zones",
397
+ "GET",
398
+ (_urlPieces, _init, queryParams) => {
399
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
400
+ // Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
401
+ removeMock();
402
+ return zones;
403
+ }
66
404
  );
67
405
  }
@@ -0,0 +1,152 @@
1
+ import * as fs from "node:fs";
2
+ import React from "react";
3
+ import TestRenderer from "react-test-renderer";
4
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
5
+ import { unsetAllMocks } from "./helpers/mock-cfetch";
6
+ import { mockConsoleMethods } from "./helpers/mock-console";
7
+ import { runInTempDir } from "./helpers/run-in-tmp";
8
+ import type { DevProps, default as DevType } from "../dev/dev";
9
+
10
+ function sleep(period = 100): Promise<void> {
11
+ return new Promise((resolve) => setTimeout(resolve, period));
12
+ }
13
+
14
+ // we use this ["mock"] form to avoid esbuild-jest from rewriting it
15
+ jest["mock"]("../proxy", () => {
16
+ return {
17
+ usePreviewServer() {},
18
+ waitForPortToBeAvailable() {},
19
+ };
20
+ });
21
+ jest["mock"]("../inspect", () => {
22
+ return () => {};
23
+ });
24
+
25
+ const Dev: typeof DevType = jest.requireActual("../dev/dev").DevImplementation;
26
+
27
+ mockAccountId();
28
+ mockApiToken();
29
+ runInTempDir();
30
+ mockConsoleMethods();
31
+ afterEach(() => {
32
+ unsetAllMocks();
33
+ });
34
+
35
+ describe("dev", () => {
36
+ it("should render", async () => {
37
+ fs.writeFileSync("./index.js", `export default {}`);
38
+
39
+ const testRenderer = await renderDev({
40
+ entry: {
41
+ file: "./index.js",
42
+ directory: process.cwd(),
43
+ format: "modules",
44
+ },
45
+ });
46
+ expect(testRenderer.toJSON()).toMatchInlineSnapshot(`
47
+ <ink-box
48
+ style={
49
+ Object {
50
+ "borderStyle": "round",
51
+ "flexDirection": "row",
52
+ "flexGrow": 0,
53
+ "flexShrink": 1,
54
+ "marginBottom": 0,
55
+ "marginLeft": 0,
56
+ "marginRight": 0,
57
+ "marginTop": 0,
58
+ "paddingBottom": 0,
59
+ "paddingLeft": 1,
60
+ "paddingRight": 1,
61
+ "paddingTop": 0,
62
+ }
63
+ }
64
+ >
65
+ <ink-text
66
+ internal_transform={[Function]}
67
+ style={
68
+ Object {
69
+ "flexDirection": "row",
70
+ "flexGrow": 0,
71
+ "flexShrink": 1,
72
+ "textWrap": "wrap",
73
+ }
74
+ }
75
+ >
76
+ B to open a browser, D to open Devtools, L to turn off local mode, X to exit
77
+ </ink-text>
78
+ </ink-box>
79
+ `);
80
+ await TestRenderer.act(async () => {
81
+ // TODO: get rid of these sleep statements
82
+ await sleep(50);
83
+ testRenderer.unmount();
84
+ await sleep(50);
85
+ });
86
+ await sleep(50);
87
+ });
88
+ });
89
+
90
+ async function renderDev({
91
+ name,
92
+ entry = { file: "index.js", directory: "", format: "modules" },
93
+ port,
94
+ inspectorPort = 9229,
95
+ accountId,
96
+ legacyEnv = true,
97
+ initialMode = "local",
98
+ jsxFactory,
99
+ jsxFragment,
100
+ localProtocol = "http",
101
+ upstreamProtocol = "https",
102
+ rules = [],
103
+ bindings = {
104
+ kv_namespaces: [],
105
+ vars: {},
106
+ durable_objects: { bindings: [] },
107
+ r2_buckets: [],
108
+ wasm_modules: undefined,
109
+ text_blobs: undefined,
110
+ unsafe: [],
111
+ },
112
+ public: publicDir,
113
+ assetPaths,
114
+ compatibilityDate,
115
+ compatibilityFlags,
116
+ usageModel,
117
+ buildCommand = {},
118
+ enableLocalPersistence = false,
119
+ env,
120
+ zone,
121
+ }: Partial<DevProps>): Promise<TestRenderer.ReactTestRenderer> {
122
+ let instance: TestRenderer.ReactTestRenderer | undefined;
123
+ await TestRenderer.act(async () => {
124
+ instance = TestRenderer.create(
125
+ <Dev
126
+ name={name}
127
+ entry={entry}
128
+ env={env}
129
+ rules={rules}
130
+ port={port}
131
+ inspectorPort={inspectorPort}
132
+ legacyEnv={legacyEnv}
133
+ buildCommand={buildCommand}
134
+ initialMode={initialMode}
135
+ localProtocol={localProtocol}
136
+ upstreamProtocol={upstreamProtocol}
137
+ jsxFactory={jsxFactory}
138
+ jsxFragment={jsxFragment}
139
+ accountId={accountId}
140
+ assetPaths={assetPaths}
141
+ public={publicDir}
142
+ compatibilityDate={compatibilityDate}
143
+ compatibilityFlags={compatibilityFlags}
144
+ usageModel={usageModel}
145
+ bindings={bindings}
146
+ enableLocalPersistence={enableLocalPersistence}
147
+ zone={zone}
148
+ />
149
+ );
150
+ });
151
+ return instance as TestRenderer.ReactTestRenderer;
152
+ }
@@ -0,0 +1,58 @@
1
+ import { writeFile } from "fs/promises";
2
+ import path from "path";
3
+ import guessWorkerFormat from "../entry";
4
+ import { runInTempDir } from "./helpers/run-in-tmp";
5
+
6
+ describe("guess worker format", () => {
7
+ runInTempDir();
8
+ it('should detect a "modules" worker', async () => {
9
+ await writeFile("./index.ts", "export default {};");
10
+ // Note that this isn't actually a valid worker, because it's missing
11
+ // a fetch handler. Regardless, our heuristic is simply to check for exports.
12
+ const guess = await guessWorkerFormat(
13
+ path.join(process.cwd(), "./index.ts"),
14
+ process.cwd(),
15
+ undefined
16
+ );
17
+ expect(guess).toBe("modules");
18
+ });
19
+
20
+ it('should detect a "service-worker" worker', async () => {
21
+ await writeFile("./index.ts", "");
22
+ // Note that this isn't actually a valid worker, because it's missing
23
+ // a fetch listener. Regardless, our heuristic is simply to check for
24
+ // the lack of exports.
25
+ const guess = await guessWorkerFormat(
26
+ path.join(process.cwd(), "./index.ts"),
27
+ process.cwd(),
28
+ undefined
29
+ );
30
+ expect(guess).toBe("service-worker");
31
+ });
32
+
33
+ it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
34
+ await writeFile("./index.ts", "export default {};");
35
+ await expect(
36
+ guessWorkerFormat(
37
+ path.join(process.cwd(), "./index.ts"),
38
+ process.cwd(),
39
+ "service-worker"
40
+ )
41
+ ).rejects.toThrow(
42
+ "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
43
+ );
44
+ });
45
+
46
+ it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
47
+ await writeFile("./index.ts", "");
48
+ await expect(
49
+ guessWorkerFormat(
50
+ path.join(process.cwd(), "./index.ts"),
51
+ process.cwd(),
52
+ "modules"
53
+ )
54
+ ).rejects.toThrow(
55
+ "You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
56
+ );
57
+ });
58
+ });
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The typings file available at `@types/cmd-shim` are out of date.
3
+ */
4
+ module "cmd-shim" {
5
+ /**
6
+ *
7
+ * Create a cmd shim at `to` for the command line program at `from`.
8
+ *
9
+ */
10
+ export default function cmdShim(from: string, to: string): Promise<void>;
11
+ }