wrangler 0.0.13 → 0.0.17

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 (67) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +20 -11
  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 +44 -51
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +3 -9
  8. package/src/__tests__/dev.test.tsx +42 -5
  9. package/src/__tests__/guess-worker-format.test.ts +66 -0
  10. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  11. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  12. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +36 -0
  15. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
  16. package/src/__tests__/helpers/mock-console.ts +62 -0
  17. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/mock-web-socket.ts +37 -0
  21. package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
  22. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  23. package/src/__tests__/helpers/write-wrangler-toml.ts +20 -0
  24. package/src/__tests__/index.test.ts +418 -71
  25. package/src/__tests__/jest.setup.ts +30 -2
  26. package/src/__tests__/kv.test.ts +147 -252
  27. package/src/__tests__/logout.test.ts +50 -0
  28. package/src/__tests__/package-manager.test.ts +206 -0
  29. package/src/__tests__/publish.test.ts +1136 -291
  30. package/src/__tests__/r2.test.ts +206 -0
  31. package/src/__tests__/secret.test.ts +210 -0
  32. package/src/__tests__/sentry.test.ts +146 -0
  33. package/src/__tests__/tail.test.ts +246 -0
  34. package/src/__tests__/whoami.test.tsx +6 -47
  35. package/src/api/form_data.ts +75 -25
  36. package/src/api/preview.ts +2 -2
  37. package/src/api/worker.ts +34 -15
  38. package/src/bundle.ts +127 -0
  39. package/src/cfetch/index.ts +7 -15
  40. package/src/cfetch/internal.ts +41 -6
  41. package/src/cli.ts +10 -0
  42. package/src/config.ts +125 -95
  43. package/src/dev.tsx +300 -193
  44. package/src/dialogs.tsx +2 -2
  45. package/src/guess-worker-format.ts +68 -0
  46. package/src/index.tsx +578 -192
  47. package/src/inspect.ts +29 -10
  48. package/src/kv.tsx +23 -17
  49. package/src/module-collection.ts +32 -12
  50. package/src/open-in-browser.ts +13 -0
  51. package/src/package-manager.ts +120 -0
  52. package/src/pages.tsx +28 -23
  53. package/src/paths.ts +26 -0
  54. package/src/proxy.ts +88 -14
  55. package/src/publish.ts +260 -297
  56. package/src/r2.ts +50 -0
  57. package/src/reporting.ts +115 -0
  58. package/src/sites.tsx +28 -27
  59. package/src/tail.tsx +178 -9
  60. package/src/user.tsx +58 -44
  61. package/templates/new-worker.js +15 -0
  62. package/templates/new-worker.ts +15 -0
  63. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  64. package/wrangler-dist/cli.js +124315 -104677
  65. package/wrangler-dist/cli.js.map +3 -3
  66. package/src/__tests__/mock-console.ts +0 -34
  67. package/src/__tests__/run-wrangler.ts +0 -8
@@ -1,14 +1,26 @@
1
+ import * as fs from "node:fs";
1
2
  import * as fsp from "node:fs/promises";
2
3
  import * as TOML from "@iarna/toml";
3
- import { mockConfirm } from "./mock-dialogs";
4
- import { runWrangler } from "./run-wrangler";
5
- import { runInTempDir } from "./run-in-tmp";
6
- import { mockConsoleMethods } from "./mock-console";
7
- import * as fs from "node:fs";
4
+ import { version as wranglerVersion } from "../../package.json";
5
+ import { getPackageManager } from "../package-manager";
6
+ import { mockConsoleMethods } from "./helpers/mock-console";
7
+ import { mockConfirm } from "./helpers/mock-dialogs";
8
+ import { runInTempDir } from "./helpers/run-in-tmp";
9
+ import { runWrangler } from "./helpers/run-wrangler";
10
+ import type { PackageManager } from "../package-manager";
8
11
 
9
12
  describe("wrangler", () => {
13
+ let mockPackageManager: PackageManager;
10
14
  runInTempDir();
11
15
 
16
+ beforeEach(() => {
17
+ mockPackageManager = {
18
+ addDevDeps: jest.fn(),
19
+ install: jest.fn(),
20
+ };
21
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
22
+ });
23
+
12
24
  const std = mockConsoleMethods();
13
25
 
14
26
  describe("no command", () => {
@@ -21,7 +33,7 @@ describe("wrangler", () => {
21
33
  Commands:
22
34
  wrangler init [name] 📥 Create a wrangler.toml configuration file
23
35
  wrangler whoami 🕵️ Retrieve your user info and test your auth config
24
- wrangler dev <filename> 👂 Start a local server for developing your worker
36
+ wrangler dev [script] 👂 Start a local server for developing your worker
25
37
  wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
26
38
  wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
27
39
  wrangler secret 🤫 Generate a secret that can be referenced in the worker script
@@ -29,6 +41,7 @@ describe("wrangler", () => {
29
41
  wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
30
42
  wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
31
43
  wrangler pages ⚡️ Configure Cloudflare Pages
44
+ wrangler r2 📦 Interact with an R2 store
32
45
 
33
46
  Flags:
34
47
  -c, --config Path to .toml configuration file [string]
@@ -45,14 +58,11 @@ describe("wrangler", () => {
45
58
 
46
59
  describe("invalid command", () => {
47
60
  it("should display an error", async () => {
48
- let err: Error | undefined;
49
- try {
50
- await runWrangler("invalid-command");
51
- } catch (e) {
52
- err = e;
53
- } finally {
54
- expect(err?.message).toBe(`Unknown command: invalid-command.`);
55
- }
61
+ await expect(
62
+ runWrangler("invalid-command")
63
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
64
+ `"Unknown command: invalid-command."`
65
+ );
56
66
 
57
67
  expect(std.out).toMatchInlineSnapshot(`""`);
58
68
  expect(std.err).toMatchInlineSnapshot(`
@@ -61,7 +71,7 @@ describe("wrangler", () => {
61
71
  Commands:
62
72
  wrangler init [name] 📥 Create a wrangler.toml configuration file
63
73
  wrangler whoami 🕵️ Retrieve your user info and test your auth config
64
- wrangler dev <filename> 👂 Start a local server for developing your worker
74
+ wrangler dev [script] 👂 Start a local server for developing your worker
65
75
  wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
66
76
  wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
67
77
  wrangler secret 🤫 Generate a secret that can be referenced in the worker script
@@ -69,6 +79,7 @@ describe("wrangler", () => {
69
79
  wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
70
80
  wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
71
81
  wrangler pages ⚡️ Configure Cloudflare Pages
82
+ wrangler r2 📦 Interact with an R2 store
72
83
 
73
84
  Flags:
74
85
  -c, --config Path to .toml configuration file [string]
@@ -92,6 +103,20 @@ describe("wrangler", () => {
92
103
  await runWrangler("init");
93
104
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
94
105
  expect(typeof parsed.compatibility_date).toBe("string");
106
+ expect(parsed.name).toContain("wrangler-tests");
107
+ expect(fs.existsSync("./package.json")).toBe(false);
108
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
109
+ });
110
+
111
+ it("should create a named Worker wrangler.toml", async () => {
112
+ mockConfirm({
113
+ text: "No package.json found. Would you like to create one?",
114
+ result: false,
115
+ });
116
+ await runWrangler("init my-worker");
117
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
118
+ expect(typeof parsed.compatibility_date).toBe("string");
119
+ expect(parsed.name).toBe("my-worker");
95
120
  expect(fs.existsSync("./package.json")).toBe(false);
96
121
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
97
122
  });
@@ -141,7 +166,11 @@ describe("wrangler", () => {
141
166
  result: true,
142
167
  },
143
168
  {
144
- text: "Would you like to use typescript?",
169
+ text: "Would you like to use TypeScript?",
170
+ result: false,
171
+ },
172
+ {
173
+ text: "Would you like to create a Worker at src/index.js?",
145
174
  result: false,
146
175
  }
147
176
  );
@@ -150,12 +179,35 @@ describe("wrangler", () => {
150
179
  const packageJson = JSON.parse(
151
180
  fs.readFileSync("./package.json", "utf-8")
152
181
  );
153
- expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
154
- expect(packageJson.version).toEqual("0.0.1");
182
+ expect(packageJson.name).toContain("wrangler-tests");
183
+ expect(packageJson.version).toEqual("0.0.0");
155
184
  expect(packageJson.devDependencies).toEqual({
156
185
  wrangler: expect.any(String),
157
186
  });
158
187
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
188
+ expect(mockPackageManager.install).toHaveBeenCalled();
189
+ });
190
+
191
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
192
+ mockConfirm(
193
+ {
194
+ text: "No package.json found. Would you like to create one?",
195
+ result: true,
196
+ },
197
+ {
198
+ text: "Would you like to use TypeScript?",
199
+ result: false,
200
+ },
201
+ {
202
+ text: "Would you like to create a Worker at src/index.js?",
203
+ result: false,
204
+ }
205
+ );
206
+ await runWrangler("init my-worker");
207
+ const packageJson = JSON.parse(
208
+ fs.readFileSync("./package.json", "utf-8")
209
+ );
210
+ expect(packageJson.name).toBe("my-worker");
159
211
  });
160
212
 
161
213
  it("should not touch an existing package.json in the same directory", async () => {
@@ -165,7 +217,11 @@ describe("wrangler", () => {
165
217
  result: false,
166
218
  },
167
219
  {
168
- text: "Would you like to use typescript?",
220
+ text: "Would you like to use TypeScript?",
221
+ result: false,
222
+ },
223
+ {
224
+ text: "Would you like to create a Worker at src/index.js?",
169
225
  result: false,
170
226
  }
171
227
  );
@@ -191,7 +247,11 @@ describe("wrangler", () => {
191
247
  result: true,
192
248
  },
193
249
  {
194
- text: "Would you like to use typescript?",
250
+ text: "Would you like to use TypeScript?",
251
+ result: false,
252
+ },
253
+ {
254
+ text: "Would you like to create a Worker at src/index.js?",
195
255
  result: false,
196
256
  }
197
257
  );
@@ -208,9 +268,9 @@ describe("wrangler", () => {
208
268
  );
209
269
  expect(packageJson.name).toEqual("test");
210
270
  expect(packageJson.version).toEqual("1.0.0");
211
- expect(packageJson.devDependencies).toEqual({
212
- wrangler: expect.any(String),
213
- });
271
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
272
+ `wrangler@${wranglerVersion}`
273
+ );
214
274
  });
215
275
 
216
276
  it("should not touch an existing package.json in an ancestor directory", async () => {
@@ -220,7 +280,11 @@ describe("wrangler", () => {
220
280
  result: false,
221
281
  },
222
282
  {
223
- text: "Would you like to use typescript?",
283
+ text: "Would you like to use TypeScript?",
284
+ result: false,
285
+ },
286
+ {
287
+ text: "Would you like to create a Worker at src/index.js?",
224
288
  result: false,
225
289
  }
226
290
  );
@@ -249,6 +313,282 @@ describe("wrangler", () => {
249
313
  `);
250
314
  });
251
315
 
316
+ it("should offer to create a worker in a non-typescript project", async () => {
317
+ mockConfirm(
318
+ {
319
+ text: "Would you like to install wrangler into your package.json?",
320
+ result: false,
321
+ },
322
+ {
323
+ text: "Would you like to use TypeScript?",
324
+ result: false,
325
+ },
326
+ {
327
+ text: "Would you like to create a Worker at src/index.js?",
328
+ result: true,
329
+ }
330
+ );
331
+
332
+ fs.writeFileSync(
333
+ "./package.json",
334
+ JSON.stringify({ name: "test", version: "1.0.0" }),
335
+ "utf-8"
336
+ );
337
+
338
+ await runWrangler("init");
339
+ expect(fs.existsSync("./src/index.js")).toBe(true);
340
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
341
+ });
342
+
343
+ it("should offer to create a worker in a typescript project", async () => {
344
+ mockConfirm(
345
+ {
346
+ text: "Would you like to install wrangler into your package.json?",
347
+ result: false,
348
+ },
349
+ {
350
+ text: "Would you like to use TypeScript?",
351
+ result: true,
352
+ },
353
+ {
354
+ text: "Would you like to create a Worker at src/index.ts?",
355
+ result: true,
356
+ }
357
+ );
358
+
359
+ fs.writeFileSync(
360
+ "./package.json",
361
+ JSON.stringify({ name: "test", version: "1.0.0" }),
362
+ "utf-8"
363
+ );
364
+
365
+ await runWrangler("init");
366
+ expect(fs.existsSync("./src/index.js")).toBe(false);
367
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
368
+ });
369
+
370
+ it("should add scripts for a typescript project with .ts extension", async () => {
371
+ mockConfirm(
372
+ {
373
+ text: "No package.json found. Would you like to create one?",
374
+ result: true,
375
+ },
376
+ {
377
+ text: "Would you like to install wrangler into your package.json?",
378
+ result: false,
379
+ },
380
+ {
381
+ text: "Would you like to use TypeScript?",
382
+ result: true,
383
+ },
384
+ {
385
+ text: "Would you like to create a Worker at src/index.ts?",
386
+ result: true,
387
+ }
388
+ );
389
+ await runWrangler("init");
390
+
391
+ expect(fs.existsSync("./package.json")).toBe(true);
392
+ const packageJson = JSON.parse(
393
+ fs.readFileSync("./package.json", "utf-8")
394
+ );
395
+
396
+ expect(fs.existsSync("./src/index.js")).toBe(false);
397
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
398
+
399
+ expect(packageJson.scripts.start).toBe("wrangler dev src/index.ts");
400
+ expect(packageJson.scripts.deploy).toBe("wrangler publish src/index.ts");
401
+ expect(packageJson.name).toContain("wrangler-tests");
402
+ expect(packageJson.version).toEqual("0.0.0");
403
+ expect(std.out).toMatchInlineSnapshot(`
404
+ "✨ Successfully created wrangler.toml
405
+ ✨ Created package.json
406
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
407
+ To start developing on your worker, run npm start.
408
+ To publish your worker on to the internet, run npm run deploy.
409
+ ✨ Created src/index.ts"
410
+ `);
411
+ });
412
+
413
+ it("should not overwrite package.json scripts for a typescript project", async () => {
414
+ mockConfirm(
415
+ {
416
+ text: "Would you like to install wrangler into your package.json?",
417
+ result: false,
418
+ },
419
+ {
420
+ text: "Would you like to use TypeScript?",
421
+ result: true,
422
+ },
423
+ {
424
+ text: "Would you like to create a Worker at src/index.ts?",
425
+ result: true,
426
+ }
427
+ );
428
+ await fsp.writeFile(
429
+ "./package.json",
430
+ JSON.stringify({
431
+ scripts: {
432
+ start: "test-start",
433
+ deploy: "test-deploy",
434
+ },
435
+ })
436
+ );
437
+ const packageJson = JSON.parse(
438
+ fs.readFileSync("./package.json", "utf-8")
439
+ );
440
+ await runWrangler("init");
441
+
442
+ expect(fs.existsSync("./src/index.js")).toBe(false);
443
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
444
+
445
+ expect(packageJson.scripts.start).toBe("test-start");
446
+ expect(packageJson.scripts.deploy).toBe("test-deploy");
447
+ expect(std.out).toMatchInlineSnapshot(`
448
+ "✨ Successfully created wrangler.toml
449
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
450
+ To start developing on your worker, npx wrangler dev src/index.ts
451
+ To publish your worker on to the internet, npx wrangler publish src/index.ts
452
+ ✨ Created src/index.ts"
453
+ `);
454
+ });
455
+
456
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
457
+ mockConfirm(
458
+ {
459
+ text: "No package.json found. Would you like to create one?",
460
+ result: true,
461
+ },
462
+ {
463
+ text: "Would you like to install wrangler into your package.json?",
464
+ result: false,
465
+ },
466
+ {
467
+ text: "Would you like to use TypeScript?",
468
+ result: false,
469
+ },
470
+ {
471
+ text: "Would you like to create a Worker at src/index.js?",
472
+ result: true,
473
+ }
474
+ );
475
+ await runWrangler("init");
476
+
477
+ expect(fs.existsSync("./package.json")).toBe(true);
478
+ const packageJson = JSON.parse(
479
+ fs.readFileSync("./package.json", "utf-8")
480
+ );
481
+
482
+ expect(fs.existsSync("./src/index.js")).toBe(true);
483
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
484
+
485
+ expect(packageJson.scripts.start).toBe("wrangler dev src/index.js");
486
+ expect(packageJson.scripts.deploy).toBe("wrangler publish src/index.js");
487
+ expect(packageJson.name).toContain("wrangler-tests");
488
+ expect(packageJson.version).toEqual("0.0.0");
489
+ expect(std.out).toMatchInlineSnapshot(`
490
+ "✨ Successfully created wrangler.toml
491
+ ✨ Created package.json
492
+ To start developing on your worker, run npm start.
493
+ To publish your worker on to the internet, run npm run deploy.
494
+ ✨ Created src/index.js"
495
+ `);
496
+ });
497
+
498
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
499
+ mockConfirm(
500
+ {
501
+ text: "Would you like to install wrangler into your package.json?",
502
+ result: false,
503
+ },
504
+ {
505
+ text: "Would you like to use TypeScript?",
506
+ result: false,
507
+ },
508
+ {
509
+ text: "Would you like to create a Worker at src/index.js?",
510
+ result: true,
511
+ }
512
+ );
513
+ await fsp.writeFile(
514
+ "./package.json",
515
+ JSON.stringify({
516
+ scripts: {
517
+ start: "test-start",
518
+ deploy: "test-deploy",
519
+ },
520
+ })
521
+ );
522
+ const packageJson = JSON.parse(
523
+ fs.readFileSync("./package.json", "utf-8")
524
+ );
525
+ await runWrangler("init");
526
+
527
+ expect(fs.existsSync("./src/index.js")).toBe(true);
528
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
529
+
530
+ expect(packageJson.scripts.start).toBe("test-start");
531
+ expect(packageJson.scripts.deploy).toBe("test-deploy");
532
+ expect(std.out).toMatchInlineSnapshot(`
533
+ "✨ Successfully created wrangler.toml
534
+ To start developing on your worker, npx wrangler dev src/index.js
535
+ To publish your worker on to the internet, npx wrangler publish src/index.js
536
+ ✨ Created src/index.js"
537
+ `);
538
+ });
539
+
540
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
541
+ mockConfirm(
542
+ {
543
+ text: "Would you like to install wrangler into your package.json?",
544
+ result: false,
545
+ },
546
+ {
547
+ text: "Would you like to use TypeScript?",
548
+ result: false,
549
+ }
550
+ );
551
+
552
+ fs.writeFileSync(
553
+ "./package.json",
554
+ JSON.stringify({ name: "test", version: "1.0.0" }),
555
+ "utf-8"
556
+ );
557
+ fs.mkdirSync("./src", { recursive: true });
558
+ const PLACEHOLDER = "/* placeholder text */";
559
+ fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
560
+
561
+ await runWrangler("init");
562
+ expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
563
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
564
+ });
565
+
566
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
567
+ mockConfirm(
568
+ {
569
+ text: "Would you like to install wrangler into your package.json?",
570
+ result: false,
571
+ },
572
+ {
573
+ text: "Would you like to use TypeScript?",
574
+ result: true,
575
+ }
576
+ );
577
+
578
+ fs.writeFileSync(
579
+ "./package.json",
580
+ JSON.stringify({ name: "test", version: "1.0.0" }),
581
+ "utf-8"
582
+ );
583
+ fs.mkdirSync("./src", { recursive: true });
584
+ const PLACEHOLDER = "/* placeholder text */";
585
+ fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
586
+
587
+ await runWrangler("init");
588
+ expect(fs.existsSync("./src/index.js")).toBe(false);
589
+ expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
590
+ });
591
+
252
592
  it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
253
593
  mockConfirm(
254
594
  {
@@ -256,8 +596,12 @@ describe("wrangler", () => {
256
596
  result: true,
257
597
  },
258
598
  {
259
- text: "Would you like to use typescript?",
599
+ text: "Would you like to use TypeScript?",
260
600
  result: true,
601
+ },
602
+ {
603
+ text: "Would you like to create a Worker at src/index.ts?",
604
+ result: false,
261
605
  }
262
606
  );
263
607
  await runWrangler("init");
@@ -268,13 +612,10 @@ describe("wrangler", () => {
268
612
  expect(tsconfigJson.compilerOptions.types).toEqual([
269
613
  "@cloudflare/workers-types",
270
614
  ]);
271
- const packageJson = JSON.parse(
272
- fs.readFileSync("./package.json", "utf-8")
615
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
616
+ "@cloudflare/workers-types",
617
+ "typescript"
273
618
  );
274
- expect(packageJson.devDependencies).toEqual({
275
- "@cloudflare/workers-types": expect.any(String),
276
- wrangler: expect.any(String),
277
- });
278
619
  });
279
620
 
280
621
  it("should not touch an existing tsconfig.json in the same directory", async () => {
@@ -312,6 +653,10 @@ describe("wrangler", () => {
312
653
  {
313
654
  text: "Would you like to install the type definitions for Workers into your package.json?",
314
655
  result: true,
656
+ },
657
+ {
658
+ text: "Would you like to create a Worker at src/index.ts?",
659
+ result: false,
315
660
  }
316
661
  );
317
662
  fs.writeFileSync(
@@ -334,12 +679,9 @@ describe("wrangler", () => {
334
679
  );
335
680
  // unchanged tsconfig
336
681
  expect(tsconfigJson.compilerOptions).toEqual({});
337
- const packageJson = JSON.parse(
338
- fs.readFileSync("./package.json", "utf-8")
682
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
683
+ "@cloudflare/workers-types"
339
684
  );
340
- expect(packageJson.devDependencies).toEqual({
341
- "@cloudflare/workers-types": expect.any(String),
342
- });
343
685
  });
344
686
 
345
687
  it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
@@ -374,51 +716,56 @@ describe("wrangler", () => {
374
716
  expect(tsconfigJson.compilerOptions).toEqual({});
375
717
  });
376
718
 
719
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
720
+ await runWrangler("init --yes");
721
+
722
+ expect(fs.existsSync("./src/index.js")).toBe(false);
723
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
724
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
725
+ expect(fs.existsSync("./package.json")).toBe(true);
726
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
727
+ });
728
+
729
+ it("should initialize with no interactive prompts if `--y` is used", async () => {
730
+ await runWrangler("init -y");
731
+
732
+ expect(fs.existsSync("./src/index.js")).toBe(false);
733
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
734
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
735
+ expect(fs.existsSync("./package.json")).toBe(true);
736
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
737
+ });
738
+
377
739
  it("should error if `--type` is used", async () => {
378
- let err: undefined | Error;
379
- try {
380
- await runWrangler("init --type");
381
- } catch (e) {
382
- err = e;
383
- } finally {
384
- expect(err?.message).toBe(`The --type option is no longer supported.`);
385
- }
740
+ await expect(
741
+ runWrangler("init --type")
742
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
743
+ `"The --type option is no longer supported."`
744
+ );
386
745
  });
387
746
 
388
747
  it("should error if `--type javascript` is used", async () => {
389
- let err: undefined | Error;
390
- try {
391
- await runWrangler("init --type javascript");
392
- } catch (e) {
393
- err = e;
394
- } finally {
395
- expect(err?.message).toBe(`The --type option is no longer supported.`);
396
- }
748
+ await expect(
749
+ runWrangler("init --type javascript")
750
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
751
+ `"The --type option is no longer supported."`
752
+ );
397
753
  });
398
754
 
399
755
  it("should error if `--type rust` is used", async () => {
400
- let err: undefined | Error;
401
- try {
402
- await runWrangler("init --type rust");
403
- } catch (e) {
404
- err = e;
405
- } finally {
406
- expect(err?.message).toBe(`The --type option is no longer supported.`);
407
- }
756
+ await expect(
757
+ runWrangler("init --type rust")
758
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
759
+ `"The --type option is no longer supported."`
760
+ );
408
761
  });
409
762
 
410
763
  it("should error if `--type webpack` is used", async () => {
411
- let err: undefined | Error;
412
- try {
413
- await runWrangler("init --type webpack");
414
- } catch (e) {
415
- err = e;
416
- } finally {
417
- expect(err?.message).toBe(
418
- `The --type option is no longer supported.
419
- If you wish to use webpack then you will need to create a custom build.`
420
- );
421
- }
764
+ await expect(runWrangler("init --type webpack")).rejects
765
+ .toThrowErrorMatchingInlineSnapshot(`
766
+ "The --type option is no longer supported.
767
+ If you wish to use webpack then you will need to create a custom build."
768
+ `);
422
769
  });
423
770
  });
424
771
  });
@@ -1,9 +1,37 @@
1
- import { mockFetchInternal } from "./mock-cfetch";
1
+ import fetchMock from "jest-fetch-mock";
2
+ import { fetchInternal, fetchKVGetValue } from "../cfetch/internal";
2
3
  import { confirm, prompt } from "../dialogs";
3
- import { fetchInternal } from "../cfetch/internal";
4
+ import { mockFetchInternal, mockFetchKVGetValue } from "./helpers/mock-cfetch";
5
+ import { MockWebSocket } from "./helpers/mock-web-socket";
6
+
7
+ jest.mock("ws", () => {
8
+ return {
9
+ __esModule: true,
10
+ default: MockWebSocket,
11
+ };
12
+ });
13
+
14
+ jest.mock("undici", () => {
15
+ return {
16
+ ...jest.requireActual("undici"),
17
+ fetch: jest.requireActual("jest-fetch-mock"),
18
+ };
19
+ });
20
+
21
+ // Outside of the Sentry tests themselves, we mock Sentry to ensure that it doesn't actually send any data and
22
+ // that it doesn't interfere with the rest of the tests.
23
+ jest.mock("../reporting");
24
+
25
+ fetchMock.doMock(() => {
26
+ // Any un-mocked fetches should throw
27
+ throw new Error("Unexpected fetch request");
28
+ });
29
+
30
+ jest.mock("../package-manager");
4
31
 
5
32
  jest.mock("../cfetch/internal");
6
33
  (fetchInternal as jest.Mock).mockImplementation(mockFetchInternal);
34
+ (fetchKVGetValue as jest.Mock).mockImplementation(mockFetchKVGetValue);
7
35
 
8
36
  jest.mock("../dialogs");
9
37