wrangler 2.6.2 → 2.7.1

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 (128) hide show
  1. package/bin/wrangler.js +9 -1
  2. package/miniflare-dist/index.mjs +1 -1
  3. package/package.json +12 -10
  4. package/src/__tests__/api-dev.test.ts +65 -36
  5. package/src/__tests__/api-devregistry.test.js +14 -6
  6. package/src/__tests__/configuration.test.ts +2 -31
  7. package/src/__tests__/{d1.test.ts → d1/d1.test.ts} +48 -5
  8. package/src/__tests__/d1/splitter.test.ts +255 -0
  9. package/src/__tests__/delete.test.ts +5 -2
  10. package/src/__tests__/deployments.test.ts +20 -6
  11. package/src/__tests__/dev.test.tsx +52 -19
  12. package/src/__tests__/generate.test.ts +7 -4
  13. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  14. package/src/__tests__/helpers/mock-cfetch.ts +2 -57
  15. package/src/__tests__/helpers/mock-dialogs.ts +70 -86
  16. package/src/__tests__/helpers/mock-oauth-flow.ts +64 -49
  17. package/src/__tests__/helpers/mock-process.ts +8 -13
  18. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  19. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  20. package/src/__tests__/index.test.ts +46 -44
  21. package/src/__tests__/init.test.ts +761 -537
  22. package/src/__tests__/jest.setup.ts +20 -24
  23. package/src/__tests__/kv.test.ts +286 -173
  24. package/src/__tests__/logout.test.ts +1 -1
  25. package/src/__tests__/metrics.test.ts +5 -7
  26. package/src/__tests__/middleware.scheduled.test.ts +40 -30
  27. package/src/__tests__/middleware.test.ts +144 -120
  28. package/src/__tests__/pages.test.ts +1617 -1161
  29. package/src/__tests__/publish.test.ts +174 -125
  30. package/src/__tests__/r2.test.ts +2 -2
  31. package/src/__tests__/secret.test.ts +183 -126
  32. package/src/__tests__/tail.test.ts +6 -0
  33. package/src/__tests__/tsconfig-sanity.ts +12 -0
  34. package/src/__tests__/tsconfig.json +8 -0
  35. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  36. package/src/__tests__/whoami.test.tsx +1 -96
  37. package/src/api/dev.ts +90 -41
  38. package/src/api/index.ts +1 -1
  39. package/src/{bundle-reporter.tsx → bundle-reporter.ts} +0 -0
  40. package/src/cfetch/index.ts +0 -2
  41. package/src/cfetch/internal.ts +6 -15
  42. package/src/cli.ts +2 -2
  43. package/src/config/validation.ts +1 -2
  44. package/src/create-worker-upload-form.ts +2 -2
  45. package/src/d1/{delete.tsx → delete.ts} +0 -0
  46. package/src/d1/execute.tsx +8 -37
  47. package/src/d1/migrations/apply.tsx +29 -19
  48. package/src/d1/migrations/{index.tsx → index.ts} +0 -0
  49. package/src/d1/splitter.ts +161 -0
  50. package/src/d1/{types.tsx → types.ts} +0 -0
  51. package/src/delete.ts +3 -8
  52. package/src/deployments.ts +6 -0
  53. package/src/deprecated/index.ts +2 -295
  54. package/src/dev/dev.tsx +2 -2
  55. package/src/dev/{get-local-persistence-path.tsx → get-local-persistence-path.ts} +0 -0
  56. package/src/dev/local.tsx +16 -4
  57. package/src/dev/remote.tsx +28 -1
  58. package/src/dev/start-server.ts +19 -11
  59. package/src/dev/use-esbuild.ts +1 -1
  60. package/src/{dev-registry.tsx → dev-registry.ts} +0 -0
  61. package/src/dev.tsx +21 -2
  62. package/src/dialogs.ts +136 -0
  63. package/src/dispatch-namespace.ts +1 -1
  64. package/src/docs/index.ts +3 -0
  65. package/src/environment-variables/factory.ts +88 -0
  66. package/src/environment-variables/misc-variables.ts +30 -0
  67. package/src/generate/index.ts +300 -0
  68. package/src/{index.tsx → index.ts} +10 -13
  69. package/src/init.ts +92 -52
  70. package/src/jest.d.ts +4 -0
  71. package/src/logger.ts +15 -3
  72. package/src/metrics/metrics-config.ts +1 -1
  73. package/src/miniflare-cli/assets.ts +4 -0
  74. package/src/miniflare-cli/index.ts +1 -5
  75. package/src/miniflare-cli/tsconfig.json +9 -0
  76. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  77. package/src/miniflare-cli/types.ts +11 -0
  78. package/src/pages/{build.tsx → build.ts} +0 -0
  79. package/src/pages/{deployment-tails.tsx → deployment-tails.ts} +0 -0
  80. package/src/pages/{dev.tsx → dev.ts} +53 -55
  81. package/src/pages/functions/buildWorker.ts +1 -1
  82. package/src/pages/functions/tsconfig.json +8 -0
  83. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  84. package/src/pages/{functions.tsx → functions.ts} +0 -0
  85. package/src/pages/{hash.tsx → hash.ts} +0 -0
  86. package/src/pages/{index.tsx → index.ts} +0 -0
  87. package/src/pages/projects.tsx +3 -5
  88. package/src/pages/publish.tsx +5 -4
  89. package/src/pages/upload.tsx +1 -1
  90. package/src/publish/publish.ts +9 -7
  91. package/src/pubsub/{pubsub-commands.tsx → pubsub-commands.ts} +1 -1
  92. package/src/secret/index.ts +1 -1
  93. package/src/{sites.tsx → sites.ts} +0 -0
  94. package/src/tail/index.ts +2 -3
  95. package/src/tsconfig-sanity.ts +16 -0
  96. package/src/user/access.ts +0 -1
  97. package/src/user/auth-variables.ts +113 -0
  98. package/src/user/choose-account.tsx +1 -31
  99. package/src/user/index.ts +0 -1
  100. package/src/user/{user.tsx → user.ts} +107 -73
  101. package/src/{whoami.tsx → whoami.ts} +37 -71
  102. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  103. package/templates/__tests__/tsconfig.json +8 -0
  104. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  105. package/templates/d1-beta-facade.js +36 -0
  106. package/templates/facade.d.ts +14 -0
  107. package/templates/first-party-worker-module-facade.ts +4 -3
  108. package/templates/format-dev-errors.ts +7 -6
  109. package/templates/init-tests/test-jest-new-worker.js +3 -5
  110. package/templates/init-tests/test-vitest-new-worker.js +3 -5
  111. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  112. package/templates/middleware/loader-modules.ts +0 -2
  113. package/templates/middleware/loader-sw.ts +6 -0
  114. package/templates/pages-dev-pipeline.ts +4 -1
  115. package/templates/pages-shim.ts +4 -1
  116. package/templates/pages-template-plugin.ts +12 -7
  117. package/templates/serve-static-assets.ts +16 -14
  118. package/templates/tsconfig-sanity.ts +11 -0
  119. package/templates/tsconfig.init.json +106 -0
  120. package/templates/tsconfig.json +5 -103
  121. package/templates/tsconfig.tsbuildinfo +1 -0
  122. package/wrangler-dist/cli.d.ts +58 -60
  123. package/wrangler-dist/cli.js +34447 -55514
  124. package/wrangler-dist/wasm-sync.wasm +0 -0
  125. package/src/__tests__/dialogs.test.tsx +0 -40
  126. package/src/dialogs.tsx +0 -168
  127. package/src/environment-variables.ts +0 -50
  128. package/src/user/env-vars.ts +0 -46
@@ -2,23 +2,17 @@ import * as fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import * as TOML from "@iarna/toml";
4
4
  import { execa, execaSync } from "execa";
5
+ import { rest } from "msw";
5
6
  import { parseConfigFileTextToJson } from "typescript";
7
+ import { FormData } from "undici";
6
8
  import { version as wranglerVersion } from "../../package.json";
9
+ import { fetchDashboardScript } from "../cfetch/internal";
7
10
  import { getPackageManager } from "../package-manager";
8
11
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
9
- import {
10
- setMockFetchDashScript,
11
- setMockResponse,
12
- unsetAllMocks,
13
- unsetSpecialMockFns,
14
- } from "./helpers/mock-cfetch";
15
12
  import { mockConsoleMethods } from "./helpers/mock-console";
16
- import {
17
- mockConfirm,
18
- clearConfirmMocks,
19
- mockSelect,
20
- clearSelectMocks,
21
- } from "./helpers/mock-dialogs";
13
+ import { clearDialogs, mockConfirm, mockSelect } from "./helpers/mock-dialogs";
14
+ import { useMockIsTTY } from "./helpers/mock-istty";
15
+ import { msw } from "./helpers/msw";
22
16
  import { runInTempDir } from "./helpers/run-in-tmp";
23
17
  import { runWrangler } from "./helpers/run-wrangler";
24
18
  import type { PackageManager } from "../package-manager";
@@ -35,8 +29,11 @@ const MINIMAL_WRANGLER_TOML = {
35
29
  describe("init", () => {
36
30
  let mockPackageManager: PackageManager;
37
31
  runInTempDir();
32
+ const { setIsTTY } = useMockIsTTY();
38
33
 
39
34
  beforeEach(() => {
35
+ setIsTTY(true);
36
+
40
37
  mockPackageManager = {
41
38
  cwd: process.cwd(),
42
39
  // @ts-expect-error we're making a fake package manager here
@@ -48,8 +45,9 @@ describe("init", () => {
48
45
  });
49
46
 
50
47
  afterEach(() => {
51
- clearConfirmMocks();
52
- clearSelectMocks();
48
+ clearDialogs();
49
+ msw.resetHandlers();
50
+ msw.restoreHandlers();
53
51
  });
54
52
 
55
53
  const std = mockConsoleMethods();
@@ -69,16 +67,18 @@ describe("init", () => {
69
67
  });
70
68
 
71
69
  expect(std.out).toMatchInlineSnapshot(`
72
- "✨ Created wrangler.toml
73
- ✨ Initialized git repository
74
- ✨ Created package.json
75
- ✨ Created tsconfig.json
76
- ✨ Created src/index.ts
77
- Installed @cloudflare/workers-types and typescript into devDependencies
70
+ "✨ Created wrangler.toml
71
+ ✨ Initialized git repository
72
+ ✨ Created package.json
73
+ ✨ Created tsconfig.json
74
+ ✨ Created src/index.ts
75
+ Created src/index.test.ts
76
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
78
77
 
79
- To start developing your Worker, run \`npm start\`
80
- To publish your Worker to the Internet, run \`npm run deploy\`"
81
- `);
78
+ To start developing your Worker, run \`npm start\`
79
+ To start testing your Worker, run \`npm test\`
80
+ To publish your Worker to the Internet, run \`npm run deploy\`"
81
+ `);
82
82
  expect(std.err).toMatchInlineSnapshot(`""`);
83
83
  expect(std.warn).toMatchInlineSnapshot(`""`);
84
84
  });
@@ -100,16 +100,18 @@ describe("init", () => {
100
100
  });
101
101
 
102
102
  expect(std.out).toMatchInlineSnapshot(`
103
- "✨ Created my-worker/wrangler.toml
104
- ✨ Initialized git repository at my-worker
105
- ✨ Created my-worker/package.json
106
- ✨ Created my-worker/tsconfig.json
107
- ✨ Created my-worker/src/index.ts
108
- Installed @cloudflare/workers-types and typescript into devDependencies
109
-
110
- To start developing your Worker, run \`cd my-worker && npm start\`
111
- To publish your Worker to the Internet, run \`npm run deploy\`"
112
- `);
103
+ "✨ Created my-worker/wrangler.toml
104
+ ✨ Initialized git repository at my-worker
105
+ ✨ Created my-worker/package.json
106
+ ✨ Created my-worker/tsconfig.json
107
+ ✨ Created my-worker/src/index.ts
108
+ Created my-worker/src/index.test.ts
109
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
110
+
111
+ To start developing your Worker, run \`cd my-worker && npm start\`
112
+ To start testing your Worker, run \`npm test\`
113
+ To publish your Worker to the Internet, run \`npm run deploy\`"
114
+ `);
113
115
  expect(std.err).toMatchInlineSnapshot(`""`);
114
116
  expect(std.warn).toMatchInlineSnapshot(`""`);
115
117
  });
@@ -128,21 +130,23 @@ describe("init", () => {
128
130
  });
129
131
 
130
132
  expect(std).toMatchInlineSnapshot(`
131
- Object {
132
- "debug": "",
133
- "err": "",
134
- "out": "✨ Created wrangler.toml
135
- ✨ Initialized git repository
136
- ✨ Created package.json
137
- ✨ Created tsconfig.json
138
- ✨ Created src/index.ts
139
- Installed @cloudflare/workers-types and typescript into devDependencies
133
+ Object {
134
+ "debug": "",
135
+ "err": "",
136
+ "out": "✨ Created wrangler.toml
137
+ ✨ Initialized git repository
138
+ ✨ Created package.json
139
+ ✨ Created tsconfig.json
140
+ ✨ Created src/index.ts
141
+ Created src/index.test.ts
142
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
140
143
 
141
- To start developing your Worker, run \`npm start\`
142
- To publish your Worker to the Internet, run \`npm run deploy\`",
143
- "warn": "",
144
- }
145
- `);
144
+ To start developing your Worker, run \`npm start\`
145
+ To start testing your Worker, run \`npm test\`
146
+ To publish your Worker to the Internet, run \`npm run deploy\`",
147
+ "warn": "",
148
+ }
149
+ `);
146
150
  });
147
151
 
148
152
  it("should error if `--type javascript` is used", async () => {
@@ -320,15 +324,15 @@ describe("init", () => {
320
324
  },
321
325
  });
322
326
  expect(std).toMatchInlineSnapshot(`
323
- Object {
324
- "debug": "",
325
- "err": "",
326
- "out": "",
327
- "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
327
+ Object {
328
+ "debug": "",
329
+ "err": "",
330
+ "out": "",
331
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
328
332
 
329
- ",
330
- }
331
- `);
333
+ ",
334
+ }
335
+ `);
332
336
  });
333
337
 
334
338
  it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
@@ -341,11 +345,11 @@ describe("init", () => {
341
345
  });
342
346
  mockConfirm(
343
347
  {
344
- text: "Would you like to use git to manage this Worker?",
348
+ text: "Do you want to continue initializing this project?",
345
349
  result: true,
346
350
  },
347
351
  {
348
- text: "Do you want to continue initializing this project?",
352
+ text: "Would you like to use git to manage this Worker?",
349
353
  result: true,
350
354
  },
351
355
  {
@@ -359,9 +363,16 @@ describe("init", () => {
359
363
  );
360
364
 
361
365
  mockSelect({
362
- text: "Would you like to create a Worker at src/index.ts?",
366
+ text: `Would you like to create a Worker at ${path.join(
367
+ "src",
368
+ "index.ts"
369
+ )}?`,
363
370
  result: "fetch",
364
371
  });
372
+ mockConfirm({
373
+ text: "Would you like us to write your first test with Vitest?",
374
+ result: true,
375
+ });
365
376
 
366
377
  await runWrangler("init");
367
378
 
@@ -408,15 +419,15 @@ describe("init", () => {
408
419
  },
409
420
  });
410
421
  expect(std).toMatchInlineSnapshot(`
411
- Object {
412
- "debug": "",
413
- "err": "",
414
- "out": "",
415
- "warn": "▲ [WARNING] wrangler.toml already exists!
422
+ Object {
423
+ "debug": "",
424
+ "err": "",
425
+ "out": "",
426
+ "warn": "▲ [WARNING] wrangler.toml already exists!
416
427
 
417
- ",
418
- }
419
- `);
428
+ ",
429
+ }
430
+ `);
420
431
  });
421
432
 
422
433
  it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
@@ -429,13 +440,14 @@ describe("init", () => {
429
440
  });
430
441
  mockConfirm(
431
442
  {
432
- text: "Would you like to use git to manage this Worker?",
443
+ text: "Do you want to continue initializing this project?",
433
444
  result: true,
434
445
  },
435
446
  {
436
- text: "Do you want to continue initializing this project?",
447
+ text: "Would you like to use git to manage this Worker?",
437
448
  result: true,
438
449
  },
450
+
439
451
  {
440
452
  text: "No package.json found. Would you like to create one?",
441
453
  result: true,
@@ -446,10 +458,17 @@ describe("init", () => {
446
458
  }
447
459
  );
448
460
  mockSelect({
449
- text: "Would you like to create a Worker at src/index.ts?",
461
+ text: `Would you like to create a Worker at ${path.join(
462
+ "src",
463
+ "index.ts"
464
+ )}?`,
450
465
  result: "scheduled",
451
466
  });
452
467
 
468
+ mockConfirm({
469
+ text: "Would you like us to write your first test with Vitest?",
470
+ result: true,
471
+ });
453
472
  await runWrangler("init");
454
473
 
455
474
  checkFiles({
@@ -477,10 +496,16 @@ describe("init", () => {
477
496
  }
478
497
  );
479
498
  mockSelect({
480
- text: "Would you like to create a Worker at src/index.ts?",
499
+ text: `Would you like to create a Worker at ${path.join(
500
+ "src",
501
+ "index.ts"
502
+ )}?`,
481
503
  result: "scheduled",
482
504
  });
483
-
505
+ mockConfirm({
506
+ text: "Would you like us to write your first test with Vitest?",
507
+ result: true,
508
+ });
484
509
  await runWrangler("init");
485
510
 
486
511
  checkFiles({
@@ -552,20 +577,22 @@ describe("init", () => {
552
577
 
553
578
  // Note the lack of "✨ Initialized git repository" in the log
554
579
  expect(std).toMatchInlineSnapshot(`
555
- Object {
556
- "debug": "",
557
- "err": "",
558
- "out": "✨ Created wrangler.toml
559
- ✨ Created package.json
560
- ✨ Created tsconfig.json
561
- ✨ Created src/index.ts
562
- Installed @cloudflare/workers-types and typescript into devDependencies
580
+ Object {
581
+ "debug": "",
582
+ "err": "",
583
+ "out": "✨ Created wrangler.toml
584
+ ✨ Created package.json
585
+ ✨ Created tsconfig.json
586
+ ✨ Created src/index.ts
587
+ Created src/index.test.ts
588
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
563
589
 
564
- To start developing your Worker, run \`npm start\`
565
- To publish your Worker to the Internet, run \`npm run deploy\`",
566
- "warn": "",
567
- }
568
- `);
590
+ To start developing your Worker, run \`npm start\`
591
+ To start testing your Worker, run \`npm test\`
592
+ To publish your Worker to the Internet, run \`npm run deploy\`",
593
+ "warn": "",
594
+ }
595
+ `);
569
596
  });
570
597
 
571
598
  it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
@@ -577,20 +604,22 @@ describe("init", () => {
577
604
 
578
605
  // Note the lack of "✨ Initialized git repository" in the log
579
606
  expect(std).toMatchInlineSnapshot(`
580
- Object {
581
- "debug": "",
582
- "err": "",
583
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
584
- ✨ Created path/to/worker/my-worker/package.json
585
- ✨ Created path/to/worker/my-worker/tsconfig.json
586
- ✨ Created path/to/worker/my-worker/src/index.ts
587
- Installed @cloudflare/workers-types and typescript into devDependencies
588
-
589
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
590
- To publish your Worker to the Internet, run \`npm run deploy\`",
591
- "warn": "",
592
- }
593
- `);
607
+ Object {
608
+ "debug": "",
609
+ "err": "",
610
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
611
+ ✨ Created path/to/worker/my-worker/package.json
612
+ ✨ Created path/to/worker/my-worker/tsconfig.json
613
+ ✨ Created path/to/worker/my-worker/src/index.ts
614
+ Created path/to/worker/my-worker/src/index.test.ts
615
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
616
+
617
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
618
+ To start testing your Worker, run \`npm test\`
619
+ To publish your Worker to the Internet, run \`npm run deploy\`",
620
+ "warn": "",
621
+ }
622
+ `);
594
623
  });
595
624
 
596
625
  // I... don't know how to test this lol
@@ -611,14 +640,14 @@ describe("init", () => {
611
640
  );
612
641
  await runWrangler("init");
613
642
  expect(std).toMatchInlineSnapshot(`
614
- Object {
615
- "debug": "",
616
- "err": "",
617
- "out": "✨ Created wrangler.toml
618
- ✨ Initialized git repository",
619
- "warn": "",
620
- }
621
- `);
643
+ Object {
644
+ "debug": "",
645
+ "err": "",
646
+ "out": "✨ Created wrangler.toml
647
+ ✨ Initialized git repository",
648
+ "warn": "",
649
+ }
650
+ `);
622
651
 
623
652
  expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
624
653
  `refs/heads/${getDefaultBranchName()}`
@@ -643,7 +672,10 @@ describe("init", () => {
643
672
  }
644
673
  );
645
674
  mockSelect({
646
- text: "Would you like to create a Worker at src/index.js?",
675
+ text: `Would you like to create a Worker at ${path.join(
676
+ "src",
677
+ "index.js"
678
+ )}?`,
647
679
  result: "none",
648
680
  });
649
681
 
@@ -691,7 +723,11 @@ describe("init", () => {
691
723
  }
692
724
  );
693
725
  mockSelect({
694
- text: "Would you like to create a Worker at my-worker/src/index.js?",
726
+ text: `Would you like to create a Worker at ${path.join(
727
+ "my-worker",
728
+ "src",
729
+ "index.js"
730
+ )}?`,
695
731
  result: "none",
696
732
  });
697
733
 
@@ -738,7 +774,10 @@ describe("init", () => {
738
774
  }
739
775
  );
740
776
  mockSelect({
741
- text: "Would you like to create a Worker at src/index.js?",
777
+ text: `Would you like to create a Worker at ${path.join(
778
+ "src",
779
+ "index.js"
780
+ )}?`,
742
781
  result: "none",
743
782
  });
744
783
  writeFiles({
@@ -755,13 +794,13 @@ describe("init", () => {
755
794
  },
756
795
  });
757
796
  expect(std).toMatchInlineSnapshot(`
758
- Object {
759
- "debug": "",
760
- "err": "",
761
- "out": "✨ Created wrangler.toml",
762
- "warn": "",
763
- }
764
- `);
797
+ Object {
798
+ "debug": "",
799
+ "err": "",
800
+ "out": "✨ Created wrangler.toml",
801
+ "warn": "",
802
+ }
803
+ `);
765
804
  });
766
805
 
767
806
  it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
@@ -774,17 +813,20 @@ describe("init", () => {
774
813
  text: "No package.json found. Would you like to create one?",
775
814
  result: true,
776
815
  },
777
- {
778
- text: "Would you like to install wrangler into path/to/worker/my-worker/package.json?",
779
- result: false,
780
- },
781
816
  {
782
817
  text: "Would you like to use TypeScript?",
783
818
  result: false,
784
819
  }
785
820
  );
786
821
  mockSelect({
787
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
822
+ text: `Would you like to create a Worker at ${path.join(
823
+ "path",
824
+ "to",
825
+ "worker",
826
+ "my-worker",
827
+ "src",
828
+ "index.js"
829
+ )}?`,
788
830
  result: "none",
789
831
  });
790
832
  writeFiles({
@@ -805,14 +847,14 @@ describe("init", () => {
805
847
  },
806
848
  });
807
849
  expect(std).toMatchInlineSnapshot(`
808
- Object {
809
- "debug": "",
810
- "err": "",
811
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
812
- ✨ Created path/to/worker/my-worker/package.json",
813
- "warn": "",
814
- }
815
- `);
850
+ Object {
851
+ "debug": "",
852
+ "err": "",
853
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
854
+ ✨ Created path/to/worker/my-worker/package.json",
855
+ "warn": "",
856
+ }
857
+ `);
816
858
  });
817
859
 
818
860
  it("should offer to install wrangler into an existing package.json", async () => {
@@ -831,7 +873,10 @@ describe("init", () => {
831
873
  }
832
874
  );
833
875
  mockSelect({
834
- text: "Would you like to create a Worker at src/index.js?",
876
+ text: `Would you like to create a Worker at ${path.join(
877
+ "src",
878
+ "index.js"
879
+ )}?`,
835
880
  result: "none",
836
881
  });
837
882
  writeFiles({
@@ -872,7 +917,10 @@ describe("init", () => {
872
917
  result: false,
873
918
  },
874
919
  {
875
- text: "Would you like to install wrangler into ../package.json?",
920
+ text: `Would you like to install wrangler into ${path.join(
921
+ "..",
922
+ "package.json"
923
+ )}?`,
876
924
  result: true,
877
925
  },
878
926
  {
@@ -881,7 +929,10 @@ describe("init", () => {
881
929
  }
882
930
  );
883
931
  mockSelect({
884
- text: "Would you like to create a Worker at src/index.js?",
932
+ text: `Would you like to create a Worker at ${path.join(
933
+ "src",
934
+ "index.js"
935
+ )}?`,
885
936
  result: "none",
886
937
  });
887
938
  writeFiles({
@@ -926,7 +977,11 @@ describe("init", () => {
926
977
  result: false,
927
978
  },
928
979
  {
929
- text: "Would you like to install wrangler into ../../package.json?",
980
+ text: `Would you like to install wrangler into ${path.join(
981
+ "..",
982
+ "..",
983
+ "package.json"
984
+ )}?`,
930
985
  result: false,
931
986
  },
932
987
  {
@@ -935,7 +990,10 @@ describe("init", () => {
935
990
  }
936
991
  );
937
992
  mockSelect({
938
- text: "Would you like to create a Worker at src/index.js?",
993
+ text: `Would you like to create a Worker at ${path.join(
994
+ "src",
995
+ "index.js"
996
+ )}?`,
939
997
  result: "none",
940
998
  });
941
999
  writeFiles({
@@ -958,13 +1016,13 @@ describe("init", () => {
958
1016
  },
959
1017
  });
960
1018
  expect(std).toMatchInlineSnapshot(`
961
- Object {
962
- "debug": "",
963
- "err": "",
964
- "out": "✨ Created wrangler.toml",
965
- "warn": "",
966
- }
967
- `);
1019
+ Object {
1020
+ "debug": "",
1021
+ "err": "",
1022
+ "out": "✨ Created wrangler.toml",
1023
+ "warn": "",
1024
+ }
1025
+ `);
968
1026
  });
969
1027
  });
970
1028
 
@@ -982,13 +1040,19 @@ describe("init", () => {
982
1040
  {
983
1041
  text: "Would you like to use TypeScript?",
984
1042
  result: false,
985
- },
986
- { text: "Would you like us to write your first test?", result: false }
1043
+ }
987
1044
  );
988
1045
  mockSelect({
989
- text: "Would you like to create a Worker at src/index.js?",
1046
+ text: `Would you like to create a Worker at ${path.join(
1047
+ "src",
1048
+ "index.js"
1049
+ )}?`,
990
1050
  result: "fetch",
991
1051
  });
1052
+ mockConfirm({
1053
+ text: "Would you like us to write your first test?",
1054
+ result: false,
1055
+ });
992
1056
  writeFiles({
993
1057
  items: {
994
1058
  "package.json": {
@@ -1039,9 +1103,16 @@ describe("init", () => {
1039
1103
  }
1040
1104
  );
1041
1105
  mockSelect({
1042
- text: "Would you like to create a Worker at src/index.ts?",
1106
+ text: `Would you like to create a Worker at ${path.join(
1107
+ "src",
1108
+ "index.ts"
1109
+ )}?`,
1043
1110
  result: "fetch",
1044
1111
  });
1112
+ mockConfirm({
1113
+ text: "Would you like us to write your first test with Vitest?",
1114
+ result: true,
1115
+ });
1045
1116
  writeFiles({
1046
1117
  items: {
1047
1118
  "package.json": {
@@ -1059,19 +1130,20 @@ describe("init", () => {
1059
1130
  },
1060
1131
  });
1061
1132
  expect(std).toMatchInlineSnapshot(`
1062
- Object {
1063
- "debug": "",
1064
- "err": "",
1065
- "out": "✨ Created wrangler.toml
1066
- ✨ Created tsconfig.json
1067
- ✨ Created src/index.ts
1068
- Installed @cloudflare/workers-types and typescript into devDependencies
1069
-
1070
- To start developing your Worker, run \`npx wrangler dev\`
1071
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1072
- "warn": "",
1073
- }
1074
- `);
1133
+ Object {
1134
+ "debug": "",
1135
+ "err": "",
1136
+ "out": "✨ Created wrangler.toml
1137
+ ✨ Created tsconfig.json
1138
+ ✨ Created src/index.ts
1139
+ Created src/index.test.ts
1140
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1141
+
1142
+ To start developing your Worker, run \`npx wrangler dev\`
1143
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1144
+ "warn": "",
1145
+ }
1146
+ `);
1075
1147
  });
1076
1148
 
1077
1149
  it("should add scripts for a typescript project with .ts extension", async () => {
@@ -1084,19 +1156,23 @@ describe("init", () => {
1084
1156
  text: "No package.json found. Would you like to create one?",
1085
1157
  result: true,
1086
1158
  },
1087
- {
1088
- text: "Would you like to install wrangler into package.json?",
1089
- result: false,
1090
- },
1159
+
1091
1160
  {
1092
1161
  text: "Would you like to use TypeScript?",
1093
1162
  result: true,
1094
1163
  }
1095
1164
  );
1096
1165
  mockSelect({
1097
- text: "Would you like to create a Worker at src/index.ts?",
1166
+ text: `Would you like to create a Worker at ${path.join(
1167
+ "src",
1168
+ "index.ts"
1169
+ )}?`,
1098
1170
  result: "fetch",
1099
1171
  });
1172
+ mockConfirm({
1173
+ text: "Would you like us to write your first test with Vitest?",
1174
+ result: true,
1175
+ });
1100
1176
 
1101
1177
  await runWrangler("init");
1102
1178
 
@@ -1107,8 +1183,12 @@ describe("init", () => {
1107
1183
  name: expect.stringContaining("wrangler-tests"),
1108
1184
  version: "0.0.0",
1109
1185
  scripts: {
1110
- start: "wrangler dev",
1111
1186
  deploy: "wrangler publish",
1187
+ start: "wrangler dev",
1188
+ test: "vitest",
1189
+ },
1190
+ devDependencies: {
1191
+ wrangler: expect.any(String),
1112
1192
  },
1113
1193
  }),
1114
1194
  },
@@ -1117,15 +1197,17 @@ describe("init", () => {
1117
1197
  },
1118
1198
  });
1119
1199
  expect(std.out).toMatchInlineSnapshot(`
1120
- "✨ Created wrangler.toml
1121
- ✨ Created package.json
1122
- ✨ Created tsconfig.json
1123
- ✨ Created src/index.ts
1124
- Installed @cloudflare/workers-types and typescript into devDependencies
1200
+ "✨ Created wrangler.toml
1201
+ ✨ Created package.json
1202
+ ✨ Created tsconfig.json
1203
+ ✨ Created src/index.ts
1204
+ Created src/index.test.ts
1205
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1125
1206
 
1126
- To start developing your Worker, run \`npm start\`
1127
- To publish your Worker to the Internet, run \`npm run deploy\`"
1128
- `);
1207
+ To start developing your Worker, run \`npm start\`
1208
+ To start testing your Worker, run \`npm test\`
1209
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1210
+ `);
1129
1211
  });
1130
1212
 
1131
1213
  it("should not overwrite package.json scripts for a typescript project", async () => {
@@ -1144,9 +1226,16 @@ describe("init", () => {
1144
1226
  }
1145
1227
  );
1146
1228
  mockSelect({
1147
- text: "Would you like to create a Worker at src/index.ts?",
1229
+ text: `Would you like to create a Worker at ${path.join(
1230
+ "src",
1231
+ "index.ts"
1232
+ )}?`,
1148
1233
  result: "fetch",
1149
1234
  });
1235
+ mockConfirm({
1236
+ text: "Would you like us to write your first test with Vitest?",
1237
+ result: true,
1238
+ });
1150
1239
  writeFiles({
1151
1240
  items: {
1152
1241
  "package.json": {
@@ -1177,14 +1266,15 @@ describe("init", () => {
1177
1266
  },
1178
1267
  });
1179
1268
  expect(std.out).toMatchInlineSnapshot(`
1180
- "✨ Created wrangler.toml
1181
- ✨ Created tsconfig.json
1182
- ✨ Created src/index.ts
1183
- Installed @cloudflare/workers-types and typescript into devDependencies
1269
+ "✨ Created wrangler.toml
1270
+ ✨ Created tsconfig.json
1271
+ ✨ Created src/index.ts
1272
+ Created src/index.test.ts
1273
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1184
1274
 
1185
- To start developing your Worker, run \`npx wrangler dev\`
1186
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1187
- `);
1275
+ To start developing your Worker, run \`npx wrangler dev\`
1276
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1277
+ `);
1188
1278
  });
1189
1279
 
1190
1280
  it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
@@ -1219,15 +1309,15 @@ describe("init", () => {
1219
1309
  },
1220
1310
  });
1221
1311
  expect(std).toMatchInlineSnapshot(`
1222
- Object {
1223
- "debug": "",
1224
- "err": "",
1225
- "out": "✨ Created wrangler.toml
1226
- ✨ Created tsconfig.json
1227
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1228
- "warn": "",
1229
- }
1230
- `);
1312
+ Object {
1313
+ "debug": "",
1314
+ "err": "",
1315
+ "out": "✨ Created wrangler.toml
1316
+ ✨ Created tsconfig.json
1317
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1318
+ "warn": "",
1319
+ }
1320
+ `);
1231
1321
  });
1232
1322
 
1233
1323
  it("should not offer to create a worker in a ts project for a named worker if a file already exists at the location", async () => {
@@ -1240,10 +1330,7 @@ describe("init", () => {
1240
1330
  text: "No package.json found. Would you like to create one?",
1241
1331
  result: true,
1242
1332
  },
1243
- {
1244
- text: "Would you like to install wrangler into package.json?",
1245
- result: false,
1246
- },
1333
+
1247
1334
  {
1248
1335
  text: "Would you like to use TypeScript?",
1249
1336
  result: true,
@@ -1266,16 +1353,16 @@ describe("init", () => {
1266
1353
  },
1267
1354
  });
1268
1355
  expect(std).toMatchInlineSnapshot(`
1269
- Object {
1270
- "debug": "",
1271
- "err": "",
1272
- "out": "✨ Created my-worker/wrangler.toml
1273
- ✨ Created my-worker/package.json
1274
- ✨ Created my-worker/tsconfig.json
1275
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1276
- "warn": "",
1277
- }
1278
- `);
1356
+ Object {
1357
+ "debug": "",
1358
+ "err": "",
1359
+ "out": "✨ Created my-worker/wrangler.toml
1360
+ ✨ Created my-worker/package.json
1361
+ ✨ Created my-worker/tsconfig.json
1362
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1363
+ "warn": "",
1364
+ }
1365
+ `);
1279
1366
  });
1280
1367
 
1281
1368
  it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
@@ -1294,7 +1381,10 @@ describe("init", () => {
1294
1381
  }
1295
1382
  );
1296
1383
  mockSelect({
1297
- text: "Would you like to create a Worker at src/index.ts?",
1384
+ text: `Would you like to create a Worker at ${path.join(
1385
+ "src",
1386
+ "index.ts"
1387
+ )}?`,
1298
1388
  result: "none",
1299
1389
  });
1300
1390
 
@@ -1319,16 +1409,16 @@ describe("init", () => {
1319
1409
  "typescript"
1320
1410
  );
1321
1411
  expect(std).toMatchInlineSnapshot(`
1322
- Object {
1323
- "debug": "",
1324
- "err": "",
1325
- "out": "✨ Created wrangler.toml
1326
- ✨ Created package.json
1327
- ✨ Created tsconfig.json
1328
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1329
- "warn": "",
1330
- }
1331
- `);
1412
+ Object {
1413
+ "debug": "",
1414
+ "err": "",
1415
+ "out": "✨ Created wrangler.toml
1416
+ ✨ Created package.json
1417
+ ✨ Created tsconfig.json
1418
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1419
+ "warn": "",
1420
+ }
1421
+ `);
1332
1422
  });
1333
1423
 
1334
1424
  it("should not touch an existing tsconfig.json in the same directory", async () => {
@@ -1337,9 +1427,16 @@ describe("init", () => {
1337
1427
  result: false,
1338
1428
  });
1339
1429
  mockSelect({
1340
- text: "Would you like to create a Worker at src/index.ts?",
1430
+ text: `Would you like to create a Worker at ${path.join(
1431
+ "src",
1432
+ "index.ts"
1433
+ )}?`,
1341
1434
  result: "fetch",
1342
1435
  });
1436
+ mockConfirm({
1437
+ text: "Would you like us to write your first test with Vitest?",
1438
+ result: true,
1439
+ });
1343
1440
  writeFiles({
1344
1441
  items: {
1345
1442
  "package.json": {
@@ -1366,17 +1463,19 @@ describe("init", () => {
1366
1463
  },
1367
1464
  });
1368
1465
  expect(std).toMatchInlineSnapshot(`
1369
- Object {
1370
- "debug": "",
1371
- "err": "",
1372
- "out": "✨ Created wrangler.toml
1373
- ✨ Created src/index.ts
1466
+ Object {
1467
+ "debug": "",
1468
+ "err": "",
1469
+ "out": "✨ Created wrangler.toml
1470
+ ✨ Created src/index.ts
1471
+ ✨ Created src/index.test.ts
1472
+ ✨ Installed vitest into devDependencies
1374
1473
 
1375
- To start developing your Worker, run \`npx wrangler dev\`
1376
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1377
- "warn": "",
1378
- }
1379
- `);
1474
+ To start developing your Worker, run \`npx wrangler dev\`
1475
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1476
+ "warn": "",
1477
+ }
1478
+ `);
1380
1479
  });
1381
1480
 
1382
1481
  it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
@@ -1395,9 +1494,20 @@ describe("init", () => {
1395
1494
  }
1396
1495
  );
1397
1496
  mockSelect({
1398
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1497
+ text: `Would you like to create a Worker at ${path.join(
1498
+ "path",
1499
+ "to",
1500
+ "worker",
1501
+ "my-worker",
1502
+ "src",
1503
+ "index.ts"
1504
+ )}?`,
1399
1505
  result: "fetch",
1400
1506
  });
1507
+ mockConfirm({
1508
+ text: "Would you like us to write your first test with Vitest?",
1509
+ result: true,
1510
+ });
1401
1511
  writeFiles({
1402
1512
  items: {
1403
1513
  "path/to/worker/package.json": {
@@ -1424,20 +1534,22 @@ describe("init", () => {
1424
1534
  },
1425
1535
  });
1426
1536
  expect(std).toMatchInlineSnapshot(`
1427
- Object {
1428
- "debug": "",
1429
- "err": "",
1430
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1431
- ✨ Created path/to/worker/my-worker/package.json
1432
- ✨ Created path/to/worker/my-worker/tsconfig.json
1433
- ✨ Created path/to/worker/my-worker/src/index.ts
1434
- Installed @cloudflare/workers-types and typescript into devDependencies
1435
-
1436
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1437
- To publish your Worker to the Internet, run \`npm run deploy\`",
1438
- "warn": "",
1439
- }
1440
- `);
1537
+ Object {
1538
+ "debug": "",
1539
+ "err": "",
1540
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1541
+ ✨ Created path/to/worker/my-worker/package.json
1542
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1543
+ ✨ Created path/to/worker/my-worker/src/index.ts
1544
+ Created path/to/worker/my-worker/src/index.test.ts
1545
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1546
+
1547
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1548
+ To start testing your Worker, run \`npm test\`
1549
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1550
+ "warn": "",
1551
+ }
1552
+ `);
1441
1553
  });
1442
1554
 
1443
1555
  it("should offer to install type definitions in an existing typescript project", async () => {
@@ -1456,7 +1568,10 @@ describe("init", () => {
1456
1568
  }
1457
1569
  );
1458
1570
  mockSelect({
1459
- text: "Would you like to create a Worker at src/index.ts?",
1571
+ text: `Would you like to create a Worker at ${path.join(
1572
+ "src",
1573
+ "index.ts"
1574
+ )}?`,
1460
1575
  result: "none",
1461
1576
  });
1462
1577
  writeFiles({
@@ -1500,9 +1615,16 @@ describe("init", () => {
1500
1615
  result: false,
1501
1616
  });
1502
1617
  mockSelect({
1503
- text: "Would you like to create a Worker at src/index.ts?",
1618
+ text: `Would you like to create a Worker at ${path.join(
1619
+ "src",
1620
+ "index.ts"
1621
+ )}?`,
1504
1622
  result: "fetch",
1505
1623
  });
1624
+ mockConfirm({
1625
+ text: "Would you like us to write your first test with Vitest?",
1626
+ result: true,
1627
+ });
1506
1628
  writeFiles({
1507
1629
  items: {
1508
1630
  "package.json": {
@@ -1531,17 +1653,19 @@ describe("init", () => {
1531
1653
  },
1532
1654
  });
1533
1655
  expect(std).toMatchInlineSnapshot(`
1534
- Object {
1535
- "debug": "",
1536
- "err": "",
1537
- "out": "✨ Created wrangler.toml
1538
- ✨ Created src/index.ts
1656
+ Object {
1657
+ "debug": "",
1658
+ "err": "",
1659
+ "out": "✨ Created wrangler.toml
1660
+ ✨ Created src/index.ts
1661
+ ✨ Created src/index.test.ts
1662
+ ✨ Installed vitest into devDependencies
1539
1663
 
1540
- To start developing your Worker, run \`npx wrangler dev\`
1541
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1542
- "warn": "",
1543
- }
1544
- `);
1664
+ To start developing your Worker, run \`npx wrangler dev\`
1665
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1666
+ "warn": "",
1667
+ }
1668
+ `);
1545
1669
  });
1546
1670
  });
1547
1671
 
@@ -1556,21 +1680,23 @@ describe("init", () => {
1556
1680
  text: "No package.json found. Would you like to create one?",
1557
1681
  result: true,
1558
1682
  },
1559
- {
1560
- text: "Would you like to install wrangler into package.json?",
1561
- result: false,
1562
- },
1683
+
1563
1684
  {
1564
1685
  text: "Would you like to use TypeScript?",
1565
1686
  result: false,
1566
- },
1567
- { text: "Would you like us to write your first test?", result: false }
1687
+ }
1568
1688
  );
1569
1689
  mockSelect({
1570
- text: "Would you like to create a Worker at src/index.js?",
1690
+ text: `Would you like to create a Worker at ${path.join(
1691
+ "src",
1692
+ "index.js"
1693
+ )}?`,
1571
1694
  result: "fetch",
1572
1695
  });
1573
-
1696
+ mockConfirm({
1697
+ text: "Would you like us to write your first test?",
1698
+ result: false,
1699
+ });
1574
1700
  await runWrangler("init");
1575
1701
 
1576
1702
  checkFiles({
@@ -1608,24 +1734,26 @@ describe("init", () => {
1608
1734
  text: "No package.json found. Would you like to create one?",
1609
1735
  result: true,
1610
1736
  },
1611
- {
1612
- text: "Would you like to install wrangler into package.json?",
1613
- result: false,
1614
- },
1615
1737
  {
1616
1738
  text: "Would you like to use TypeScript?",
1617
1739
  result: false,
1618
- },
1619
- { text: "Would you like us to write your first test?", result: true }
1620
- );
1621
- mockSelect(
1622
- {
1623
- text: "Would you like to create a Worker at src/index.js?",
1624
- result: "fetch",
1625
- },
1626
- { text: "Which test runner would you like to use?", result: "jest" }
1740
+ }
1627
1741
  );
1628
-
1742
+ mockSelect({
1743
+ text: `Would you like to create a Worker at ${path.join(
1744
+ "src",
1745
+ "index.js"
1746
+ )}?`,
1747
+ result: "fetch",
1748
+ });
1749
+ mockConfirm({
1750
+ text: "Would you like us to write your first test?",
1751
+ result: true,
1752
+ });
1753
+ mockSelect({
1754
+ text: "Which test runner would you like to use?",
1755
+ result: "jest",
1756
+ });
1629
1757
  await runWrangler("init");
1630
1758
 
1631
1759
  checkFiles({
@@ -1669,24 +1797,26 @@ describe("init", () => {
1669
1797
  text: "No package.json found. Would you like to create one?",
1670
1798
  result: true,
1671
1799
  },
1672
- {
1673
- text: "Would you like to install wrangler into package.json?",
1674
- result: false,
1675
- },
1676
1800
  {
1677
1801
  text: "Would you like to use TypeScript?",
1678
1802
  result: false,
1679
- },
1680
- { text: "Would you like us to write your first test?", result: true }
1681
- );
1682
- mockSelect(
1683
- {
1684
- text: "Would you like to create a Worker at src/index.js?",
1685
- result: "fetch",
1686
- },
1687
- { text: "Which test runner would you like to use?", result: "vitest" }
1803
+ }
1688
1804
  );
1689
-
1805
+ mockSelect({
1806
+ text: `Would you like to create a Worker at ${path.join(
1807
+ "src",
1808
+ "index.js"
1809
+ )}?`,
1810
+ result: "fetch",
1811
+ });
1812
+ mockConfirm({
1813
+ text: "Would you like us to write your first test?",
1814
+ result: true,
1815
+ });
1816
+ mockSelect({
1817
+ text: "Which test runner would you like to use?",
1818
+ result: "vitest",
1819
+ });
1690
1820
  await runWrangler("init");
1691
1821
 
1692
1822
  checkFiles({
@@ -1733,13 +1863,19 @@ describe("init", () => {
1733
1863
  {
1734
1864
  text: "Would you like to use TypeScript?",
1735
1865
  result: false,
1736
- },
1737
- { text: "Would you like us to write your first test?", result: false }
1866
+ }
1738
1867
  );
1739
1868
  mockSelect({
1740
- text: "Would you like to create a Worker at src/index.js?",
1869
+ text: `Would you like to create a Worker at ${path.join(
1870
+ "src",
1871
+ "index.js"
1872
+ )}?`,
1741
1873
  result: "fetch",
1742
1874
  });
1875
+ mockConfirm({
1876
+ text: "Would you like us to write your first test?",
1877
+ result: false,
1878
+ });
1743
1879
  writeFiles({
1744
1880
  items: {
1745
1881
  "package.json": {
@@ -1826,7 +1962,10 @@ describe("init", () => {
1826
1962
  result: false,
1827
1963
  },
1828
1964
  {
1829
- text: "Would you like to install wrangler into my-worker/package.json?",
1965
+ text: `Would you like to install wrangler into ${path.join(
1966
+ "my-worker",
1967
+ "package.json"
1968
+ )}?`,
1830
1969
  result: false,
1831
1970
  },
1832
1971
  {
@@ -1887,24 +2026,40 @@ describe("init", () => {
1887
2026
  ...MINIMAL_WRANGLER_TOML,
1888
2027
  name: workerName,
1889
2028
  }),
2029
+ "package.json": {
2030
+ contents: expect.objectContaining({
2031
+ name: expect.stringContaining("wrangler-tests"),
2032
+ version: "0.0.0",
2033
+ scripts: {
2034
+ deploy: "wrangler publish",
2035
+ start: "wrangler dev",
2036
+ test: "vitest",
2037
+ },
2038
+ devDependencies: {
2039
+ wrangler: expect.any(String),
2040
+ },
2041
+ }),
2042
+ },
1890
2043
  },
1891
2044
  });
1892
2045
  expect(std).toMatchInlineSnapshot(`
1893
- Object {
1894
- "debug": "",
1895
- "err": "",
1896
- "out": "✨ Created wrangler.toml
1897
- ✨ Initialized git repository
1898
- ✨ Created package.json
1899
- ✨ Created tsconfig.json
1900
- ✨ Created src/index.ts
1901
- Installed @cloudflare/workers-types and typescript into devDependencies
2046
+ Object {
2047
+ "debug": "",
2048
+ "err": "",
2049
+ "out": "✨ Created wrangler.toml
2050
+ ✨ Initialized git repository
2051
+ ✨ Created package.json
2052
+ ✨ Created tsconfig.json
2053
+ ✨ Created src/index.ts
2054
+ Created src/index.test.ts
2055
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1902
2056
 
1903
- To start developing your Worker, run \`npm start\`
1904
- To publish your Worker to the Internet, run \`npm run deploy\`",
1905
- "warn": "",
1906
- }
1907
- `);
2057
+ To start developing your Worker, run \`npm start\`
2058
+ To start testing your Worker, run \`npm test\`
2059
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2060
+ "warn": "",
2061
+ }
2062
+ `);
1908
2063
  });
1909
2064
 
1910
2065
  it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
@@ -1919,21 +2074,23 @@ describe("init", () => {
1919
2074
  },
1920
2075
  });
1921
2076
  expect(std).toMatchInlineSnapshot(`
1922
- Object {
1923
- "debug": "",
1924
- "err": "",
1925
- "out": "✨ Created path/to/worker/wrangler.toml
1926
- ✨ Initialized git repository at path/to/worker
1927
- ✨ Created path/to/worker/package.json
1928
- ✨ Created path/to/worker/tsconfig.json
1929
- ✨ Created path/to/worker/src/index.ts
1930
- Installed @cloudflare/workers-types and typescript into devDependencies
1931
-
1932
- To start developing your Worker, run \`cd path/to/worker && npm start\`
1933
- To publish your Worker to the Internet, run \`npm run deploy\`",
1934
- "warn": "",
1935
- }
1936
- `);
2077
+ Object {
2078
+ "debug": "",
2079
+ "err": "",
2080
+ "out": "✨ Created path/to/worker/wrangler.toml
2081
+ ✨ Initialized git repository at path/to/worker
2082
+ ✨ Created path/to/worker/package.json
2083
+ ✨ Created path/to/worker/tsconfig.json
2084
+ ✨ Created path/to/worker/src/index.ts
2085
+ Created path/to/worker/src/index.test.ts
2086
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2087
+
2088
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
2089
+ To start testing your Worker, run \`npm test\`
2090
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2091
+ "warn": "",
2092
+ }
2093
+ `);
1937
2094
  });
1938
2095
 
1939
2096
  it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
@@ -1948,21 +2105,23 @@ describe("init", () => {
1948
2105
  },
1949
2106
  });
1950
2107
  expect(std).toMatchInlineSnapshot(`
1951
- Object {
1952
- "debug": "",
1953
- "err": "",
1954
- "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1955
- ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1956
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1957
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1958
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1959
- Installed @cloudflare/workers-types and typescript into devDependencies
1960
-
1961
- To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1962
- To publish your Worker to the Internet, run \`npm run deploy\`",
1963
- "warn": "",
1964
- }
1965
- `);
2108
+ Object {
2109
+ "debug": "",
2110
+ "err": "",
2111
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
2112
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
2113
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
2114
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
2115
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
2116
+ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.test.ts
2117
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2118
+
2119
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
2120
+ To start testing your Worker, run \`npm test\`
2121
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2122
+ "warn": "",
2123
+ }
2124
+ `);
1966
2125
  });
1967
2126
 
1968
2127
  it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
@@ -1971,23 +2130,29 @@ describe("init", () => {
1971
2130
  text: "Would you like to use git to manage this Worker?",
1972
2131
  result: false,
1973
2132
  },
1974
- {
1975
- text: "Would you like to use TypeScript?",
1976
- result: true,
1977
- },
1978
2133
  {
1979
2134
  text: "No package.json found. Would you like to create one?",
1980
2135
  result: true,
1981
2136
  },
1982
2137
  {
1983
- text: "Would you like to install the type definitions for Workers into your package.json?",
2138
+ text: "Would you like to use TypeScript?",
1984
2139
  result: true,
1985
2140
  }
1986
2141
  );
1987
2142
  mockSelect({
1988
- text: "Would you like to create a Worker at sub/folder/worker/src/index.ts?",
2143
+ text: `Would you like to create a Worker at ${path.join(
2144
+ "sub",
2145
+ "folder",
2146
+ "worker",
2147
+ "src",
2148
+ "index.ts"
2149
+ )}?`,
1989
2150
  result: "fetch",
1990
2151
  });
2152
+ mockConfirm({
2153
+ text: "Would you like us to write your first test with Vitest?",
2154
+ result: true,
2155
+ });
1991
2156
  writeFiles({
1992
2157
  items: {
1993
2158
  "package.json": { contents: { name: "top-level" } },
@@ -2035,10 +2200,7 @@ describe("init", () => {
2035
2200
  describe("from dashboard", () => {
2036
2201
  mockApiToken();
2037
2202
  mockAccountId({ accountId: "LCARS" });
2038
- afterEach(() => {
2039
- unsetAllMocks();
2040
- unsetSpecialMockFns();
2041
- });
2203
+
2042
2204
  const mockDashboardScript = `
2043
2205
  export default {
2044
2206
  async fetch(request, env, ctx) {
@@ -2114,7 +2276,7 @@ describe("init", () => {
2114
2276
  type: "service",
2115
2277
  },
2116
2278
  {
2117
- type: "namespace",
2279
+ type: "dispatch_namespace",
2118
2280
  name: "name-namespace-mock",
2119
2281
  namespace: "namespace-mock",
2120
2282
  },
@@ -2276,69 +2438,108 @@ describe("init", () => {
2276
2438
  expectedEnvironment: string;
2277
2439
  expectedCompatDate: string | undefined;
2278
2440
  }) {
2279
- setMockResponse(
2280
- `/accounts/:accountId/workers/services/:scriptName`,
2281
- "GET",
2282
- ([_url, accountId, scriptName]) => {
2283
- expect(accountId).toEqual(expectedAccountId);
2284
- expect(scriptName).toEqual(expectedScriptName);
2285
-
2286
- if (expectedCompatDate === undefined)
2287
- (mockServiceMetadata.default_environment.script
2288
- .compatibility_date as unknown) = expectedCompatDate;
2289
- return mockServiceMetadata;
2290
- }
2291
- );
2292
- setMockResponse(
2293
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2294
- "GET",
2295
- ([_url, accountId, scriptName, environment]) => {
2296
- expect(accountId).toEqual(expectedAccountId);
2297
- expect(scriptName).toEqual(expectedScriptName);
2298
- expect(environment).toEqual(expectedEnvironment);
2299
-
2300
- return mockBindingsRes;
2301
- }
2302
- );
2303
- setMockResponse(
2304
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2305
- "GET",
2306
- ([_url, accountId, scriptName, environment]) => {
2307
- expect(accountId).toEqual(expectedAccountId);
2308
- expect(scriptName).toEqual(expectedScriptName);
2309
- expect(environment).toEqual(expectedEnvironment);
2310
-
2311
- return mockRoutesRes;
2312
- }
2313
- );
2314
- setMockResponse(
2315
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2316
- "GET",
2317
- ([_url, accountId, scriptName, environment]) => {
2318
- expect(accountId).toEqual(expectedAccountId);
2319
- expect(scriptName).toEqual(expectedScriptName);
2320
- expect(environment).toEqual(expectedEnvironment);
2321
-
2322
- return mockServiceMetadata.default_environment;
2323
- }
2324
- );
2325
- setMockResponse(
2326
- `/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2327
- "GET",
2328
- ([_url, accountId, scriptName]) => {
2329
- expect(accountId).toEqual(expectedAccountId);
2330
- expect(scriptName).toEqual(expectedScriptName);
2331
-
2332
- return {
2333
- schedules: [
2334
- {
2335
- cron: "0 0 0 * * *",
2336
- created_on: new Date(1987, 9, 27),
2337
- modified_on: new Date(1987, 9, 27),
2338
- },
2339
- ],
2340
- };
2341
- }
2441
+ msw.use(
2442
+ rest.get(
2443
+ `*/accounts/:accountId/workers/services/:scriptName`,
2444
+ (req, res, ctx) => {
2445
+ expect(req.params.accountId).toEqual(expectedAccountId);
2446
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2447
+
2448
+ if (expectedCompatDate === undefined)
2449
+ (mockServiceMetadata.default_environment.script
2450
+ .compatibility_date as unknown) = expectedCompatDate;
2451
+
2452
+ return res.once(
2453
+ ctx.status(200),
2454
+ ctx.json({
2455
+ success: true,
2456
+ errors: [],
2457
+ messages: [],
2458
+ result: mockServiceMetadata,
2459
+ })
2460
+ );
2461
+ }
2462
+ ),
2463
+ rest.get(
2464
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2465
+ (req, res, ctx) => {
2466
+ expect(req.params.accountId).toEqual(expectedAccountId);
2467
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2468
+ expect(req.params.environment).toEqual(expectedEnvironment);
2469
+
2470
+ return res.once(
2471
+ ctx.status(200),
2472
+ ctx.json({
2473
+ success: true,
2474
+ errors: [],
2475
+ messages: [],
2476
+ result: mockBindingsRes,
2477
+ })
2478
+ );
2479
+ }
2480
+ ),
2481
+ rest.get(
2482
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2483
+ (req, res, ctx) => {
2484
+ expect(req.params.accountId).toEqual(expectedAccountId);
2485
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2486
+ expect(req.params.environment).toEqual(expectedEnvironment);
2487
+
2488
+ return res.once(
2489
+ ctx.status(200),
2490
+ ctx.json({
2491
+ success: true,
2492
+ errors: [],
2493
+ messages: [],
2494
+ result: mockRoutesRes,
2495
+ })
2496
+ );
2497
+ }
2498
+ ),
2499
+ rest.get(
2500
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2501
+
2502
+ (req, res, ctx) => {
2503
+ expect(req.params.accountId).toEqual(expectedAccountId);
2504
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2505
+ expect(req.params.environment).toEqual(expectedEnvironment);
2506
+
2507
+ return res.once(
2508
+ ctx.status(200),
2509
+ ctx.json({
2510
+ success: true,
2511
+ errors: [],
2512
+ messages: [],
2513
+ result: mockServiceMetadata.default_environment,
2514
+ })
2515
+ );
2516
+ }
2517
+ ),
2518
+ rest.get(
2519
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2520
+ (req, res, ctx) => {
2521
+ expect(req.params.accountId).toEqual(expectedAccountId);
2522
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2523
+
2524
+ return res.once(
2525
+ ctx.status(200),
2526
+ ctx.json({
2527
+ success: true,
2528
+ errors: [],
2529
+ messages: [],
2530
+ result: {
2531
+ schedules: [
2532
+ {
2533
+ cron: "0 0 0 * * *",
2534
+ created_on: new Date(1987, 9, 27),
2535
+ modified_on: new Date(1987, 9, 27),
2536
+ },
2537
+ ],
2538
+ },
2539
+ })
2540
+ );
2541
+ }
2542
+ )
2342
2543
  );
2343
2544
  }
2344
2545
 
@@ -2350,27 +2551,18 @@ describe("init", () => {
2350
2551
  expectedEnvironment: "test",
2351
2552
  expectedCompatDate: "1987-9-27",
2352
2553
  });
2353
- setMockFetchDashScript({
2354
- accountId: "LCARS",
2355
- fromDashScriptName: "memory-crystal",
2356
- environment: mockServiceMetadata.default_environment.environment,
2357
- mockResponse: mockDashboardScript,
2358
- });
2554
+ setMockFetchDashScript(mockDashboardScript);
2359
2555
  mockConfirm(
2360
2556
  {
2361
2557
  text: "Would you like to use git to manage this Worker?",
2362
2558
  result: false,
2363
2559
  },
2364
- {
2365
- text: "Would you like to use TypeScript?",
2366
- result: true,
2367
- },
2368
2560
  {
2369
2561
  text: "No package.json found. Would you like to create one?",
2370
2562
  result: true,
2371
2563
  },
2372
2564
  {
2373
- text: "Would you like to install the type definitions for Workers into your package.json?",
2565
+ text: "Would you like to use TypeScript?",
2374
2566
  result: true,
2375
2567
  }
2376
2568
  );
@@ -2401,32 +2593,34 @@ describe("init", () => {
2401
2593
  });
2402
2594
 
2403
2595
  it("should fail on init --from-dash on non-existent worker name", async () => {
2404
- setMockResponse(
2405
- `/accounts/:accountId/workers/services/:scriptName`,
2406
- "GET",
2407
- () => mockServiceMetadata
2596
+ msw.use(
2597
+ rest.get(
2598
+ `*/accounts/:accountId/workers/services/:scriptName`,
2599
+ (req, res, ctx) => {
2600
+ return res.once(
2601
+ ctx.status(200),
2602
+ ctx.json({
2603
+ success: true,
2604
+ errors: [],
2605
+ messages: [],
2606
+ result: mockServiceMetadata,
2607
+ })
2608
+ );
2609
+ }
2610
+ )
2408
2611
  );
2409
- setMockFetchDashScript({
2410
- accountId: "LCARS",
2411
- fromDashScriptName: "memory-crystal",
2412
- environment: mockServiceMetadata.default_environment.environment,
2413
- mockResponse: mockDashboardScript,
2414
- });
2612
+ setMockFetchDashScript(mockDashboardScript);
2415
2613
  mockConfirm(
2416
2614
  {
2417
2615
  text: "Would you like to use git to manage this Worker?",
2418
2616
  result: false,
2419
2617
  },
2420
- {
2421
- text: "Would you like to use TypeScript?",
2422
- result: true,
2423
- },
2424
2618
  {
2425
2619
  text: "No package.json found. Would you like to create one?",
2426
2620
  result: true,
2427
2621
  },
2428
2622
  {
2429
- text: "Would you like to install the type definitions for Workers into your package.json?",
2623
+ text: "Would you like to use TypeScript?",
2430
2624
  result: true,
2431
2625
  }
2432
2626
  );
@@ -2443,32 +2637,23 @@ describe("init", () => {
2443
2637
  expectedEnvironment: "test",
2444
2638
  expectedCompatDate: "1987-9-27",
2445
2639
  });
2446
- setMockFetchDashScript({
2447
- accountId: "LCARS",
2448
- fromDashScriptName: "isolinear-optical-chip",
2449
- environment: mockServiceMetadata.default_environment.environment,
2450
- mockResponse: mockDashboardScript,
2451
- });
2640
+ setMockFetchDashScript(mockDashboardScript);
2452
2641
  mockConfirm(
2453
2642
  {
2454
2643
  text: "Would you like to use git to manage this Worker?",
2455
2644
  result: false,
2456
2645
  },
2457
- {
2458
- text: "Would you like to use TypeScript?",
2459
- result: true,
2460
- },
2461
2646
  {
2462
2647
  text: "No package.json found. Would you like to create one?",
2463
2648
  result: true,
2464
2649
  },
2465
2650
  {
2466
- text: "Would you like to install the type definitions for Workers into your package.json?",
2651
+ text: "Would you like to use TypeScript?",
2467
2652
  result: true,
2468
2653
  }
2469
2654
  );
2470
2655
 
2471
- await runWrangler("init --from-dash isolinear-optical-chip");
2656
+ await runWrangler("init --from-dash isolinear-optical-chip");
2472
2657
 
2473
2658
  expect(fs.readFileSync("./isolinear-optical-chip/wrangler.toml", "utf8"))
2474
2659
  .toMatchInlineSnapshot(`
@@ -2580,28 +2765,19 @@ describe("init", () => {
2580
2765
  expectedEnvironment: "test",
2581
2766
  expectedCompatDate: "1987-9-27",
2582
2767
  });
2583
- setMockFetchDashScript({
2584
- accountId: "LCARS",
2585
- fromDashScriptName: "isolinear-optical-chip",
2586
- environment: mockServiceMetadata.default_environment.environment,
2587
- mockResponse: mockDashboardScript,
2588
- });
2768
+ setMockFetchDashScript(mockDashboardScript);
2589
2769
  mockConfirm(
2590
2770
  {
2591
2771
  text: "Would you like to use git to manage this Worker?",
2592
2772
  result: false,
2593
2773
  },
2594
- {
2595
- text: "Would you like to use TypeScript?",
2596
- result: false,
2597
- },
2598
2774
  {
2599
2775
  text: "No package.json found. Would you like to create one?",
2600
2776
  result: true,
2601
2777
  },
2602
2778
  {
2603
- text: "Would you like to install the type definitions for Workers into your package.json?",
2604
- result: true,
2779
+ text: "Would you like to use TypeScript?",
2780
+ result: false,
2605
2781
  }
2606
2782
  );
2607
2783
 
@@ -2639,27 +2815,18 @@ describe("init", () => {
2639
2815
  expectedEnvironment: "test",
2640
2816
  expectedCompatDate: undefined,
2641
2817
  });
2642
- setMockFetchDashScript({
2643
- accountId: "LCARS",
2644
- fromDashScriptName: "isolinear-optical-chip",
2645
- environment: mockServiceMetadata.default_environment.environment,
2646
- mockResponse: mockDashboardScript,
2647
- });
2818
+ setMockFetchDashScript(mockDashboardScript);
2648
2819
  mockConfirm(
2649
2820
  {
2650
2821
  text: "Would you like to use git to manage this Worker?",
2651
2822
  result: false,
2652
2823
  },
2653
- {
2654
- text: "Would you like to use TypeScript?",
2655
- result: true,
2656
- },
2657
2824
  {
2658
2825
  text: "No package.json found. Would you like to create one?",
2659
2826
  result: true,
2660
2827
  },
2661
2828
  {
2662
- text: "Would you like to install the type definitions for Workers into your package.json?",
2829
+ text: "Would you like to use TypeScript?",
2663
2830
  result: true,
2664
2831
  }
2665
2832
  );
@@ -2687,23 +2854,32 @@ describe("init", () => {
2687
2854
  });
2688
2855
 
2689
2856
  it("should throw an error to retry if a request fails", async () => {
2690
- setMockResponse(
2691
- `/accounts/:accountId/workers/services/:scriptName`,
2692
- "GET",
2693
- () => mockServiceMetadata
2694
- );
2695
- setMockResponse(
2696
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2697
- "GET",
2698
- () => Promise.reject()
2857
+ msw.use(
2858
+ rest.get(
2859
+ `*/accounts/:accountId/workers/services/:scriptName`,
2860
+ (req, res, ctx) => {
2861
+ expect(req.params.accountId).toBe("LCARS");
2862
+ expect(req.params.scriptName).toBe("isolinear-optical-chip");
2863
+ return res.once(
2864
+ ctx.status(200),
2865
+ ctx.json({
2866
+ success: true,
2867
+ errors: [],
2868
+ messages: [],
2869
+ result: mockServiceMetadata,
2870
+ })
2871
+ );
2872
+ }
2873
+ ),
2874
+ rest.get(
2875
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2876
+ (req, res) => {
2877
+ return res.networkError("Mock Network Error");
2878
+ }
2879
+ )
2699
2880
  );
2700
2881
 
2701
- setMockFetchDashScript({
2702
- accountId: "LCARS",
2703
- fromDashScriptName: "isolinear-optical-chip",
2704
- environment: mockServiceMetadata.default_environment.environment,
2705
- mockResponse: mockDashboardScript,
2706
- });
2882
+ setMockFetchDashScript(mockDashboardScript);
2707
2883
  mockConfirm(
2708
2884
  {
2709
2885
  text: "Would you like to use git to manage this Worker?",
@@ -2712,23 +2888,15 @@ describe("init", () => {
2712
2888
  {
2713
2889
  text: "Would you like to use TypeScript?",
2714
2890
  result: false,
2715
- },
2716
- {
2717
- text: "No package.json found. Would you like to create one?",
2718
- result: true,
2719
- },
2720
- {
2721
- text: "Would you like to install the type definitions for Workers into your package.json?",
2722
- result: true,
2723
2891
  }
2724
2892
  );
2725
2893
 
2726
2894
  await expect(
2727
- runWrangler("init --from-dash isolinear-optical-chip")
2895
+ runWrangler("init --from-dash isolinear-optical-chip")
2728
2896
  ).rejects.toThrowError();
2729
2897
  });
2730
2898
 
2731
- it("should not inlcude migrations in config file when none are necessary", async () => {
2899
+ it("should not include migrations in config file when none are necessary", async () => {
2732
2900
  const mockDate = "1988-08-07";
2733
2901
  jest
2734
2902
  .spyOn(Date.prototype, "toISOString")
@@ -2752,59 +2920,93 @@ describe("init", () => {
2752
2920
  },
2753
2921
  environments: [],
2754
2922
  };
2755
-
2756
- setMockResponse(
2757
- `/accounts/:accountId/workers/services/:scriptName`,
2758
- "GET",
2759
- () => mockData
2760
- );
2761
- setMockResponse(
2762
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2763
- "GET",
2764
- () => []
2765
- );
2766
- setMockResponse(
2767
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2768
- "GET",
2769
- () => []
2770
- );
2771
- setMockResponse(
2772
- `/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2773
- "GET",
2774
- () => mockServiceMetadata.default_environment
2775
- );
2776
- setMockResponse(
2777
- `/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2778
- "GET",
2779
- () => {
2780
- return {
2781
- schedules: [],
2782
- };
2783
- }
2923
+ msw.use(
2924
+ rest.get(
2925
+ `*/accounts/:accountId/workers/services/:scriptName`,
2926
+ (req, res, ctx) => {
2927
+ return res.once(
2928
+ ctx.status(200),
2929
+ ctx.json({
2930
+ success: true,
2931
+ errors: [],
2932
+ messages: [],
2933
+ result: mockData,
2934
+ })
2935
+ );
2936
+ }
2937
+ ),
2938
+ rest.get(
2939
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2940
+ (req, res, ctx) => {
2941
+ return res.once(
2942
+ ctx.status(200),
2943
+ ctx.json({
2944
+ success: true,
2945
+ errors: [],
2946
+ messages: [],
2947
+ result: [],
2948
+ })
2949
+ );
2950
+ }
2951
+ ),
2952
+ rest.get(
2953
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2954
+ (req, res, ctx) => {
2955
+ return res.once(
2956
+ ctx.status(200),
2957
+ ctx.json({
2958
+ success: true,
2959
+ errors: [],
2960
+ messages: [],
2961
+ result: [],
2962
+ })
2963
+ );
2964
+ }
2965
+ ),
2966
+ rest.get(
2967
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2968
+ (req, res, ctx) => {
2969
+ return res.once(
2970
+ ctx.status(200),
2971
+ ctx.json({
2972
+ success: true,
2973
+ errors: [],
2974
+ messages: [],
2975
+ result: mockServiceMetadata.default_environment,
2976
+ })
2977
+ );
2978
+ }
2979
+ ),
2980
+ rest.get(
2981
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2982
+ (req, res, ctx) => {
2983
+ return res.once(
2984
+ ctx.status(200),
2985
+ ctx.json({
2986
+ success: true,
2987
+ errors: [],
2988
+ messages: [],
2989
+ result: { schedules: [] },
2990
+ })
2991
+ );
2992
+ }
2993
+ )
2784
2994
  );
2785
2995
 
2786
- setMockFetchDashScript({
2787
- accountId: "LCARS",
2788
- fromDashScriptName: "isolinear-optical-chip",
2789
- environment: mockServiceMetadata.default_environment.environment,
2790
- mockResponse: mockDashboardScript,
2791
- });
2996
+ setMockFetchDashScript(mockDashboardScript);
2792
2997
 
2793
2998
  mockConfirm(
2794
2999
  {
2795
3000
  text: "Would you like to use git to manage this Worker?",
2796
3001
  result: false,
2797
3002
  },
2798
- {
2799
- text: "Would you like to use TypeScript?",
2800
- result: true,
2801
- },
3003
+
2802
3004
  {
2803
3005
  text: "No package.json found. Would you like to create one?",
2804
3006
  result: true,
2805
3007
  },
2806
3008
  {
2807
- text: "Would you like to install the type definitions for Workers into your package.json?",
3009
+ text: "Would you like to use TypeScript?",
2808
3010
  result: true,
2809
3011
  }
2810
3012
  );
@@ -2831,7 +3033,7 @@ describe("init", () => {
2831
3033
  await expect(
2832
3034
  runWrangler("init --from-dash")
2833
3035
  ).rejects.toMatchInlineSnapshot(
2834
- `[YError: Not enough arguments following: from-dash]`
3036
+ `[Error: Not enough arguments following: from-dash]`
2835
3037
  );
2836
3038
  checkFiles({
2837
3039
  items: {
@@ -2861,6 +3063,28 @@ function getDefaultBranchName() {
2861
3063
  }
2862
3064
  }
2863
3065
 
3066
+ /**
3067
+ * Mock setter for usage within test blocks for dashboard script
3068
+ */
3069
+ export function setMockFetchDashScript(mockResponse: string) {
3070
+ msw.use(
3071
+ rest.get(
3072
+ `*/accounts/:accountId/workers/services/:fromDashScriptName/environments/:environment/content`,
3073
+ (req, res, ctx) => {
3074
+ // This is a fake FormData object, until we can get MSW (beta) that supports FormData
3075
+ const mockForm = new FormData();
3076
+ mockForm.append("name", mockResponse);
3077
+ // Sending it as JSON will result in empty object, but cause the fetchDashboardScript to execute its code
3078
+ return res(ctx.status(200), ctx.json(mockForm));
3079
+ }
3080
+ )
3081
+ );
3082
+ // Mock the actual return value that FormData would return
3083
+ (fetchDashboardScript as jest.Mock).mockImplementationOnce(() => {
3084
+ return mockResponse;
3085
+ });
3086
+ }
3087
+
2864
3088
  /**
2865
3089
  * Change the current working directory, ensuring that this exists.
2866
3090
  */