wrangler 0.0.0-e178d6fb → 0.0.0-e1c2f5b9

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 (239) hide show
  1. package/README.md +16 -2
  2. package/bin/wrangler.js +31 -11
  3. package/miniflare-dist/index.mjs +1340 -330
  4. package/package.json +40 -13
  5. package/src/__tests__/access.test.ts +25 -0
  6. package/src/__tests__/api-dev.test.ts +238 -0
  7. package/src/__tests__/api-devregistry.test.js +64 -0
  8. package/src/__tests__/api.test.ts +80 -0
  9. package/src/__tests__/configuration.test.ts +683 -470
  10. package/src/__tests__/d1.test.ts +123 -0
  11. package/src/__tests__/delete.test.ts +263 -0
  12. package/src/__tests__/deployments.test.ts +105 -0
  13. package/src/__tests__/dev.test.tsx +453 -70
  14. package/src/__tests__/dialogs.test.tsx +40 -0
  15. package/src/__tests__/generate.test.ts +235 -0
  16. package/src/__tests__/get-host-from-url.test.ts +16 -0
  17. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  18. package/src/__tests__/helpers/mock-cfetch.ts +92 -26
  19. package/src/__tests__/helpers/mock-console.ts +11 -2
  20. package/src/__tests__/helpers/mock-dialogs.ts +1 -1
  21. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  22. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  23. package/src/__tests__/helpers/mock-kv.ts +1 -1
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +74 -53
  25. package/src/__tests__/helpers/mock-process.ts +8 -13
  26. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  27. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  28. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  29. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  30. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  31. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  32. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  33. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  34. package/src/__tests__/helpers/msw/index.ts +21 -0
  35. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  36. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  37. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  38. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  39. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  40. package/src/__tests__/index.test.ts +103 -97
  41. package/src/__tests__/init.test.ts +1484 -382
  42. package/src/__tests__/jest.setup.ts +88 -5
  43. package/src/__tests__/kv.test.ts +655 -540
  44. package/src/__tests__/logout.test.ts +47 -0
  45. package/src/__tests__/metrics.test.ts +95 -45
  46. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  47. package/src/__tests__/middleware.test.ts +750 -0
  48. package/src/__tests__/pages-deployment-tail.test.ts +884 -0
  49. package/src/__tests__/pages.test.ts +1301 -119
  50. package/src/__tests__/paths.test.ts +39 -0
  51. package/src/__tests__/publish.test.ts +2266 -1180
  52. package/src/__tests__/pubsub.test.ts +211 -88
  53. package/src/__tests__/queues.test.ts +522 -0
  54. package/src/__tests__/r2.test.ts +363 -203
  55. package/src/__tests__/secret.test.ts +261 -59
  56. package/src/__tests__/tail.test.ts +352 -151
  57. package/src/__tests__/test-old-node-version.js +3 -3
  58. package/src/__tests__/tsconfig-sanity.ts +12 -0
  59. package/src/__tests__/tsconfig.json +8 -0
  60. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  61. package/src/__tests__/type-generation.test.ts +233 -0
  62. package/src/__tests__/user.test.ts +59 -73
  63. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  64. package/src/__tests__/validate-dev-props.test.ts +56 -0
  65. package/src/__tests__/whoami.test.tsx +114 -66
  66. package/src/__tests__/worker-namespace.test.ts +154 -155
  67. package/src/api/dev.ts +170 -38
  68. package/src/api/index.ts +1 -0
  69. package/src/bundle-reporter.tsx +41 -2
  70. package/src/bundle.ts +668 -81
  71. package/src/cfetch/index.ts +40 -3
  72. package/src/cfetch/internal.ts +144 -32
  73. package/src/cli.ts +4 -3
  74. package/src/config/config.ts +27 -3
  75. package/src/config/environment.ts +131 -7
  76. package/src/config/index.ts +128 -8
  77. package/src/config/validation-helpers.ts +19 -6
  78. package/src/config/validation.ts +411 -14
  79. package/src/config-cache.ts +3 -2
  80. package/src/create-worker-preview.ts +31 -6
  81. package/src/create-worker-upload-form.ts +84 -22
  82. package/src/d1/backups.tsx +223 -0
  83. package/src/d1/constants.ts +2 -0
  84. package/src/d1/create.tsx +63 -0
  85. package/src/d1/delete.tsx +56 -0
  86. package/src/d1/execute.tsx +352 -0
  87. package/src/d1/formatTimeAgo.ts +14 -0
  88. package/src/d1/index.ts +101 -0
  89. package/src/d1/list.tsx +48 -0
  90. package/src/d1/migrations/apply.tsx +196 -0
  91. package/src/d1/migrations/create.tsx +77 -0
  92. package/src/d1/migrations/helpers.ts +147 -0
  93. package/src/d1/migrations/index.tsx +3 -0
  94. package/src/d1/migrations/list.tsx +79 -0
  95. package/src/d1/migrations/options.ts +18 -0
  96. package/src/d1/options.ts +22 -0
  97. package/src/d1/types.tsx +24 -0
  98. package/src/d1/utils.ts +48 -0
  99. package/src/delete.ts +108 -0
  100. package/src/deployments.ts +83 -0
  101. package/src/deprecated/index.ts +145 -0
  102. package/src/dev/dev-vars.ts +11 -8
  103. package/src/dev/dev.tsx +220 -58
  104. package/src/dev/get-local-persistence-path.tsx +31 -0
  105. package/src/dev/local.tsx +740 -181
  106. package/src/dev/remote.tsx +340 -123
  107. package/src/dev/start-server.ts +543 -0
  108. package/src/dev/use-esbuild.ts +61 -7
  109. package/src/dev/validate-dev-props.ts +31 -0
  110. package/src/dev-registry.tsx +201 -0
  111. package/src/dev.tsx +614 -258
  112. package/src/dialogs.tsx +41 -8
  113. package/src/{worker-namespace.ts → dispatch-namespace.ts} +23 -22
  114. package/src/docs/index.ts +97 -0
  115. package/src/entry.ts +3 -2
  116. package/src/environment-variables/factory.ts +88 -0
  117. package/src/environment-variables/misc-variables.ts +30 -0
  118. package/src/generate/index.ts +300 -0
  119. package/src/git-client.ts +100 -7
  120. package/src/index.tsx +249 -1560
  121. package/src/init.ts +626 -150
  122. package/src/inspect.ts +137 -32
  123. package/src/{metrics/is-ci.ts → is-ci.ts} +0 -0
  124. package/src/is-interactive.ts +4 -0
  125. package/src/jest.d.ts +4 -0
  126. package/src/{kv.ts → kv/helpers.ts} +3 -3
  127. package/src/kv/index.ts +596 -0
  128. package/src/logger.ts +27 -8
  129. package/src/metrics/index.ts +1 -0
  130. package/src/metrics/metrics-config.ts +4 -4
  131. package/src/metrics/metrics-dispatcher.ts +1 -0
  132. package/src/metrics/metrics-usage-headers.ts +24 -0
  133. package/src/metrics/send-event.ts +13 -8
  134. package/src/miniflare-cli/assets.ts +172 -465
  135. package/src/miniflare-cli/index.ts +149 -24
  136. package/src/miniflare-cli/tsconfig.json +9 -0
  137. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  138. package/src/miniflare-cli/types.ts +11 -0
  139. package/src/module-collection.ts +12 -3
  140. package/src/pages/build.tsx +108 -42
  141. package/src/pages/constants.ts +11 -0
  142. package/src/pages/deployment-tails.tsx +284 -0
  143. package/src/pages/deployments.tsx +9 -31
  144. package/src/pages/dev.tsx +417 -108
  145. package/src/pages/errors.ts +66 -0
  146. package/src/pages/functions/buildPlugin.ts +86 -84
  147. package/src/pages/functions/buildWorker.ts +121 -106
  148. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  149. package/src/pages/functions/routes-consolidation.ts +73 -0
  150. package/src/pages/functions/routes-transformation.test.ts +282 -0
  151. package/src/pages/functions/routes-transformation.ts +115 -0
  152. package/src/pages/functions/routes-validation.test.ts +403 -0
  153. package/src/pages/functions/routes-validation.ts +202 -0
  154. package/src/pages/functions/tsconfig.json +8 -0
  155. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  156. package/src/pages/functions.tsx +82 -0
  157. package/src/pages/hash.tsx +13 -0
  158. package/src/pages/index.tsx +73 -55
  159. package/src/pages/projects.tsx +9 -3
  160. package/src/pages/prompt-select-project.tsx +31 -0
  161. package/src/pages/publish.tsx +183 -60
  162. package/src/pages/types.ts +1 -0
  163. package/src/pages/upload.tsx +107 -56
  164. package/src/pages/utils.ts +13 -2
  165. package/src/paths.ts +49 -4
  166. package/src/proxy.ts +375 -157
  167. package/src/publish/index.ts +270 -0
  168. package/src/{publish.ts → publish/publish.ts} +431 -188
  169. package/src/pubsub/pubsub-commands.tsx +4 -3
  170. package/src/queues/cli/commands/consumer/add.ts +71 -0
  171. package/src/queues/cli/commands/consumer/index.ts +22 -0
  172. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  173. package/src/queues/cli/commands/create.ts +25 -0
  174. package/src/queues/cli/commands/delete.ts +26 -0
  175. package/src/queues/cli/commands/index.ts +36 -0
  176. package/src/queues/cli/commands/list.ts +25 -0
  177. package/src/queues/client.ts +135 -0
  178. package/src/queues/utils.ts +18 -0
  179. package/src/r2/constants.ts +4 -0
  180. package/src/r2/helpers.ts +131 -0
  181. package/src/r2/index.ts +289 -0
  182. package/src/routes.ts +140 -0
  183. package/src/secret/index.ts +379 -0
  184. package/src/sites.tsx +3 -3
  185. package/src/tail/createTail.ts +379 -0
  186. package/src/tail/filters.ts +4 -6
  187. package/src/tail/index.ts +199 -284
  188. package/src/tail/printing.ts +43 -3
  189. package/src/tsconfig-sanity.ts +16 -0
  190. package/src/type-generation.ts +181 -0
  191. package/src/user/access.ts +68 -0
  192. package/src/user/auth-variables.ts +113 -0
  193. package/src/user/choose-account.tsx +1 -1
  194. package/src/user/index.ts +0 -1
  195. package/src/user/user.tsx +116 -49
  196. package/src/utils/collectKeyValues.ts +14 -0
  197. package/src/whoami.tsx +80 -2
  198. package/src/worker.ts +69 -9
  199. package/src/yargs-types.ts +24 -0
  200. package/src/zones.ts +124 -9
  201. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  202. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  203. package/templates/__tests__/tsconfig.json +8 -0
  204. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  205. package/templates/d1-beta-facade.js +205 -0
  206. package/templates/facade.d.ts +14 -0
  207. package/templates/first-party-worker-module-facade.ts +19 -0
  208. package/templates/format-dev-errors.ts +33 -0
  209. package/templates/init-tests/test-jest-new-worker.js +23 -0
  210. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  211. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  212. package/templates/middleware/common.ts +62 -0
  213. package/templates/middleware/loader-modules.ts +82 -0
  214. package/templates/middleware/loader-sw.ts +233 -0
  215. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  216. package/templates/middleware/middleware-pretty-error.ts +40 -0
  217. package/templates/middleware/middleware-scheduled.ts +15 -0
  218. package/templates/new-worker.js +1 -1
  219. package/templates/pages-dev-pipeline.ts +31 -0
  220. package/templates/pages-dev-util.ts +52 -0
  221. package/templates/pages-shim.ts +4 -1
  222. package/templates/pages-template-plugin.ts +21 -5
  223. package/templates/pages-template-worker.ts +37 -9
  224. package/templates/{static-asset-facade.js → serve-static-assets.ts} +23 -7
  225. package/templates/service-bindings-module-facade.js +54 -0
  226. package/templates/service-bindings-sw-facade.js +42 -0
  227. package/templates/tsconfig-sanity.ts +11 -0
  228. package/templates/tsconfig.init.json +106 -0
  229. package/templates/tsconfig.json +5 -102
  230. package/templates/tsconfig.tsbuildinfo +1 -0
  231. package/wrangler-dist/cli.d.ts +621 -42
  232. package/wrangler-dist/cli.js +144441 -102606
  233. package/src/environment-variables.ts +0 -35
  234. package/src/faye-websocket.d.ts +0 -6
  235. package/src/generate.ts +0 -33
  236. package/src/miniflare-cli/enum-keys.ts +0 -17
  237. package/src/preview.ts +0 -31
  238. package/src/r2.ts +0 -50
  239. package/src/user/env-vars.ts +0 -46
@@ -2,16 +2,16 @@ 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";
11
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
8
12
  import { mockConsoleMethods } from "./helpers/mock-console";
9
- import {
10
- mockConfirm,
11
- clearConfirmMocks,
12
- mockSelect,
13
- clearSelectMocks,
14
- } from "./helpers/mock-dialogs";
13
+ import { mockConfirm, mockSelect } from "./helpers/mock-dialogs";
14
+ import { msw } from "./helpers/msw";
15
15
  import { runInTempDir } from "./helpers/run-in-tmp";
16
16
  import { runWrangler } from "./helpers/run-wrangler";
17
17
  import type { PackageManager } from "../package-manager";
@@ -41,8 +41,8 @@ describe("init", () => {
41
41
  });
42
42
 
43
43
  afterEach(() => {
44
- clearConfirmMocks();
45
- clearSelectMocks();
44
+ msw.resetHandlers();
45
+ msw.restoreHandlers();
46
46
  });
47
47
 
48
48
  const std = mockConsoleMethods();
@@ -62,16 +62,17 @@ describe("init", () => {
62
62
  });
63
63
 
64
64
  expect(std.out).toMatchInlineSnapshot(`
65
- "✨ Created wrangler.toml
66
- ✨ Initialized git repository
67
- ✨ Created package.json
68
- ✨ Created tsconfig.json
69
- ✨ Created src/index.ts
70
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
71
-
72
- To start developing your Worker, run \`npm start\`
73
- To publish your Worker to the Internet, run \`npm run deploy\`"
74
- `);
65
+ "✨ Created wrangler.toml
66
+ ✨ Initialized git repository
67
+ ✨ Created package.json
68
+ ✨ Created tsconfig.json
69
+ ✨ Created src/index.ts
70
+ ✨ Created src/index.test.ts
71
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
72
+
73
+ To start developing your Worker, run \`npm start\`
74
+ To publish your Worker to the Internet, run \`npm run deploy\`"
75
+ `);
75
76
  expect(std.err).toMatchInlineSnapshot(`""`);
76
77
  expect(std.warn).toMatchInlineSnapshot(`""`);
77
78
  });
@@ -93,16 +94,17 @@ describe("init", () => {
93
94
  });
94
95
 
95
96
  expect(std.out).toMatchInlineSnapshot(`
96
- "✨ Created my-worker/wrangler.toml
97
- ✨ Initialized git repository at my-worker
98
- ✨ Created my-worker/package.json
99
- ✨ Created my-worker/tsconfig.json
100
- ✨ Created my-worker/src/index.ts
101
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
102
-
103
- To start developing your Worker, run \`cd my-worker && npm start\`
104
- To publish your Worker to the Internet, run \`npm run deploy\`"
105
- `);
97
+ "✨ Created my-worker/wrangler.toml
98
+ ✨ Initialized git repository at my-worker
99
+ ✨ Created my-worker/package.json
100
+ ✨ Created my-worker/tsconfig.json
101
+ ✨ Created my-worker/src/index.ts
102
+ ✨ Created my-worker/src/index.test.ts
103
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
104
+
105
+ To start developing your Worker, run \`cd my-worker && npm start\`
106
+ To publish your Worker to the Internet, run \`npm run deploy\`"
107
+ `);
106
108
  expect(std.err).toMatchInlineSnapshot(`""`);
107
109
  expect(std.warn).toMatchInlineSnapshot(`""`);
108
110
  });
@@ -121,21 +123,22 @@ describe("init", () => {
121
123
  });
122
124
 
123
125
  expect(std).toMatchInlineSnapshot(`
124
- Object {
125
- "debug": "",
126
- "err": "",
127
- "out": "✨ Created wrangler.toml
128
- ✨ Initialized git repository
129
- ✨ Created package.json
130
- ✨ Created tsconfig.json
131
- ✨ Created src/index.ts
132
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
133
-
134
- To start developing your Worker, run \`npm start\`
135
- To publish your Worker to the Internet, run \`npm run deploy\`",
136
- "warn": "",
137
- }
138
- `);
126
+ Object {
127
+ "debug": "",
128
+ "err": "",
129
+ "out": "✨ Created wrangler.toml
130
+ ✨ Initialized git repository
131
+ ✨ Created package.json
132
+ ✨ Created tsconfig.json
133
+ ✨ Created src/index.ts
134
+ ✨ Created src/index.test.ts
135
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
136
+
137
+ To start developing your Worker, run \`npm start\`
138
+ To publish your Worker to the Internet, run \`npm run deploy\`",
139
+ "warn": "",
140
+ }
141
+ `);
139
142
  });
140
143
 
141
144
  it("should error if `--type javascript` is used", async () => {
@@ -204,13 +207,13 @@ describe("init", () => {
204
207
  });
205
208
 
206
209
  expect(std).toMatchInlineSnapshot(`
207
- Object {
208
- "debug": "",
209
- "err": "",
210
- "out": "✨ Created wrangler.toml",
211
- "warn": "",
212
- }
213
- `);
210
+ Object {
211
+ "debug": "",
212
+ "err": "",
213
+ "out": "✨ Created wrangler.toml",
214
+ "warn": "",
215
+ }
216
+ `);
214
217
  });
215
218
 
216
219
  it("should create a wrangler.toml and a directory for a named Worker ", async () => {
@@ -238,13 +241,13 @@ describe("init", () => {
238
241
  });
239
242
 
240
243
  expect(std).toMatchInlineSnapshot(`
241
- Object {
242
- "debug": "",
243
- "err": "",
244
- "out": "✨ Created my-worker/wrangler.toml",
245
- "warn": "",
246
- }
247
- `);
244
+ Object {
245
+ "debug": "",
246
+ "err": "",
247
+ "out": "✨ Created my-worker/wrangler.toml",
248
+ "warn": "",
249
+ }
250
+ `);
248
251
  });
249
252
 
250
253
  it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
@@ -276,15 +279,15 @@ describe("init", () => {
276
279
  });
277
280
 
278
281
  expect(std).toMatchInlineSnapshot(`
279
- Object {
280
- "debug": "",
281
- "err": "",
282
- "out": "",
283
- "warn": "▲ [WARNING] wrangler.toml already exists!
284
-
285
- ",
286
- }
287
- `);
282
+ Object {
283
+ "debug": "",
284
+ "err": "",
285
+ "out": "",
286
+ "warn": "▲ [WARNING] wrangler.toml already exists!
287
+
288
+ ",
289
+ }
290
+ `);
288
291
  });
289
292
 
290
293
  it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
@@ -313,15 +316,15 @@ describe("init", () => {
313
316
  },
314
317
  });
315
318
  expect(std).toMatchInlineSnapshot(`
316
- Object {
317
- "debug": "",
318
- "err": "",
319
- "out": "",
320
- "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
321
-
322
- ",
323
- }
324
- `);
319
+ Object {
320
+ "debug": "",
321
+ "err": "",
322
+ "out": "",
323
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
324
+
325
+ ",
326
+ }
327
+ `);
325
328
  });
326
329
 
327
330
  it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
@@ -348,6 +351,10 @@ describe("init", () => {
348
351
  {
349
352
  text: "Would you like to use TypeScript?",
350
353
  result: true,
354
+ },
355
+ {
356
+ text: "Would you like us to write your first test with Vitest?",
357
+ result: true,
351
358
  }
352
359
  );
353
360
 
@@ -401,15 +408,15 @@ describe("init", () => {
401
408
  },
402
409
  });
403
410
  expect(std).toMatchInlineSnapshot(`
404
- Object {
405
- "debug": "",
406
- "err": "",
407
- "out": "",
408
- "warn": "▲ [WARNING] wrangler.toml already exists!
409
-
410
- ",
411
- }
412
- `);
411
+ Object {
412
+ "debug": "",
413
+ "err": "",
414
+ "out": "",
415
+ "warn": "▲ [WARNING] wrangler.toml already exists!
416
+
417
+ ",
418
+ }
419
+ `);
413
420
  });
414
421
 
415
422
  it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
@@ -436,6 +443,10 @@ describe("init", () => {
436
443
  {
437
444
  text: "Would you like to use TypeScript?",
438
445
  result: true,
446
+ },
447
+ {
448
+ text: "Would you like us to write your first test with Vitest?",
449
+ result: true,
439
450
  }
440
451
  );
441
452
  mockSelect({
@@ -467,6 +478,10 @@ describe("init", () => {
467
478
  {
468
479
  text: "Would you like to use TypeScript?",
469
480
  result: true,
481
+ },
482
+ {
483
+ text: "Would you like us to write your first test with Vitest?",
484
+ result: true,
470
485
  }
471
486
  );
472
487
  mockSelect({
@@ -513,16 +528,16 @@ describe("init", () => {
513
528
  },
514
529
  });
515
530
  expect(std).toMatchInlineSnapshot(`
516
- Object {
517
- "debug": "",
518
- "err": "",
519
- "out": "✨ Created wrangler.toml
520
- ✨ Initialized git repository",
521
- "warn": "",
522
- }
523
- `);
531
+ Object {
532
+ "debug": "",
533
+ "err": "",
534
+ "out": "✨ Created wrangler.toml
535
+ ✨ Initialized git repository",
536
+ "warn": "",
537
+ }
538
+ `);
524
539
  expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
525
- "main"
540
+ getDefaultBranchName()
526
541
  );
527
542
  });
528
543
 
@@ -545,20 +560,21 @@ describe("init", () => {
545
560
 
546
561
  // Note the lack of "✨ Initialized git repository" in the log
547
562
  expect(std).toMatchInlineSnapshot(`
548
- Object {
549
- "debug": "",
550
- "err": "",
551
- "out": "✨ Created wrangler.toml
552
- ✨ Created package.json
553
- ✨ Created tsconfig.json
554
- ✨ Created src/index.ts
555
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
556
-
557
- To start developing your Worker, run \`npm start\`
558
- To publish your Worker to the Internet, run \`npm run deploy\`",
559
- "warn": "",
560
- }
561
- `);
563
+ Object {
564
+ "debug": "",
565
+ "err": "",
566
+ "out": "✨ Created wrangler.toml
567
+ ✨ Created package.json
568
+ ✨ Created tsconfig.json
569
+ ✨ Created src/index.ts
570
+ ✨ Created src/index.test.ts
571
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
572
+
573
+ To start developing your Worker, run \`npm start\`
574
+ To publish your Worker to the Internet, run \`npm run deploy\`",
575
+ "warn": "",
576
+ }
577
+ `);
562
578
  });
563
579
 
564
580
  it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
@@ -570,20 +586,21 @@ describe("init", () => {
570
586
 
571
587
  // Note the lack of "✨ Initialized git repository" in the log
572
588
  expect(std).toMatchInlineSnapshot(`
573
- Object {
574
- "debug": "",
575
- "err": "",
576
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
577
- ✨ Created path/to/worker/my-worker/package.json
578
- ✨ Created path/to/worker/my-worker/tsconfig.json
579
- ✨ Created path/to/worker/my-worker/src/index.ts
580
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
581
-
582
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
583
- To publish your Worker to the Internet, run \`npm run deploy\`",
584
- "warn": "",
585
- }
586
- `);
589
+ Object {
590
+ "debug": "",
591
+ "err": "",
592
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
593
+ ✨ Created path/to/worker/my-worker/package.json
594
+ ✨ Created path/to/worker/my-worker/tsconfig.json
595
+ ✨ Created path/to/worker/my-worker/src/index.ts
596
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
597
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
598
+
599
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
600
+ To publish your Worker to the Internet, run \`npm run deploy\`",
601
+ "warn": "",
602
+ }
603
+ `);
587
604
  });
588
605
 
589
606
  // I... don't know how to test this lol
@@ -591,7 +608,7 @@ describe("init", () => {
591
608
  "should not offer to initialize a git repo if git is not installed"
592
609
  );
593
610
 
594
- it("should initialize git repo with `main` default branch", async () => {
611
+ it("should initialize git repo with the user's default branch", async () => {
595
612
  mockConfirm(
596
613
  {
597
614
  text: "Would you like to use git to manage this Worker?",
@@ -604,17 +621,17 @@ describe("init", () => {
604
621
  );
605
622
  await runWrangler("init");
606
623
  expect(std).toMatchInlineSnapshot(`
607
- Object {
608
- "debug": "",
609
- "err": "",
610
- "out": "✨ Created wrangler.toml
611
- ✨ Initialized git repository",
612
- "warn": "",
613
- }
614
- `);
624
+ Object {
625
+ "debug": "",
626
+ "err": "",
627
+ "out": "✨ Created wrangler.toml
628
+ ✨ Initialized git repository",
629
+ "warn": "",
630
+ }
631
+ `);
615
632
 
616
633
  expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
617
- "refs/heads/main"
634
+ `refs/heads/${getDefaultBranchName()}`
618
635
  );
619
636
  });
620
637
  });
@@ -658,14 +675,14 @@ describe("init", () => {
658
675
  });
659
676
  expect(mockPackageManager.install).toHaveBeenCalled();
660
677
  expect(std).toMatchInlineSnapshot(`
661
- Object {
662
- "debug": "",
663
- "err": "",
664
- "out": "✨ Created wrangler.toml
665
- ✨ Created package.json",
666
- "warn": "",
667
- }
668
- `);
678
+ Object {
679
+ "debug": "",
680
+ "err": "",
681
+ "out": "✨ Created wrangler.toml
682
+ ✨ Created package.json",
683
+ "warn": "",
684
+ }
685
+ `);
669
686
  });
670
687
 
671
688
  it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
@@ -705,14 +722,14 @@ describe("init", () => {
705
722
  },
706
723
  });
707
724
  expect(std).toMatchInlineSnapshot(`
708
- Object {
709
- "debug": "",
710
- "err": "",
711
- "out": "✨ Created my-worker/wrangler.toml
712
- ✨ Created my-worker/package.json",
713
- "warn": "",
714
- }
715
- `);
725
+ Object {
726
+ "debug": "",
727
+ "err": "",
728
+ "out": "✨ Created my-worker/wrangler.toml
729
+ ✨ Created my-worker/package.json",
730
+ "warn": "",
731
+ }
732
+ `);
716
733
  });
717
734
 
718
735
  it("should not touch an existing package.json in the same directory", async () => {
@@ -748,13 +765,13 @@ describe("init", () => {
748
765
  },
749
766
  });
750
767
  expect(std).toMatchInlineSnapshot(`
751
- Object {
752
- "debug": "",
753
- "err": "",
754
- "out": "✨ Created wrangler.toml",
755
- "warn": "",
756
- }
757
- `);
768
+ Object {
769
+ "debug": "",
770
+ "err": "",
771
+ "out": "✨ Created wrangler.toml",
772
+ "warn": "",
773
+ }
774
+ `);
758
775
  });
759
776
 
760
777
  it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
@@ -798,14 +815,14 @@ describe("init", () => {
798
815
  },
799
816
  });
800
817
  expect(std).toMatchInlineSnapshot(`
801
- Object {
802
- "debug": "",
803
- "err": "",
804
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
805
- ✨ Created path/to/worker/my-worker/package.json",
806
- "warn": "",
807
- }
808
- `);
818
+ Object {
819
+ "debug": "",
820
+ "err": "",
821
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
822
+ ✨ Created path/to/worker/my-worker/package.json",
823
+ "warn": "",
824
+ }
825
+ `);
809
826
  });
810
827
 
811
828
  it("should offer to install wrangler into an existing package.json", async () => {
@@ -848,14 +865,14 @@ describe("init", () => {
848
865
  `wrangler@${wranglerVersion}`
849
866
  );
850
867
  expect(std).toMatchInlineSnapshot(`
851
- Object {
852
- "debug": "",
853
- "err": "",
854
- "out": "✨ Created wrangler.toml
855
- ✨ Installed wrangler into devDependencies",
856
- "warn": "",
857
- }
858
- `);
868
+ Object {
869
+ "debug": "",
870
+ "err": "",
871
+ "out": "✨ Created wrangler.toml
872
+ ✨ Installed wrangler into devDependencies",
873
+ "warn": "",
874
+ }
875
+ `);
859
876
  });
860
877
 
861
878
  it("should offer to install wrangler into a package.json relative to the target directory, if no name is provided", async () => {
@@ -902,14 +919,14 @@ describe("init", () => {
902
919
  );
903
920
  expect(mockPackageManager.cwd).toBe(process.cwd());
904
921
  expect(std).toMatchInlineSnapshot(`
905
- Object {
906
- "debug": "",
907
- "err": "",
908
- "out": "✨ Created wrangler.toml
909
- ✨ Installed wrangler into devDependencies",
910
- "warn": "",
911
- }
912
- `);
922
+ Object {
923
+ "debug": "",
924
+ "err": "",
925
+ "out": "✨ Created wrangler.toml
926
+ ✨ Installed wrangler into devDependencies",
927
+ "warn": "",
928
+ }
929
+ `);
913
930
  });
914
931
 
915
932
  it("should not touch an existing package.json in an ancestor directory", async () => {
@@ -951,13 +968,13 @@ describe("init", () => {
951
968
  },
952
969
  });
953
970
  expect(std).toMatchInlineSnapshot(`
954
- Object {
955
- "debug": "",
956
- "err": "",
957
- "out": "✨ Created wrangler.toml",
958
- "warn": "",
959
- }
960
- `);
971
+ Object {
972
+ "debug": "",
973
+ "err": "",
974
+ "out": "✨ Created wrangler.toml",
975
+ "warn": "",
976
+ }
977
+ `);
961
978
  });
962
979
  });
963
980
 
@@ -975,7 +992,8 @@ describe("init", () => {
975
992
  {
976
993
  text: "Would you like to use TypeScript?",
977
994
  result: false,
978
- }
995
+ },
996
+ { text: "Would you like us to write your first test?", result: false }
979
997
  );
980
998
  mockSelect({
981
999
  text: "Would you like to create a Worker at src/index.js?",
@@ -1002,17 +1020,17 @@ describe("init", () => {
1002
1020
  },
1003
1021
  });
1004
1022
  expect(std).toMatchInlineSnapshot(`
1005
- Object {
1006
- "debug": "",
1007
- "err": "",
1008
- "out": "✨ Created wrangler.toml
1009
- ✨ Created src/index.js
1010
-
1011
- To start developing your Worker, run \`npx wrangler dev\`
1012
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1013
- "warn": "",
1014
- }
1015
- `);
1023
+ Object {
1024
+ "debug": "",
1025
+ "err": "",
1026
+ "out": "✨ Created wrangler.toml
1027
+ ✨ Created src/index.js
1028
+
1029
+ To start developing your Worker, run \`npx wrangler dev\`
1030
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1031
+ "warn": "",
1032
+ }
1033
+ `);
1016
1034
  });
1017
1035
 
1018
1036
  it("should offer to create a worker in a typescript project", async () => {
@@ -1028,6 +1046,10 @@ describe("init", () => {
1028
1046
  {
1029
1047
  text: "Would you like to use TypeScript?",
1030
1048
  result: true,
1049
+ },
1050
+ {
1051
+ text: "Would you like us to write your first test with Vitest?",
1052
+ result: true,
1031
1053
  }
1032
1054
  );
1033
1055
  mockSelect({
@@ -1051,19 +1073,20 @@ describe("init", () => {
1051
1073
  },
1052
1074
  });
1053
1075
  expect(std).toMatchInlineSnapshot(`
1054
- Object {
1055
- "debug": "",
1056
- "err": "",
1057
- "out": "✨ Created wrangler.toml
1058
- ✨ Created tsconfig.json
1059
- ✨ Created src/index.ts
1060
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1061
-
1062
- To start developing your Worker, run \`npx wrangler dev\`
1063
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1064
- "warn": "",
1065
- }
1066
- `);
1076
+ Object {
1077
+ "debug": "",
1078
+ "err": "",
1079
+ "out": "✨ Created wrangler.toml
1080
+ ✨ Created tsconfig.json
1081
+ ✨ Created src/index.ts
1082
+ ✨ Created src/index.test.ts
1083
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1084
+
1085
+ To start developing your Worker, run \`npx wrangler dev\`
1086
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1087
+ "warn": "",
1088
+ }
1089
+ `);
1067
1090
  });
1068
1091
 
1069
1092
  it("should add scripts for a typescript project with .ts extension", async () => {
@@ -1083,6 +1106,10 @@ describe("init", () => {
1083
1106
  {
1084
1107
  text: "Would you like to use TypeScript?",
1085
1108
  result: true,
1109
+ },
1110
+ {
1111
+ text: "Would you like us to write your first test with Vitest?",
1112
+ result: true,
1086
1113
  }
1087
1114
  );
1088
1115
  mockSelect({
@@ -1109,15 +1136,16 @@ describe("init", () => {
1109
1136
  },
1110
1137
  });
1111
1138
  expect(std.out).toMatchInlineSnapshot(`
1112
- "✨ Created wrangler.toml
1113
- ✨ Created package.json
1114
- ✨ Created tsconfig.json
1115
- ✨ Created src/index.ts
1116
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1117
-
1118
- To start developing your Worker, run \`npm start\`
1119
- To publish your Worker to the Internet, run \`npm run deploy\`"
1120
- `);
1139
+ "✨ Created wrangler.toml
1140
+ ✨ Created package.json
1141
+ ✨ Created tsconfig.json
1142
+ ✨ Created src/index.ts
1143
+ ✨ Created src/index.test.ts
1144
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1145
+
1146
+ To start developing your Worker, run \`npm start\`
1147
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1148
+ `);
1121
1149
  });
1122
1150
 
1123
1151
  it("should not overwrite package.json scripts for a typescript project", async () => {
@@ -1133,6 +1161,10 @@ describe("init", () => {
1133
1161
  {
1134
1162
  text: "Would you like to use TypeScript?",
1135
1163
  result: true,
1164
+ },
1165
+ {
1166
+ text: "Would you like us to write your first test with Vitest?",
1167
+ result: true,
1136
1168
  }
1137
1169
  );
1138
1170
  mockSelect({
@@ -1169,14 +1201,15 @@ describe("init", () => {
1169
1201
  },
1170
1202
  });
1171
1203
  expect(std.out).toMatchInlineSnapshot(`
1172
- "✨ Created wrangler.toml
1173
- ✨ Created tsconfig.json
1174
- ✨ Created src/index.ts
1175
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1176
-
1177
- To start developing your Worker, run \`npx wrangler dev\`
1178
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1179
- `);
1204
+ "✨ Created wrangler.toml
1205
+ ✨ Created tsconfig.json
1206
+ ✨ Created src/index.ts
1207
+ ✨ Created src/index.test.ts
1208
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1209
+
1210
+ To start developing your Worker, run \`npx wrangler dev\`
1211
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1212
+ `);
1180
1213
  });
1181
1214
 
1182
1215
  it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
@@ -1211,15 +1244,15 @@ describe("init", () => {
1211
1244
  },
1212
1245
  });
1213
1246
  expect(std).toMatchInlineSnapshot(`
1214
- Object {
1215
- "debug": "",
1216
- "err": "",
1217
- "out": "✨ Created wrangler.toml
1218
- ✨ Created tsconfig.json
1219
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1220
- "warn": "",
1221
- }
1222
- `);
1247
+ Object {
1248
+ "debug": "",
1249
+ "err": "",
1250
+ "out": "✨ Created wrangler.toml
1251
+ ✨ Created tsconfig.json
1252
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1253
+ "warn": "",
1254
+ }
1255
+ `);
1223
1256
  });
1224
1257
 
1225
1258
  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 () => {
@@ -1258,16 +1291,16 @@ describe("init", () => {
1258
1291
  },
1259
1292
  });
1260
1293
  expect(std).toMatchInlineSnapshot(`
1261
- Object {
1262
- "debug": "",
1263
- "err": "",
1264
- "out": "✨ Created my-worker/wrangler.toml
1265
- ✨ Created my-worker/package.json
1266
- ✨ Created my-worker/tsconfig.json
1267
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1268
- "warn": "",
1269
- }
1270
- `);
1294
+ Object {
1295
+ "debug": "",
1296
+ "err": "",
1297
+ "out": "✨ Created my-worker/wrangler.toml
1298
+ ✨ Created my-worker/package.json
1299
+ ✨ Created my-worker/tsconfig.json
1300
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1301
+ "warn": "",
1302
+ }
1303
+ `);
1271
1304
  });
1272
1305
 
1273
1306
  it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
@@ -1298,7 +1331,7 @@ describe("init", () => {
1298
1331
  contents: {
1299
1332
  config: {
1300
1333
  compilerOptions: expect.objectContaining({
1301
- types: ["@cloudflare/workers-types"],
1334
+ types: ["@cloudflare/workers-types", "jest"],
1302
1335
  }),
1303
1336
  },
1304
1337
  error: undefined,
@@ -1311,23 +1344,29 @@ describe("init", () => {
1311
1344
  "typescript"
1312
1345
  );
1313
1346
  expect(std).toMatchInlineSnapshot(`
1314
- Object {
1315
- "debug": "",
1316
- "err": "",
1317
- "out": "✨ Created wrangler.toml
1318
- ✨ Created package.json
1319
- ✨ Created tsconfig.json
1320
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1321
- "warn": "",
1322
- }
1323
- `);
1347
+ Object {
1348
+ "debug": "",
1349
+ "err": "",
1350
+ "out": "✨ Created wrangler.toml
1351
+ ✨ Created package.json
1352
+ ✨ Created tsconfig.json
1353
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1354
+ "warn": "",
1355
+ }
1356
+ `);
1324
1357
  });
1325
1358
 
1326
1359
  it("should not touch an existing tsconfig.json in the same directory", async () => {
1327
- mockConfirm({
1328
- text: "Would you like to use git to manage this Worker?",
1329
- result: false,
1330
- });
1360
+ mockConfirm(
1361
+ {
1362
+ text: "Would you like to use git to manage this Worker?",
1363
+ result: false,
1364
+ },
1365
+ {
1366
+ text: "Would you like us to write your first test with Vitest?",
1367
+ result: true,
1368
+ }
1369
+ );
1331
1370
  mockSelect({
1332
1371
  text: "Would you like to create a Worker at src/index.ts?",
1333
1372
  result: "fetch",
@@ -1358,17 +1397,19 @@ describe("init", () => {
1358
1397
  },
1359
1398
  });
1360
1399
  expect(std).toMatchInlineSnapshot(`
1361
- Object {
1362
- "debug": "",
1363
- "err": "",
1364
- "out": "✨ Created wrangler.toml
1365
- ✨ Created src/index.ts
1366
-
1367
- To start developing your Worker, run \`npx wrangler dev\`
1368
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1369
- "warn": "",
1370
- }
1371
- `);
1400
+ Object {
1401
+ "debug": "",
1402
+ "err": "",
1403
+ "out": "✨ Created wrangler.toml
1404
+ ✨ Created src/index.ts
1405
+ ✨ Created src/index.test.ts
1406
+ ✨ Installed vitest into devDependencies
1407
+
1408
+ To start developing your Worker, run \`npx wrangler dev\`
1409
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1410
+ "warn": "",
1411
+ }
1412
+ `);
1372
1413
  });
1373
1414
 
1374
1415
  it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
@@ -1384,6 +1425,10 @@ describe("init", () => {
1384
1425
  {
1385
1426
  text: "Would you like to use TypeScript?",
1386
1427
  result: true,
1428
+ },
1429
+ {
1430
+ text: "Would you like us to write your first test with Vitest?",
1431
+ result: true,
1387
1432
  }
1388
1433
  );
1389
1434
  mockSelect({
@@ -1416,20 +1461,21 @@ describe("init", () => {
1416
1461
  },
1417
1462
  });
1418
1463
  expect(std).toMatchInlineSnapshot(`
1419
- Object {
1420
- "debug": "",
1421
- "err": "",
1422
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1423
- ✨ Created path/to/worker/my-worker/package.json
1424
- ✨ Created path/to/worker/my-worker/tsconfig.json
1425
- ✨ Created path/to/worker/my-worker/src/index.ts
1426
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1427
-
1428
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1429
- To publish your Worker to the Internet, run \`npm run deploy\`",
1430
- "warn": "",
1431
- }
1432
- `);
1464
+ Object {
1465
+ "debug": "",
1466
+ "err": "",
1467
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1468
+ ✨ Created path/to/worker/my-worker/package.json
1469
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1470
+ ✨ Created path/to/worker/my-worker/src/index.ts
1471
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
1472
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1473
+
1474
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1475
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1476
+ "warn": "",
1477
+ }
1478
+ `);
1433
1479
  });
1434
1480
 
1435
1481
  it("should offer to install type definitions in an existing typescript project", async () => {
@@ -1475,22 +1521,28 @@ describe("init", () => {
1475
1521
  "@cloudflare/workers-types"
1476
1522
  );
1477
1523
  expect(std).toMatchInlineSnapshot(`
1478
- Object {
1479
- "debug": "",
1480
- "err": "",
1481
- "out": "✨ Created wrangler.toml
1482
- ✨ Installed @cloudflare/workers-types into devDependencies
1483
- 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1484
- "warn": "",
1485
- }
1486
- `);
1524
+ Object {
1525
+ "debug": "",
1526
+ "err": "",
1527
+ "out": "✨ Created wrangler.toml
1528
+ ✨ Installed @cloudflare/workers-types into devDependencies
1529
+ 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1530
+ "warn": "",
1531
+ }
1532
+ `);
1487
1533
  });
1488
1534
 
1489
1535
  it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1490
- mockConfirm({
1491
- text: "Would you like to use git to manage this Worker?",
1492
- result: false,
1493
- });
1536
+ mockConfirm(
1537
+ {
1538
+ text: "Would you like to use git to manage this Worker?",
1539
+ result: false,
1540
+ },
1541
+ {
1542
+ text: "Would you like us to write your first test with Vitest?",
1543
+ result: true,
1544
+ }
1545
+ );
1494
1546
  mockSelect({
1495
1547
  text: "Would you like to create a Worker at src/index.ts?",
1496
1548
  result: "fetch",
@@ -1523,17 +1575,19 @@ describe("init", () => {
1523
1575
  },
1524
1576
  });
1525
1577
  expect(std).toMatchInlineSnapshot(`
1526
- Object {
1527
- "debug": "",
1528
- "err": "",
1529
- "out": "✨ Created wrangler.toml
1530
- ✨ Created src/index.ts
1531
-
1532
- To start developing your Worker, run \`npx wrangler dev\`
1533
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1534
- "warn": "",
1535
- }
1536
- `);
1578
+ Object {
1579
+ "debug": "",
1580
+ "err": "",
1581
+ "out": "✨ Created wrangler.toml
1582
+ ✨ Created src/index.ts
1583
+ ✨ Created src/index.test.ts
1584
+ ✨ Installed vitest into devDependencies
1585
+
1586
+ To start developing your Worker, run \`npx wrangler dev\`
1587
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1588
+ "warn": "",
1589
+ }
1590
+ `);
1537
1591
  });
1538
1592
  });
1539
1593
 
@@ -1555,7 +1609,8 @@ describe("init", () => {
1555
1609
  {
1556
1610
  text: "Would you like to use TypeScript?",
1557
1611
  result: false,
1558
- }
1612
+ },
1613
+ { text: "Would you like us to write your first test?", result: false }
1559
1614
  );
1560
1615
  mockSelect({
1561
1616
  text: "Would you like to create a Worker at src/index.js?",
@@ -1581,13 +1636,134 @@ describe("init", () => {
1581
1636
  },
1582
1637
  });
1583
1638
  expect(std.out).toMatchInlineSnapshot(`
1584
- "✨ Created wrangler.toml
1585
- ✨ Created package.json
1586
- ✨ Created src/index.js
1639
+ "✨ Created wrangler.toml
1640
+ ✨ Created package.json
1641
+ ✨ Created src/index.js
1587
1642
 
1588
- To start developing your Worker, run \`npm start\`
1589
- To publish your Worker to the Internet, run \`npm run deploy\`"
1590
- `);
1643
+ To start developing your Worker, run \`npm start\`
1644
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1645
+ `);
1646
+ });
1647
+ it("should add a jest test for a non-ts project with .js extension", async () => {
1648
+ mockConfirm(
1649
+ {
1650
+ text: "Would you like to use git to manage this Worker?",
1651
+ result: false,
1652
+ },
1653
+ {
1654
+ text: "No package.json found. Would you like to create one?",
1655
+ result: true,
1656
+ },
1657
+ {
1658
+ text: "Would you like to install wrangler into package.json?",
1659
+ result: false,
1660
+ },
1661
+ {
1662
+ text: "Would you like to use TypeScript?",
1663
+ result: false,
1664
+ },
1665
+ { text: "Would you like us to write your first test?", result: true }
1666
+ );
1667
+ mockSelect(
1668
+ {
1669
+ text: "Would you like to create a Worker at src/index.js?",
1670
+ result: "fetch",
1671
+ },
1672
+ { text: "Which test runner would you like to use?", result: "jest" }
1673
+ );
1674
+
1675
+ await runWrangler("init");
1676
+
1677
+ checkFiles({
1678
+ items: {
1679
+ "src/index.js": true,
1680
+ "src/index.test.js": true,
1681
+ "src/index.ts": false,
1682
+ "package.json": {
1683
+ contents: expect.objectContaining({
1684
+ name: expect.stringContaining("wrangler-tests"),
1685
+ version: "0.0.0",
1686
+ scripts: {
1687
+ start: "wrangler dev",
1688
+ deploy: "wrangler publish",
1689
+ test: "jest",
1690
+ },
1691
+ }),
1692
+ },
1693
+ },
1694
+ });
1695
+ expect(std.out).toMatchInlineSnapshot(`
1696
+ "✨ Created wrangler.toml
1697
+ ✨ Created package.json
1698
+ ✨ Created src/index.js
1699
+ ✨ Created src/index.test.js
1700
+ ✨ Installed jest into devDependencies
1701
+
1702
+ To start developing your Worker, run \`npm start\`
1703
+ To start testing your Worker, run \`npm test\`
1704
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1705
+ `);
1706
+ });
1707
+
1708
+ it("should add a vitest test for a non-ts project with .js extension", async () => {
1709
+ mockConfirm(
1710
+ {
1711
+ text: "Would you like to use git to manage this Worker?",
1712
+ result: false,
1713
+ },
1714
+ {
1715
+ text: "No package.json found. Would you like to create one?",
1716
+ result: true,
1717
+ },
1718
+ {
1719
+ text: "Would you like to install wrangler into package.json?",
1720
+ result: false,
1721
+ },
1722
+ {
1723
+ text: "Would you like to use TypeScript?",
1724
+ result: false,
1725
+ },
1726
+ { text: "Would you like us to write your first test?", result: true }
1727
+ );
1728
+ mockSelect(
1729
+ {
1730
+ text: "Would you like to create a Worker at src/index.js?",
1731
+ result: "fetch",
1732
+ },
1733
+ { text: "Which test runner would you like to use?", result: "vitest" }
1734
+ );
1735
+
1736
+ await runWrangler("init");
1737
+
1738
+ checkFiles({
1739
+ items: {
1740
+ "src/index.js": true,
1741
+ "src/index.test.js": true,
1742
+ "src/index.ts": false,
1743
+ "package.json": {
1744
+ contents: expect.objectContaining({
1745
+ name: expect.stringContaining("wrangler-tests"),
1746
+ version: "0.0.0",
1747
+ scripts: {
1748
+ start: "wrangler dev",
1749
+ deploy: "wrangler publish",
1750
+ test: "vitest",
1751
+ },
1752
+ }),
1753
+ },
1754
+ },
1755
+ });
1756
+ expect(std.out).toMatchInlineSnapshot(`
1757
+ "✨ Created wrangler.toml
1758
+ ✨ Created package.json
1759
+ ✨ Created src/index.js
1760
+ ✨ Created src/index.test.js
1761
+ ✨ Installed vitest into devDependencies
1762
+
1763
+ To start developing your Worker, run \`npm start\`
1764
+ To start testing your Worker, run \`npm test\`
1765
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1766
+ `);
1591
1767
  });
1592
1768
 
1593
1769
  it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
@@ -1603,7 +1779,8 @@ describe("init", () => {
1603
1779
  {
1604
1780
  text: "Would you like to use TypeScript?",
1605
1781
  result: false,
1606
- }
1782
+ },
1783
+ { text: "Would you like us to write your first test?", result: false }
1607
1784
  );
1608
1785
  mockSelect({
1609
1786
  text: "Would you like to create a Worker at src/index.js?",
@@ -1639,12 +1816,12 @@ describe("init", () => {
1639
1816
  },
1640
1817
  });
1641
1818
  expect(std.out).toMatchInlineSnapshot(`
1642
- "✨ Created wrangler.toml
1643
- ✨ Created src/index.js
1819
+ "✨ Created wrangler.toml
1820
+ ✨ Created src/index.js
1644
1821
 
1645
- To start developing your Worker, run \`npx wrangler dev\`
1646
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1647
- `);
1822
+ To start developing your Worker, run \`npx wrangler dev\`
1823
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1824
+ `);
1648
1825
  });
1649
1826
 
1650
1827
  it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
@@ -1679,13 +1856,13 @@ describe("init", () => {
1679
1856
  },
1680
1857
  });
1681
1858
  expect(std).toMatchInlineSnapshot(`
1682
- Object {
1683
- "debug": "",
1684
- "err": "",
1685
- "out": "✨ Created wrangler.toml",
1686
- "warn": "",
1687
- }
1688
- `);
1859
+ Object {
1860
+ "debug": "",
1861
+ "err": "",
1862
+ "out": "✨ Created wrangler.toml",
1863
+ "warn": "",
1864
+ }
1865
+ `);
1689
1866
  });
1690
1867
 
1691
1868
  it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
@@ -1722,13 +1899,13 @@ describe("init", () => {
1722
1899
  },
1723
1900
  });
1724
1901
  expect(std).toMatchInlineSnapshot(`
1725
- Object {
1726
- "debug": "",
1727
- "err": "",
1728
- "out": "✨ Created my-worker/wrangler.toml",
1729
- "warn": "",
1730
- }
1731
- `);
1902
+ Object {
1903
+ "debug": "",
1904
+ "err": "",
1905
+ "out": "✨ Created my-worker/wrangler.toml",
1906
+ "warn": "",
1907
+ }
1908
+ `);
1732
1909
  });
1733
1910
  });
1734
1911
 
@@ -1759,21 +1936,22 @@ describe("init", () => {
1759
1936
  },
1760
1937
  });
1761
1938
  expect(std).toMatchInlineSnapshot(`
1762
- Object {
1763
- "debug": "",
1764
- "err": "",
1765
- "out": "✨ Created wrangler.toml
1766
- ✨ Initialized git repository
1767
- ✨ Created package.json
1768
- ✨ Created tsconfig.json
1769
- ✨ Created src/index.ts
1770
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1771
-
1772
- To start developing your Worker, run \`npm start\`
1773
- To publish your Worker to the Internet, run \`npm run deploy\`",
1774
- "warn": "",
1775
- }
1776
- `);
1939
+ Object {
1940
+ "debug": "",
1941
+ "err": "",
1942
+ "out": "✨ Created wrangler.toml
1943
+ ✨ Initialized git repository
1944
+ ✨ Created package.json
1945
+ ✨ Created tsconfig.json
1946
+ ✨ Created src/index.ts
1947
+ ✨ Created src/index.test.ts
1948
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1949
+
1950
+ To start developing your Worker, run \`npm start\`
1951
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1952
+ "warn": "",
1953
+ }
1954
+ `);
1777
1955
  });
1778
1956
 
1779
1957
  it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
@@ -1788,21 +1966,22 @@ describe("init", () => {
1788
1966
  },
1789
1967
  });
1790
1968
  expect(std).toMatchInlineSnapshot(`
1791
- Object {
1792
- "debug": "",
1793
- "err": "",
1794
- "out": "✨ Created path/to/worker/wrangler.toml
1795
- ✨ Initialized git repository at path/to/worker
1796
- ✨ Created path/to/worker/package.json
1797
- ✨ Created path/to/worker/tsconfig.json
1798
- ✨ Created path/to/worker/src/index.ts
1799
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1800
-
1801
- To start developing your Worker, run \`cd path/to/worker && npm start\`
1802
- To publish your Worker to the Internet, run \`npm run deploy\`",
1803
- "warn": "",
1804
- }
1805
- `);
1969
+ Object {
1970
+ "debug": "",
1971
+ "err": "",
1972
+ "out": "✨ Created path/to/worker/wrangler.toml
1973
+ ✨ Initialized git repository at path/to/worker
1974
+ ✨ Created path/to/worker/package.json
1975
+ ✨ Created path/to/worker/tsconfig.json
1976
+ ✨ Created path/to/worker/src/index.ts
1977
+ ✨ Created path/to/worker/src/index.test.ts
1978
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1979
+
1980
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
1981
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1982
+ "warn": "",
1983
+ }
1984
+ `);
1806
1985
  });
1807
1986
 
1808
1987
  it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
@@ -1817,21 +1996,22 @@ describe("init", () => {
1817
1996
  },
1818
1997
  });
1819
1998
  expect(std).toMatchInlineSnapshot(`
1820
- Object {
1821
- "debug": "",
1822
- "err": "",
1823
- "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1824
- ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1825
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1826
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1827
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1828
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1829
-
1830
- To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1831
- To publish your Worker to the Internet, run \`npm run deploy\`",
1832
- "warn": "",
1833
- }
1834
- `);
1999
+ Object {
2000
+ "debug": "",
2001
+ "err": "",
2002
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
2003
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
2004
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
2005
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
2006
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
2007
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.test.ts
2008
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2009
+
2010
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
2011
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2012
+ "warn": "",
2013
+ }
2014
+ `);
1835
2015
  });
1836
2016
 
1837
2017
  it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
@@ -1851,6 +2031,10 @@ describe("init", () => {
1851
2031
  {
1852
2032
  text: "Would you like to install the type definitions for Workers into your package.json?",
1853
2033
  result: true,
2034
+ },
2035
+ {
2036
+ text: "Would you like us to write your first test with Vitest?",
2037
+ result: true,
1854
2038
  }
1855
2039
  );
1856
2040
  mockSelect({
@@ -1900,8 +2084,926 @@ describe("init", () => {
1900
2084
  });
1901
2085
  });
1902
2086
  });
2087
+
2088
+ describe("from dashboard", () => {
2089
+ mockApiToken();
2090
+ mockAccountId({ accountId: "LCARS" });
2091
+
2092
+ const mockDashboardScript = `
2093
+ export default {
2094
+ async fetch(request, env, ctx) {
2095
+ return new Response("Hello World!");
2096
+ },
2097
+ };
2098
+ `;
2099
+ const mockServiceMetadata = {
2100
+ id: "memory-crystal",
2101
+ default_environment: {
2102
+ environment: "test",
2103
+ created_on: "1987-9-27",
2104
+ modified_on: "1987-9-27",
2105
+ script: {
2106
+ id: "memory-crystal",
2107
+ tag: "test-tag",
2108
+ etag: "some-etag",
2109
+ handlers: [],
2110
+ modified_on: "1987-9-27",
2111
+ created_on: "1987-9-27",
2112
+ migration_tag: "some-migration-tag",
2113
+ usage_model: "bundled",
2114
+ compatibility_date: "1987-9-27",
2115
+ },
2116
+ },
2117
+ created_on: "1987-9-27",
2118
+ modified_on: "1987-9-27",
2119
+ usage_model: "bundled",
2120
+ environments: [
2121
+ {
2122
+ environment: "test",
2123
+ created_on: "1987-9-27",
2124
+ modified_on: "1987-9-27",
2125
+ },
2126
+ {
2127
+ environment: "staging",
2128
+ created_on: "1987-9-27",
2129
+ modified_on: "1987-9-27",
2130
+ },
2131
+ ],
2132
+ };
2133
+ const mockBindingsRes = [
2134
+ {
2135
+ type: "secret_text",
2136
+ name: "ABC",
2137
+ },
2138
+ {
2139
+ type: "plain_text",
2140
+ name: "ANOTHER-NAME",
2141
+ text: "thing-TEXT",
2142
+ },
2143
+ {
2144
+ type: "durable_object_namespace",
2145
+ name: "DURABLE_TEST",
2146
+ class_name: "Durability",
2147
+ script_name: "another-durable-object-worker",
2148
+ environment: "production",
2149
+ },
2150
+ {
2151
+ type: "kv_namespace",
2152
+ name: "kv_testing",
2153
+ namespace_id: "some-namespace-id",
2154
+ },
2155
+ {
2156
+ type: "r2_bucket",
2157
+ bucket_name: "test-bucket",
2158
+ name: "test-bucket",
2159
+ },
2160
+ {
2161
+ environment: "production",
2162
+ name: "website",
2163
+ service: "website",
2164
+ type: "service",
2165
+ },
2166
+ {
2167
+ type: "dispatch_namespace",
2168
+ name: "name-namespace-mock",
2169
+ namespace: "namespace-mock",
2170
+ },
2171
+ {
2172
+ name: "httplogs",
2173
+ type: "logfwdr",
2174
+ destination: "httplogs",
2175
+ },
2176
+ {
2177
+ name: "trace",
2178
+ type: "logfwdr",
2179
+ destination: "trace",
2180
+ },
2181
+ {
2182
+ type: "wasm_module",
2183
+ name: "WASM_MODULE_ONE",
2184
+ part: "./some_wasm.wasm",
2185
+ },
2186
+ {
2187
+ type: "wasm_module",
2188
+ name: "WASM_MODULE_TWO",
2189
+ part: "./more_wasm.wasm",
2190
+ },
2191
+ {
2192
+ type: "text_blob",
2193
+ name: "TEXT_BLOB_ONE",
2194
+ part: "./my-entire-app-depends-on-this.cfg",
2195
+ },
2196
+ {
2197
+ type: "text_blob",
2198
+ name: "TEXT_BLOB_TWO",
2199
+ part: "./the-entirety-of-human-knowledge.txt",
2200
+ },
2201
+ { type: "data_blob", name: "DATA_BLOB_ONE", part: "DATA_BLOB_ONE" },
2202
+ { type: "data_blob", name: "DATA_BLOB_TWO", part: "DATA_BLOB_TWO" },
2203
+ {
2204
+ type: "some unsafe thing",
2205
+ name: "UNSAFE_BINDING_ONE",
2206
+ data: { some: { unsafe: "thing" } },
2207
+ },
2208
+ {
2209
+ type: "another unsafe thing",
2210
+ name: "UNSAFE_BINDING_TWO",
2211
+ data: 1337,
2212
+ },
2213
+ ];
2214
+ const mockRoutesRes = [
2215
+ {
2216
+ id: "some-route-id",
2217
+ pattern: "delta.quadrant",
2218
+ },
2219
+ ];
2220
+ const mockConfigExpected = {
2221
+ main: "src/index.ts",
2222
+ compatibility_date: "1987-9-27",
2223
+ name: "isolinear-optical-chip",
2224
+ migrations: [
2225
+ {
2226
+ new_classes: ["Durability"],
2227
+ tag: "some-migration-tag",
2228
+ },
2229
+ ],
2230
+ durable_objects: {
2231
+ bindings: [
2232
+ {
2233
+ class_name: "Durability",
2234
+ name: "DURABLE_TEST",
2235
+ script_name: "another-durable-object-worker",
2236
+ environment: "production",
2237
+ },
2238
+ ],
2239
+ },
2240
+ kv_namespaces: [
2241
+ {
2242
+ binding: "kv_testing",
2243
+ id: "some-namespace-id",
2244
+ },
2245
+ ],
2246
+ r2_buckets: [
2247
+ {
2248
+ bucket_name: "test-bucket",
2249
+ binding: "test-bucket",
2250
+ },
2251
+ ],
2252
+ dispatch_namespaces: [
2253
+ {
2254
+ binding: "name-namespace-mock",
2255
+ namespace: "namespace-mock",
2256
+ },
2257
+ ],
2258
+ route: "delta.quadrant",
2259
+ services: [
2260
+ {
2261
+ environment: "production",
2262
+ binding: "website",
2263
+ service: "website",
2264
+ },
2265
+ ],
2266
+ triggers: {
2267
+ crons: ["0 0 0 * * *"],
2268
+ },
2269
+ usage_model: "bundled",
2270
+ vars: {
2271
+ "ANOTHER-NAME": "thing-TEXT",
2272
+ },
2273
+ env: {
2274
+ test: {},
2275
+ staging: {},
2276
+ },
2277
+ unsafe: {
2278
+ bindings: [
2279
+ {
2280
+ name: "UNSAFE_BINDING_ONE",
2281
+ type: "some unsafe thing",
2282
+ data: { some: { unsafe: "thing" } },
2283
+ },
2284
+ {
2285
+ name: "UNSAFE_BINDING_TWO",
2286
+ type: "another unsafe thing",
2287
+ data: 1337,
2288
+ },
2289
+ ],
2290
+ },
2291
+ wasm_modules: {
2292
+ WASM_MODULE_ONE: "./some_wasm.wasm",
2293
+ WASM_MODULE_TWO: "./more_wasm.wasm",
2294
+ },
2295
+ text_blobs: {
2296
+ TEXT_BLOB_ONE: "./my-entire-app-depends-on-this.cfg",
2297
+ TEXT_BLOB_TWO: "./the-entirety-of-human-knowledge.txt",
2298
+ },
2299
+ data_blobs: {
2300
+ DATA_BLOB_ONE: "DATA_BLOB_ONE",
2301
+ DATA_BLOB_TWO: "DATA_BLOB_TWO",
2302
+ },
2303
+ logfwdr: {
2304
+ schema: "",
2305
+ bindings: [
2306
+ {
2307
+ name: "httplogs",
2308
+ destination: "httplogs",
2309
+ },
2310
+ {
2311
+ name: "trace",
2312
+ destination: "trace",
2313
+ },
2314
+ ],
2315
+ },
2316
+ };
2317
+
2318
+ function mockSupportingDashRequests({
2319
+ expectedAccountId = "",
2320
+ expectedScriptName = "",
2321
+ expectedEnvironment = "",
2322
+ expectedCompatDate,
2323
+ }: {
2324
+ expectedAccountId: string;
2325
+ expectedScriptName: string;
2326
+ expectedEnvironment: string;
2327
+ expectedCompatDate: string | undefined;
2328
+ }) {
2329
+ msw.use(
2330
+ rest.get(
2331
+ `*/accounts/:accountId/workers/services/:scriptName`,
2332
+ (req, res, ctx) => {
2333
+ expect(req.params.accountId).toEqual(expectedAccountId);
2334
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2335
+
2336
+ if (expectedCompatDate === undefined)
2337
+ (mockServiceMetadata.default_environment.script
2338
+ .compatibility_date as unknown) = expectedCompatDate;
2339
+
2340
+ return res.once(
2341
+ ctx.status(200),
2342
+ ctx.json({
2343
+ success: true,
2344
+ errors: [],
2345
+ messages: [],
2346
+ result: mockServiceMetadata,
2347
+ })
2348
+ );
2349
+ }
2350
+ ),
2351
+ rest.get(
2352
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2353
+ (req, res, ctx) => {
2354
+ expect(req.params.accountId).toEqual(expectedAccountId);
2355
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2356
+ expect(req.params.environment).toEqual(expectedEnvironment);
2357
+
2358
+ return res.once(
2359
+ ctx.status(200),
2360
+ ctx.json({
2361
+ success: true,
2362
+ errors: [],
2363
+ messages: [],
2364
+ result: mockBindingsRes,
2365
+ })
2366
+ );
2367
+ }
2368
+ ),
2369
+ rest.get(
2370
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2371
+ (req, res, ctx) => {
2372
+ expect(req.params.accountId).toEqual(expectedAccountId);
2373
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2374
+ expect(req.params.environment).toEqual(expectedEnvironment);
2375
+
2376
+ return res.once(
2377
+ ctx.status(200),
2378
+ ctx.json({
2379
+ success: true,
2380
+ errors: [],
2381
+ messages: [],
2382
+ result: mockRoutesRes,
2383
+ })
2384
+ );
2385
+ }
2386
+ ),
2387
+ rest.get(
2388
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2389
+
2390
+ (req, res, ctx) => {
2391
+ expect(req.params.accountId).toEqual(expectedAccountId);
2392
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2393
+ expect(req.params.environment).toEqual(expectedEnvironment);
2394
+
2395
+ return res.once(
2396
+ ctx.status(200),
2397
+ ctx.json({
2398
+ success: true,
2399
+ errors: [],
2400
+ messages: [],
2401
+ result: mockServiceMetadata.default_environment,
2402
+ })
2403
+ );
2404
+ }
2405
+ ),
2406
+ rest.get(
2407
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2408
+ (req, res, ctx) => {
2409
+ expect(req.params.accountId).toEqual(expectedAccountId);
2410
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2411
+
2412
+ return res.once(
2413
+ ctx.status(200),
2414
+ ctx.json({
2415
+ success: true,
2416
+ errors: [],
2417
+ messages: [],
2418
+ result: {
2419
+ schedules: [
2420
+ {
2421
+ cron: "0 0 0 * * *",
2422
+ created_on: new Date(1987, 9, 27),
2423
+ modified_on: new Date(1987, 9, 27),
2424
+ },
2425
+ ],
2426
+ },
2427
+ })
2428
+ );
2429
+ }
2430
+ )
2431
+ );
2432
+ }
2433
+
2434
+ //TODO: Tests for a case when a worker name doesn't exist - JACOB & CASS
2435
+ it("should download source script from dashboard w/ positional <name> in TypeScript project", async () => {
2436
+ mockSupportingDashRequests({
2437
+ expectedAccountId: "LCARS",
2438
+ expectedScriptName: "memory-crystal",
2439
+ expectedEnvironment: "test",
2440
+ expectedCompatDate: "1987-9-27",
2441
+ });
2442
+ setMockFetchDashScript(mockDashboardScript);
2443
+ mockConfirm(
2444
+ {
2445
+ text: "Would you like to use git to manage this Worker?",
2446
+ result: false,
2447
+ },
2448
+ {
2449
+ text: "Would you like to use TypeScript?",
2450
+ result: true,
2451
+ },
2452
+ {
2453
+ text: "No package.json found. Would you like to create one?",
2454
+ result: true,
2455
+ },
2456
+ {
2457
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2458
+ result: true,
2459
+ }
2460
+ );
2461
+
2462
+ await runWrangler(
2463
+ "init isolinear-optical-chip --from-dash memory-crystal"
2464
+ );
2465
+
2466
+ expect(std.out).toContain("cd isolinear-optical-chip");
2467
+
2468
+ checkFiles({
2469
+ items: {
2470
+ "isolinear-optical-chip/src/index.js": false,
2471
+ "isolinear-optical-chip/src/index.ts": {
2472
+ contents: mockDashboardScript,
2473
+ },
2474
+ "isolinear-optical-chip/package.json": {
2475
+ contents: expect.objectContaining({
2476
+ name: "isolinear-optical-chip",
2477
+ }),
2478
+ },
2479
+ "isolinear-optical-chip/tsconfig.json": true,
2480
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2481
+ ...mockConfigExpected,
2482
+ }),
2483
+ },
2484
+ });
2485
+ });
2486
+
2487
+ it("should fail on init --from-dash on non-existent worker name", async () => {
2488
+ msw.use(
2489
+ rest.get(
2490
+ `*/accounts/:accountId/workers/services/:scriptName`,
2491
+ (req, res, ctx) => {
2492
+ return res.once(
2493
+ ctx.status(200),
2494
+ ctx.json({
2495
+ success: true,
2496
+ errors: [],
2497
+ messages: [],
2498
+ result: mockServiceMetadata,
2499
+ })
2500
+ );
2501
+ }
2502
+ )
2503
+ );
2504
+ setMockFetchDashScript(mockDashboardScript);
2505
+ mockConfirm(
2506
+ {
2507
+ text: "Would you like to use git to manage this Worker?",
2508
+ result: false,
2509
+ },
2510
+ {
2511
+ text: "Would you like to use TypeScript?",
2512
+ result: true,
2513
+ },
2514
+ {
2515
+ text: "No package.json found. Would you like to create one?",
2516
+ result: true,
2517
+ },
2518
+ {
2519
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2520
+ result: true,
2521
+ }
2522
+ );
2523
+
2524
+ await expect(
2525
+ runWrangler("init isolinear-optical-chip --from-dash i-dont-exist")
2526
+ ).rejects.toThrowError();
2527
+ });
2528
+
2529
+ it("should download source script from dashboard w/ out positional <name>", async () => {
2530
+ mockSupportingDashRequests({
2531
+ expectedAccountId: "LCARS",
2532
+ expectedScriptName: "isolinear-optical-chip",
2533
+ expectedEnvironment: "test",
2534
+ expectedCompatDate: "1987-9-27",
2535
+ });
2536
+ setMockFetchDashScript(mockDashboardScript);
2537
+ mockConfirm(
2538
+ {
2539
+ text: "Would you like to use git to manage this Worker?",
2540
+ result: false,
2541
+ },
2542
+ {
2543
+ text: "Would you like to use TypeScript?",
2544
+ result: true,
2545
+ },
2546
+ {
2547
+ text: "No package.json found. Would you like to create one?",
2548
+ result: true,
2549
+ },
2550
+ {
2551
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2552
+ result: true,
2553
+ }
2554
+ );
2555
+
2556
+ await runWrangler("init --from-dash isolinear-optical-chip");
2557
+
2558
+ expect(fs.readFileSync("./isolinear-optical-chip/wrangler.toml", "utf8"))
2559
+ .toMatchInlineSnapshot(`
2560
+ "name = \\"isolinear-optical-chip\\"
2561
+ main = \\"src/index.ts\\"
2562
+ compatibility_date = \\"1987-9-27\\"
2563
+ route = \\"delta.quadrant\\"
2564
+ usage_model = \\"bundled\\"
2565
+
2566
+ [[migrations]]
2567
+ tag = \\"some-migration-tag\\"
2568
+ new_classes = [ \\"Durability\\" ]
2569
+
2570
+ [triggers]
2571
+ crons = [ \\"0 0 0 * * *\\" ]
2572
+
2573
+ [env]
2574
+ test = { }
2575
+ staging = { }
2576
+
2577
+ [vars]
2578
+ ANOTHER-NAME = \\"thing-TEXT\\"
2579
+
2580
+ [[durable_objects.bindings]]
2581
+ name = \\"DURABLE_TEST\\"
2582
+ class_name = \\"Durability\\"
2583
+ script_name = \\"another-durable-object-worker\\"
2584
+ environment = \\"production\\"
2585
+
2586
+ [[kv_namespaces]]
2587
+ id = \\"some-namespace-id\\"
2588
+ binding = \\"kv_testing\\"
2589
+
2590
+ [[r2_buckets]]
2591
+ binding = \\"test-bucket\\"
2592
+ bucket_name = \\"test-bucket\\"
2593
+
2594
+ [[services]]
2595
+ binding = \\"website\\"
2596
+ service = \\"website\\"
2597
+ environment = \\"production\\"
2598
+
2599
+ [[dispatch_namespaces]]
2600
+ binding = \\"name-namespace-mock\\"
2601
+ namespace = \\"namespace-mock\\"
2602
+
2603
+ [logfwdr]
2604
+ schema = \\"\\"
2605
+
2606
+ [[logfwdr.bindings]]
2607
+ name = \\"httplogs\\"
2608
+ destination = \\"httplogs\\"
2609
+
2610
+ [[logfwdr.bindings]]
2611
+ name = \\"trace\\"
2612
+ destination = \\"trace\\"
2613
+
2614
+ [wasm_modules]
2615
+ WASM_MODULE_ONE = \\"./some_wasm.wasm\\"
2616
+ WASM_MODULE_TWO = \\"./more_wasm.wasm\\"
2617
+
2618
+ [text_blobs]
2619
+ TEXT_BLOB_ONE = \\"./my-entire-app-depends-on-this.cfg\\"
2620
+ TEXT_BLOB_TWO = \\"./the-entirety-of-human-knowledge.txt\\"
2621
+
2622
+ [data_blobs]
2623
+ DATA_BLOB_ONE = \\"DATA_BLOB_ONE\\"
2624
+ DATA_BLOB_TWO = \\"DATA_BLOB_TWO\\"
2625
+
2626
+ [[unsafe.bindings]]
2627
+ type = \\"some unsafe thing\\"
2628
+ name = \\"UNSAFE_BINDING_ONE\\"
2629
+
2630
+ [unsafe.bindings.data.some]
2631
+ unsafe = \\"thing\\"
2632
+
2633
+ [[unsafe.bindings]]
2634
+ type = \\"another unsafe thing\\"
2635
+ name = \\"UNSAFE_BINDING_TWO\\"
2636
+ data = 1_337
2637
+ "
2638
+ `);
2639
+ expect(std.out).toContain("cd isolinear-optical-chip");
2640
+
2641
+ checkFiles({
2642
+ items: {
2643
+ "isolinear-optical-chip/src/index.js": false,
2644
+ "isolinear-optical-chip/src/index.ts": {
2645
+ contents: mockDashboardScript,
2646
+ },
2647
+ "isolinear-optical-chip/package.json": {
2648
+ contents: expect.objectContaining({
2649
+ name: "isolinear-optical-chip",
2650
+ }),
2651
+ },
2652
+ "isolinear-optical-chip/tsconfig.json": true,
2653
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2654
+ ...mockConfigExpected,
2655
+ name: "isolinear-optical-chip",
2656
+ }),
2657
+ },
2658
+ });
2659
+ });
2660
+
2661
+ it("should download source script from dashboard as plain JavaScript", async () => {
2662
+ mockSupportingDashRequests({
2663
+ expectedAccountId: "LCARS",
2664
+ expectedScriptName: "isolinear-optical-chip",
2665
+ expectedEnvironment: "test",
2666
+ expectedCompatDate: "1987-9-27",
2667
+ });
2668
+ setMockFetchDashScript(mockDashboardScript);
2669
+ mockConfirm(
2670
+ {
2671
+ text: "Would you like to use git to manage this Worker?",
2672
+ result: false,
2673
+ },
2674
+ {
2675
+ text: "Would you like to use TypeScript?",
2676
+ result: false,
2677
+ },
2678
+ {
2679
+ text: "No package.json found. Would you like to create one?",
2680
+ result: true,
2681
+ },
2682
+ {
2683
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2684
+ result: true,
2685
+ }
2686
+ );
2687
+
2688
+ await runWrangler("init --from-dash isolinear-optical-chip");
2689
+
2690
+ checkFiles({
2691
+ items: {
2692
+ "isolinear-optical-chip/src/index.js": {
2693
+ contents: mockDashboardScript,
2694
+ },
2695
+ "isolinear-optical-chip/src/index.ts": false,
2696
+ "isolinear-optical-chip/package.json": {
2697
+ contents: expect.objectContaining({
2698
+ name: "isolinear-optical-chip",
2699
+ }),
2700
+ },
2701
+ "isolinear-optical-chip/tsconfig.json": false,
2702
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2703
+ ...mockConfigExpected,
2704
+ name: "isolinear-optical-chip",
2705
+ }),
2706
+ },
2707
+ });
2708
+ });
2709
+
2710
+ it("should use fallback compatibility date if none is upstream", async () => {
2711
+ const mockDate = "1988-08-07";
2712
+ jest
2713
+ .spyOn(Date.prototype, "toISOString")
2714
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2715
+
2716
+ mockSupportingDashRequests({
2717
+ expectedAccountId: "LCARS",
2718
+ expectedScriptName: "isolinear-optical-chip",
2719
+ expectedEnvironment: "test",
2720
+ expectedCompatDate: undefined,
2721
+ });
2722
+ setMockFetchDashScript(mockDashboardScript);
2723
+ mockConfirm(
2724
+ {
2725
+ text: "Would you like to use git to manage this Worker?",
2726
+ result: false,
2727
+ },
2728
+ {
2729
+ text: "Would you like to use TypeScript?",
2730
+ result: true,
2731
+ },
2732
+ {
2733
+ text: "No package.json found. Would you like to create one?",
2734
+ result: true,
2735
+ },
2736
+ {
2737
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2738
+ result: true,
2739
+ }
2740
+ );
2741
+
2742
+ await runWrangler("init --from-dash isolinear-optical-chip");
2743
+
2744
+ mockConfigExpected.compatibility_date = "1988-08-07";
2745
+ checkFiles({
2746
+ items: {
2747
+ "isolinear-optical-chip/src/index.ts": {
2748
+ contents: mockDashboardScript,
2749
+ },
2750
+ "isolinear-optical-chip/package.json": {
2751
+ contents: expect.objectContaining({
2752
+ name: "isolinear-optical-chip",
2753
+ }),
2754
+ },
2755
+ "isolinear-optical-chip/tsconfig.json": true,
2756
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2757
+ ...mockConfigExpected,
2758
+ name: "isolinear-optical-chip",
2759
+ }),
2760
+ },
2761
+ });
2762
+ });
2763
+
2764
+ it("should throw an error to retry if a request fails", async () => {
2765
+ msw.use(
2766
+ rest.get(
2767
+ `*/accounts/:accountId/workers/services/:scriptName`,
2768
+ (req, res, ctx) => {
2769
+ expect(req.params.accountId).toBe("LCARS");
2770
+ expect(req.params.scriptName).toBe("isolinear-optical-chip");
2771
+ return res.once(
2772
+ ctx.status(200),
2773
+ ctx.json({
2774
+ success: true,
2775
+ errors: [],
2776
+ messages: [],
2777
+ result: mockServiceMetadata,
2778
+ })
2779
+ );
2780
+ }
2781
+ ),
2782
+ rest.get(
2783
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2784
+ (req, res) => {
2785
+ return res.networkError("Mock Network Error");
2786
+ }
2787
+ )
2788
+ );
2789
+
2790
+ setMockFetchDashScript(mockDashboardScript);
2791
+ mockConfirm(
2792
+ {
2793
+ text: "Would you like to use git to manage this Worker?",
2794
+ result: false,
2795
+ },
2796
+ {
2797
+ text: "Would you like to use TypeScript?",
2798
+ result: false,
2799
+ },
2800
+ {
2801
+ text: "No package.json found. Would you like to create one?",
2802
+ result: true,
2803
+ },
2804
+ {
2805
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2806
+ result: true,
2807
+ }
2808
+ );
2809
+
2810
+ await expect(
2811
+ runWrangler("init --from-dash isolinear-optical-chip")
2812
+ ).rejects.toThrowError();
2813
+ });
2814
+
2815
+ it("should not inlcude migrations in config file when none are necessary", async () => {
2816
+ const mockDate = "1988-08-07";
2817
+ jest
2818
+ .spyOn(Date.prototype, "toISOString")
2819
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2820
+ const mockData = {
2821
+ id: "memory-crystal",
2822
+ default_environment: {
2823
+ environment: "test",
2824
+ created_on: "1988-08-07",
2825
+ modified_on: "1988-08-07",
2826
+ script: {
2827
+ id: "memory-crystal",
2828
+ tag: "test-tag",
2829
+ etag: "some-etag",
2830
+ handlers: [],
2831
+ modified_on: "1988-08-07",
2832
+ created_on: "1988-08-07",
2833
+ usage_model: "bundled",
2834
+ compatibility_date: "1988-08-07",
2835
+ },
2836
+ },
2837
+ environments: [],
2838
+ };
2839
+ msw.use(
2840
+ rest.get(
2841
+ `*/accounts/:accountId/workers/services/:scriptName`,
2842
+ (req, res, ctx) => {
2843
+ return res.once(
2844
+ ctx.status(200),
2845
+ ctx.json({
2846
+ success: true,
2847
+ errors: [],
2848
+ messages: [],
2849
+ result: mockData,
2850
+ })
2851
+ );
2852
+ }
2853
+ ),
2854
+ rest.get(
2855
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2856
+ (req, res, ctx) => {
2857
+ return res.once(
2858
+ ctx.status(200),
2859
+ ctx.json({
2860
+ success: true,
2861
+ errors: [],
2862
+ messages: [],
2863
+ result: [],
2864
+ })
2865
+ );
2866
+ }
2867
+ ),
2868
+ rest.get(
2869
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2870
+ (req, res, ctx) => {
2871
+ return res.once(
2872
+ ctx.status(200),
2873
+ ctx.json({
2874
+ success: true,
2875
+ errors: [],
2876
+ messages: [],
2877
+ result: [],
2878
+ })
2879
+ );
2880
+ }
2881
+ ),
2882
+ rest.get(
2883
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2884
+ (req, res, ctx) => {
2885
+ return res.once(
2886
+ ctx.status(200),
2887
+ ctx.json({
2888
+ success: true,
2889
+ errors: [],
2890
+ messages: [],
2891
+ result: mockServiceMetadata.default_environment,
2892
+ })
2893
+ );
2894
+ }
2895
+ ),
2896
+ rest.get(
2897
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2898
+ (req, res, ctx) => {
2899
+ return res.once(
2900
+ ctx.status(200),
2901
+ ctx.json({
2902
+ success: true,
2903
+ errors: [],
2904
+ messages: [],
2905
+ result: { schedules: [] },
2906
+ })
2907
+ );
2908
+ }
2909
+ )
2910
+ );
2911
+
2912
+ setMockFetchDashScript(mockDashboardScript);
2913
+
2914
+ mockConfirm(
2915
+ {
2916
+ text: "Would you like to use git to manage this Worker?",
2917
+ result: false,
2918
+ },
2919
+ {
2920
+ text: "Would you like to use TypeScript?",
2921
+ result: true,
2922
+ },
2923
+ {
2924
+ text: "No package.json found. Would you like to create one?",
2925
+ result: true,
2926
+ },
2927
+ {
2928
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2929
+ result: true,
2930
+ }
2931
+ );
2932
+
2933
+ await runWrangler("init --from-dash isolinear-optical-chip");
2934
+
2935
+ checkFiles({
2936
+ items: {
2937
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2938
+ compatibility_date: "1988-08-07",
2939
+ env: {},
2940
+ main: "src/index.ts",
2941
+ triggers: {
2942
+ crons: [],
2943
+ },
2944
+ usage_model: "bundled",
2945
+ name: "isolinear-optical-chip",
2946
+ }),
2947
+ },
2948
+ });
2949
+ });
2950
+
2951
+ it("should not continue if no worker name is provided", async () => {
2952
+ await expect(
2953
+ runWrangler("init --from-dash")
2954
+ ).rejects.toMatchInlineSnapshot(
2955
+ `[Error: Not enough arguments following: from-dash]`
2956
+ );
2957
+ checkFiles({
2958
+ items: {
2959
+ "isolinear-optical-chip/src/index.js": false,
2960
+ "isolinear-optical-chip/src/index.ts": false,
2961
+ "isolinear-optical-chip/package.json": false,
2962
+ "isolinear-optical-chip/tsconfig.json": false,
2963
+ "isolinear-optical-chip/wrangler.toml": false,
2964
+ },
2965
+ });
2966
+ });
2967
+ });
1903
2968
  });
1904
2969
 
2970
+ function getDefaultBranchName() {
2971
+ try {
2972
+ const { stdout: defaultBranchName } = execaSync("git", [
2973
+ "config",
2974
+ "--get",
2975
+ "init.defaultBranch",
2976
+ ]);
2977
+
2978
+ return defaultBranchName;
2979
+ } catch {
2980
+ // ew
2981
+ return "master";
2982
+ }
2983
+ }
2984
+
2985
+ /**
2986
+ * Mock setter for usage within test blocks for dashboard script
2987
+ */
2988
+ export function setMockFetchDashScript(mockResponse: string) {
2989
+ msw.use(
2990
+ rest.get(
2991
+ `*/accounts/:accountId/workers/services/:fromDashScriptName/environments/:environment/content`,
2992
+ (req, res, ctx) => {
2993
+ // This is a fake FormData object, until we can get MSW (beta) that supports FormData
2994
+ const mockForm = new FormData();
2995
+ mockForm.append("name", mockResponse);
2996
+ // Sending it as JSON will result in empty object, but cause the fetchDashboardScript to execute its code
2997
+ return res(ctx.status(200), ctx.json(mockForm));
2998
+ }
2999
+ )
3000
+ );
3001
+ // Mock the actual return value that FormData would return
3002
+ (fetchDashboardScript as jest.Mock).mockImplementationOnce(() => {
3003
+ return mockResponse;
3004
+ });
3005
+ }
3006
+
1905
3007
  /**
1906
3008
  * Change the current working directory, ensuring that this exists.
1907
3009
  */