wrangler 0.0.0-e928f94 → 0.0.0-e93063e9

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 (338) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +180 -28
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6364 -0
  6. package/package.json +194 -113
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +238 -0
  9. package/src/__tests__/api-devregistry.test.js +64 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +4163 -0
  14. package/src/__tests__/d1/d1.test.ts +123 -0
  15. package/src/__tests__/d1/splitter.test.ts +255 -0
  16. package/src/__tests__/delete.test.ts +266 -0
  17. package/src/__tests__/deployments.test.ts +105 -0
  18. package/src/__tests__/dev.test.tsx +1586 -0
  19. package/src/__tests__/generate.test.ts +238 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +85 -0
  22. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  23. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  24. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  25. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  26. package/src/__tests__/helpers/mock-bin.ts +36 -0
  27. package/src/__tests__/helpers/mock-cfetch.ts +278 -0
  28. package/src/__tests__/helpers/mock-console.ts +106 -0
  29. package/src/__tests__/helpers/mock-dialogs.ts +137 -0
  30. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  31. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  32. package/src/__tests__/helpers/mock-istty.ts +74 -0
  33. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  34. package/src/__tests__/helpers/mock-kv.ts +33 -0
  35. package/src/__tests__/helpers/mock-oauth-flow.ts +271 -0
  36. package/src/__tests__/helpers/mock-process.ts +34 -0
  37. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  38. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  39. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  40. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  41. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  42. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  43. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  44. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  45. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  46. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  47. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  48. package/src/__tests__/helpers/msw/index.ts +21 -0
  49. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  50. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  51. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  52. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  53. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  54. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  55. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  56. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  57. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  58. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  59. package/src/__tests__/https-options.test.ts +163 -0
  60. package/src/__tests__/index.test.ts +264 -149
  61. package/src/__tests__/init.test.ts +3147 -0
  62. package/src/__tests__/jest.setup.ts +198 -0
  63. package/src/__tests__/kv.test.ts +1788 -0
  64. package/src/__tests__/logger.test.ts +141 -0
  65. package/src/__tests__/logout.test.ts +47 -0
  66. package/src/__tests__/metrics.test.ts +493 -0
  67. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  68. package/src/__tests__/middleware.test.ts +750 -0
  69. package/src/__tests__/package-manager.test.ts +353 -0
  70. package/src/__tests__/pages-deployment-tail.test.ts +884 -0
  71. package/src/__tests__/pages.test.ts +2899 -0
  72. package/src/__tests__/parse.test.ts +330 -0
  73. package/src/__tests__/paths.test.ts +39 -0
  74. package/src/__tests__/publish.test.ts +7769 -0
  75. package/src/__tests__/pubsub.test.ts +490 -0
  76. package/src/__tests__/queues.test.ts +522 -0
  77. package/src/__tests__/r2.test.ts +393 -0
  78. package/src/__tests__/route.test.ts +45 -0
  79. package/src/__tests__/secret.test.ts +701 -0
  80. package/src/__tests__/tail.test.ts +905 -0
  81. package/src/__tests__/test-old-node-version.js +31 -0
  82. package/src/__tests__/tsconfig-sanity.ts +12 -0
  83. package/src/__tests__/tsconfig.json +8 -0
  84. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  85. package/src/__tests__/type-generation.test.ts +233 -0
  86. package/src/__tests__/user.test.ts +129 -0
  87. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  88. package/src/__tests__/validate-dev-props.test.ts +56 -0
  89. package/src/__tests__/version.test.ts +35 -0
  90. package/src/__tests__/whoami.test.tsx +183 -0
  91. package/src/__tests__/worker-namespace.test.ts +326 -0
  92. package/src/abort.d.ts +3 -0
  93. package/src/api/dev.ts +247 -0
  94. package/src/api/index.ts +2 -0
  95. package/src/bundle-reporter.ts +68 -0
  96. package/src/bundle.ts +823 -0
  97. package/src/cfetch/index.ts +158 -0
  98. package/src/cfetch/internal.ts +261 -0
  99. package/src/cli.ts +26 -8
  100. package/src/config/README.md +107 -0
  101. package/src/config/config.ts +275 -0
  102. package/src/config/diagnostics.ts +80 -0
  103. package/src/config/environment.ts +577 -0
  104. package/src/config/index.ts +358 -0
  105. package/src/config/validation-helpers.ts +597 -0
  106. package/src/config/validation.ts +2151 -0
  107. package/src/config-cache.ts +85 -0
  108. package/src/create-worker-preview.ts +282 -0
  109. package/src/create-worker-upload-form.ts +325 -0
  110. package/src/d1/backups.tsx +223 -0
  111. package/src/d1/constants.ts +2 -0
  112. package/src/d1/create.tsx +63 -0
  113. package/src/d1/delete.ts +56 -0
  114. package/src/d1/execute.tsx +323 -0
  115. package/src/d1/formatTimeAgo.ts +14 -0
  116. package/src/d1/index.ts +101 -0
  117. package/src/d1/list.tsx +48 -0
  118. package/src/d1/migrations/apply.tsx +197 -0
  119. package/src/d1/migrations/create.tsx +77 -0
  120. package/src/d1/migrations/helpers.ts +147 -0
  121. package/src/d1/migrations/index.ts +3 -0
  122. package/src/d1/migrations/list.tsx +79 -0
  123. package/src/d1/migrations/options.ts +18 -0
  124. package/src/d1/options.ts +22 -0
  125. package/src/d1/splitter.ts +161 -0
  126. package/src/d1/types.ts +24 -0
  127. package/src/d1/utils.ts +48 -0
  128. package/src/delete.ts +103 -0
  129. package/src/deployments.ts +83 -0
  130. package/src/deprecated/index.ts +145 -0
  131. package/src/dev/dev-vars.ts +39 -0
  132. package/src/dev/dev.tsx +614 -0
  133. package/src/dev/get-local-persistence-path.ts +31 -0
  134. package/src/dev/local.tsx +932 -0
  135. package/src/dev/remote.tsx +630 -0
  136. package/src/dev/start-server.ts +543 -0
  137. package/src/dev/use-esbuild.ts +204 -0
  138. package/src/dev/validate-dev-props.ts +31 -0
  139. package/src/dev-registry.ts +201 -0
  140. package/src/dev.tsx +929 -704
  141. package/src/dialogs.ts +136 -0
  142. package/src/dispatch-namespace.ts +207 -0
  143. package/src/docs/index.ts +97 -0
  144. package/src/durable.ts +102 -0
  145. package/src/entry.ts +329 -0
  146. package/src/environment-variables/factory.ts +88 -0
  147. package/src/environment-variables/misc-variables.ts +30 -0
  148. package/src/errors.ts +11 -0
  149. package/src/generate/index.ts +300 -0
  150. package/src/git-client.ts +135 -0
  151. package/src/global-wrangler-config-path.ts +26 -0
  152. package/src/https-options.ts +127 -0
  153. package/src/index.ts +694 -0
  154. package/src/init.ts +1023 -0
  155. package/src/inspect.ts +767 -0
  156. package/src/intl-polyfill.d.ts +139 -0
  157. package/src/is-ci.ts +14 -0
  158. package/src/is-interactive.ts +16 -0
  159. package/src/jest.d.ts +4 -0
  160. package/src/kv/helpers.ts +430 -0
  161. package/src/kv/index.ts +596 -0
  162. package/src/logger.ts +104 -0
  163. package/src/metrics/index.ts +5 -0
  164. package/src/metrics/metrics-config.ts +239 -0
  165. package/src/metrics/metrics-dispatcher.ts +96 -0
  166. package/src/metrics/metrics-usage-headers.ts +24 -0
  167. package/src/metrics/send-event.ts +97 -0
  168. package/src/miniflare-cli/README.md +30 -0
  169. package/src/miniflare-cli/assets.ts +250 -0
  170. package/src/miniflare-cli/index.ts +210 -0
  171. package/src/miniflare-cli/request-context.ts +40 -0
  172. package/src/miniflare-cli/tsconfig.json +9 -0
  173. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  174. package/src/miniflare-cli/types.ts +11 -0
  175. package/src/module-collection.ts +256 -53
  176. package/src/open-in-browser.ts +17 -0
  177. package/src/package-manager.ts +219 -0
  178. package/src/pages/build.ts +270 -0
  179. package/src/pages/constants.ts +18 -0
  180. package/src/pages/deployment-tails.ts +284 -0
  181. package/src/pages/deployments.tsx +84 -0
  182. package/src/pages/dev.ts +711 -0
  183. package/src/pages/errors.ts +66 -0
  184. package/src/pages/functions/buildPlugin.ts +107 -0
  185. package/src/pages/functions/buildWorker.ts +166 -0
  186. package/src/pages/functions/filepath-routing.test.ts +234 -0
  187. package/src/pages/functions/filepath-routing.ts +189 -0
  188. package/src/pages/functions/identifiers.ts +78 -0
  189. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  190. package/src/pages/functions/routes-consolidation.ts +73 -0
  191. package/src/pages/functions/routes-transformation.test.ts +282 -0
  192. package/src/pages/functions/routes-transformation.ts +115 -0
  193. package/src/pages/functions/routes-validation.test.ts +403 -0
  194. package/src/pages/functions/routes-validation.ts +202 -0
  195. package/src/pages/functions/routes.ts +151 -0
  196. package/src/pages/functions/tsconfig.json +8 -0
  197. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  198. package/src/pages/functions.ts +82 -0
  199. package/src/pages/hash.ts +13 -0
  200. package/src/pages/index.ts +102 -0
  201. package/src/pages/projects.tsx +167 -0
  202. package/src/pages/prompt-select-project.tsx +31 -0
  203. package/src/pages/publish.tsx +462 -0
  204. package/src/pages/types.ts +41 -0
  205. package/src/pages/upload.tsx +447 -0
  206. package/src/pages/utils.ts +23 -0
  207. package/src/parse.ts +286 -0
  208. package/src/paths.ts +71 -0
  209. package/src/proxy.ts +694 -0
  210. package/src/publish/index.ts +270 -0
  211. package/src/publish/publish.ts +1038 -0
  212. package/src/pubsub/index.ts +286 -0
  213. package/src/pubsub/pubsub-commands.ts +624 -0
  214. package/src/queues/cli/commands/consumer/add.ts +71 -0
  215. package/src/queues/cli/commands/consumer/index.ts +22 -0
  216. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  217. package/src/queues/cli/commands/create.ts +25 -0
  218. package/src/queues/cli/commands/delete.ts +26 -0
  219. package/src/queues/cli/commands/index.ts +36 -0
  220. package/src/queues/cli/commands/list.ts +25 -0
  221. package/src/queues/client.ts +135 -0
  222. package/src/queues/utils.ts +18 -0
  223. package/src/r2/constants.ts +4 -0
  224. package/src/r2/helpers.ts +131 -0
  225. package/src/r2/index.ts +289 -0
  226. package/src/routes.ts +140 -0
  227. package/src/secret/index.ts +379 -0
  228. package/src/selfsigned.d.ts +29 -0
  229. package/src/sites.ts +372 -0
  230. package/src/tail/createTail.ts +379 -0
  231. package/src/tail/filters.ts +277 -0
  232. package/src/tail/index.ts +213 -0
  233. package/src/tail/printing.ts +107 -0
  234. package/src/tsconfig-sanity.ts +16 -0
  235. package/src/type-generation.ts +181 -0
  236. package/src/update-check.ts +19 -0
  237. package/src/user/access.ts +68 -0
  238. package/src/user/auth-variables.ts +113 -0
  239. package/src/user/choose-account.tsx +39 -0
  240. package/src/user/generate-auth-url.ts +33 -0
  241. package/src/user/generate-random-state.ts +16 -0
  242. package/src/user/index.ts +2 -0
  243. package/src/user/user.ts +1232 -0
  244. package/src/utils/collectKeyValues.ts +14 -0
  245. package/src/whoami.ts +135 -0
  246. package/src/worker.ts +251 -0
  247. package/src/yargs-types.ts +24 -0
  248. package/src/zones.ts +191 -0
  249. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  250. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  251. package/templates/__tests__/tsconfig.json +8 -0
  252. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  253. package/templates/checked-fetch.js +17 -0
  254. package/templates/d1-beta-facade.js +241 -0
  255. package/templates/facade.d.ts +14 -0
  256. package/templates/first-party-worker-module-facade.ts +19 -0
  257. package/templates/format-dev-errors.ts +33 -0
  258. package/templates/gitignore +171 -0
  259. package/templates/init-tests/test-jest-new-worker.js +23 -0
  260. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  261. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  262. package/templates/middleware/common.ts +62 -0
  263. package/templates/middleware/loader-modules.ts +82 -0
  264. package/templates/middleware/loader-sw.ts +233 -0
  265. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  266. package/templates/middleware/middleware-pretty-error.ts +40 -0
  267. package/templates/middleware/middleware-scheduled.ts +15 -0
  268. package/templates/new-worker-scheduled.js +17 -0
  269. package/templates/new-worker-scheduled.ts +32 -0
  270. package/templates/new-worker.js +15 -0
  271. package/templates/new-worker.ts +30 -0
  272. package/templates/no-op-worker.js +10 -0
  273. package/templates/pages-dev-pipeline.ts +31 -0
  274. package/templates/pages-dev-util.ts +52 -0
  275. package/templates/pages-shim.ts +12 -0
  276. package/templates/pages-template-plugin.ts +176 -0
  277. package/templates/pages-template-worker.ts +189 -0
  278. package/templates/serve-static-assets.ts +65 -0
  279. package/templates/service-bindings-module-facade.js +54 -0
  280. package/templates/service-bindings-sw-facade.js +42 -0
  281. package/templates/tsconfig-sanity.ts +11 -0
  282. package/templates/tsconfig.init.json +106 -0
  283. package/templates/tsconfig.json +8 -0
  284. package/templates/tsconfig.tsbuildinfo +1 -0
  285. package/wrangler-dist/cli.d.ts +906 -0
  286. package/wrangler-dist/cli.js +133533 -113146
  287. package/wrangler-dist/wasm-sync.wasm +0 -0
  288. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  289. package/src/__tests__/mock-cfetch.js +0 -46
  290. package/src/api/form_data.ts +0 -141
  291. package/src/api/inspect.ts +0 -441
  292. package/src/api/preview.ts +0 -128
  293. package/src/api/worker.ts +0 -161
  294. package/src/cfetch.ts +0 -72
  295. package/src/config.ts +0 -122
  296. package/src/dialogs.tsx +0 -85
  297. package/src/index.tsx +0 -1858
  298. package/src/kv.tsx +0 -211
  299. package/src/pages.tsx +0 -843
  300. package/src/publish.ts +0 -346
  301. package/src/sites.tsx +0 -115
  302. package/src/tail.tsx +0 -71
  303. package/src/user.tsx +0 -1029
  304. package/static-asset-facade.js +0 -47
  305. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  306. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  307. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  308. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  309. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  310. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  311. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  312. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  313. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  314. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  315. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  316. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  317. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  318. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  319. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  320. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  321. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  322. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  323. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  324. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  325. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  326. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  327. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  328. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  329. package/vendor/wrangler-mime/LICENSE +0 -21
  330. package/vendor/wrangler-mime/Mime.js +0 -97
  331. package/vendor/wrangler-mime/README.md +0 -187
  332. package/vendor/wrangler-mime/cli.js +0 -46
  333. package/vendor/wrangler-mime/index.js +0 -4
  334. package/vendor/wrangler-mime/lite.js +0 -4
  335. package/vendor/wrangler-mime/package.json +0 -52
  336. package/vendor/wrangler-mime/types/other.js +0 -1
  337. package/vendor/wrangler-mime/types/standard.js +0 -1
  338. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,3147 @@
1
+ import * as fs from "node:fs";
2
+ import path from "node:path";
3
+ import * as TOML from "@iarna/toml";
4
+ import { execa, execaSync } from "execa";
5
+ import { rest } from "msw";
6
+ import { parseConfigFileTextToJson } from "typescript";
7
+ import { FormData } from "undici";
8
+ import { version as wranglerVersion } from "../../package.json";
9
+ import { fetchDashboardScript } from "../cfetch/internal";
10
+ import { getPackageManager } from "../package-manager";
11
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
12
+ import { mockConsoleMethods } from "./helpers/mock-console";
13
+ import { clearDialogs, mockConfirm, mockSelect } from "./helpers/mock-dialogs";
14
+ import { useMockIsTTY } from "./helpers/mock-istty";
15
+ import { msw } from "./helpers/msw";
16
+ import { runInTempDir } from "./helpers/run-in-tmp";
17
+ import { runWrangler } from "./helpers/run-wrangler";
18
+ import type { PackageManager } from "../package-manager";
19
+
20
+ /**
21
+ * An expectation matcher for the minimal generated wrangler.toml.
22
+ */
23
+ const MINIMAL_WRANGLER_TOML = {
24
+ compatibility_date: expect.any(String),
25
+ name: expect.stringContaining("wrangler-tests"),
26
+ main: "src/index.ts",
27
+ };
28
+
29
+ describe("init", () => {
30
+ let mockPackageManager: PackageManager;
31
+ runInTempDir();
32
+ const { setIsTTY } = useMockIsTTY();
33
+
34
+ beforeEach(() => {
35
+ setIsTTY(true);
36
+
37
+ mockPackageManager = {
38
+ cwd: process.cwd(),
39
+ // @ts-expect-error we're making a fake package manager here
40
+ type: "mockpm",
41
+ addDevDeps: jest.fn(),
42
+ install: jest.fn(),
43
+ };
44
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
45
+ });
46
+
47
+ afterEach(() => {
48
+ clearDialogs();
49
+ msw.resetHandlers();
50
+ msw.restoreHandlers();
51
+ });
52
+
53
+ const std = mockConsoleMethods();
54
+
55
+ describe("options", () => {
56
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
57
+ await runWrangler("init --yes");
58
+
59
+ checkFiles({
60
+ items: {
61
+ "./src/index.js": false,
62
+ "./src/index.ts": true,
63
+ "./tsconfig.json": true,
64
+ "./package.json": true,
65
+ "./wrangler.toml": true,
66
+ },
67
+ });
68
+
69
+ expect(std.out).toMatchInlineSnapshot(`
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
77
+
78
+ To start developing your Worker, run \`npm start\`
79
+ To publish your Worker to the Internet, run \`npm run deploy\`"
80
+ `);
81
+ expect(std.err).toMatchInlineSnapshot(`""`);
82
+ expect(std.warn).toMatchInlineSnapshot(`""`);
83
+ });
84
+
85
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
86
+ await runWrangler("init my-worker --yes");
87
+
88
+ checkFiles({
89
+ items: {
90
+ "./my-worker/src/index.js": false,
91
+ "./my-worker/src/index.ts": true,
92
+ "./my-worker/tsconfig.json": true,
93
+ "./my-worker/package.json": true,
94
+ "./my-worker/wrangler.toml": wranglerToml({
95
+ ...MINIMAL_WRANGLER_TOML,
96
+ name: "my-worker",
97
+ }),
98
+ },
99
+ });
100
+
101
+ expect(std.out).toMatchInlineSnapshot(`
102
+ "✨ Created my-worker/wrangler.toml
103
+ ✨ Initialized git repository at my-worker
104
+ ✨ Created my-worker/package.json
105
+ ✨ Created my-worker/tsconfig.json
106
+ ✨ Created my-worker/src/index.ts
107
+ ✨ Created my-worker/src/index.test.ts
108
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest 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
+ `);
113
+ expect(std.err).toMatchInlineSnapshot(`""`);
114
+ expect(std.warn).toMatchInlineSnapshot(`""`);
115
+ });
116
+
117
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
118
+ await runWrangler("init -y");
119
+
120
+ checkFiles({
121
+ items: {
122
+ "./src/index.js": false,
123
+ "./src/index.ts": true,
124
+ "./tsconfig.json": true,
125
+ "./package.json": true,
126
+ "./wrangler.toml": true,
127
+ },
128
+ });
129
+
130
+ 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
+ ✨ Created src/index.test.ts
140
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
141
+
142
+ To start developing your Worker, run \`npm start\`
143
+ To publish your Worker to the Internet, run \`npm run deploy\`",
144
+ "warn": "",
145
+ }
146
+ `);
147
+ });
148
+
149
+ it("should error if `--type javascript` is used", async () => {
150
+ await expect(
151
+ runWrangler("init --type javascript")
152
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
153
+ `"The --type option is no longer supported."`
154
+ );
155
+ });
156
+
157
+ it("should error if `--type rust` is used", async () => {
158
+ await expect(
159
+ runWrangler("init --type rust")
160
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
161
+ `"The --type option is no longer supported."`
162
+ );
163
+ });
164
+
165
+ it("should error if `--type webpack` is used", async () => {
166
+ await expect(runWrangler("init --type webpack")).rejects
167
+ .toThrowErrorMatchingInlineSnapshot(`
168
+ "The --type option is no longer supported.
169
+ If you wish to use webpack then you will need to create a custom build."
170
+ `);
171
+ });
172
+
173
+ it("should error if `--site` is used", async () => {
174
+ await expect(runWrangler("init --site")).rejects
175
+ .toThrowErrorMatchingInlineSnapshot(`
176
+ "The --site option is no longer supported.
177
+ If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
178
+
179
+ \`\`\`
180
+ git clone --depth=1 --branch=wrangler2 https://github.com/cloudflare/worker-sites-template my-site
181
+ cd my-site
182
+ \`\`\`
183
+
184
+ Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
185
+ Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
186
+ `);
187
+ });
188
+ });
189
+
190
+ describe("wrangler.toml", () => {
191
+ it("should create a wrangler.toml", async () => {
192
+ mockConfirm(
193
+ {
194
+ text: "Would you like to use git to manage this Worker?",
195
+ result: false,
196
+ },
197
+ {
198
+ text: "No package.json found. Would you like to create one?",
199
+ result: false,
200
+ }
201
+ );
202
+ await runWrangler("init");
203
+ checkFiles({
204
+ items: {
205
+ "wrangler.toml": wranglerToml({
206
+ compatibility_date: expect.any(String),
207
+ name: expect.stringContaining("wrangler-tests"),
208
+ }),
209
+ "package.json": false,
210
+ "tsconfig.json": false,
211
+ },
212
+ });
213
+
214
+ expect(std).toMatchInlineSnapshot(`
215
+ Object {
216
+ "debug": "",
217
+ "err": "",
218
+ "out": "✨ Created wrangler.toml",
219
+ "warn": "",
220
+ }
221
+ `);
222
+ });
223
+
224
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
225
+ mockConfirm(
226
+ {
227
+ text: "Would you like to use git to manage this Worker?",
228
+ result: false,
229
+ },
230
+ {
231
+ text: "No package.json found. Would you like to create one?",
232
+ result: false,
233
+ }
234
+ );
235
+ await runWrangler("init my-worker");
236
+
237
+ checkFiles({
238
+ items: {
239
+ "my-worker/wrangler.toml": wranglerToml({
240
+ compatibility_date: expect.any(String),
241
+ name: "my-worker",
242
+ }),
243
+ "my-worker/package.json": false,
244
+ "my-worker/tsconfig.json": false,
245
+ },
246
+ });
247
+
248
+ expect(std).toMatchInlineSnapshot(`
249
+ Object {
250
+ "debug": "",
251
+ "err": "",
252
+ "out": "✨ Created my-worker/wrangler.toml",
253
+ "warn": "",
254
+ }
255
+ `);
256
+ });
257
+
258
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
259
+ writeFiles({
260
+ items: {
261
+ "wrangler.toml": wranglerToml({
262
+ // use a fake value to make sure the file is not overwritten
263
+ compatibility_date: "something-else",
264
+ }),
265
+ },
266
+ });
267
+
268
+ mockConfirm({
269
+ text: "Do you want to continue initializing this project?",
270
+ result: false,
271
+ });
272
+
273
+ await runWrangler("init");
274
+ expect(std.warn).toContain("wrangler.toml already exists!");
275
+
276
+ checkFiles({
277
+ items: {
278
+ "wrangler.toml": wranglerToml({
279
+ compatibility_date: "something-else",
280
+ }),
281
+ "package.json": false,
282
+ "tsconfig.json": false,
283
+ },
284
+ });
285
+
286
+ expect(std).toMatchInlineSnapshot(`
287
+ Object {
288
+ "debug": "",
289
+ "err": "",
290
+ "out": "",
291
+ "warn": "▲ [WARNING] wrangler.toml already exists!
292
+
293
+ ",
294
+ }
295
+ `);
296
+ });
297
+
298
+ it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
299
+ writeFiles({
300
+ items: {
301
+ "path/to/worker/wrangler.toml": wranglerToml({
302
+ compatibility_date: "something-else",
303
+ }),
304
+ },
305
+ });
306
+ mockConfirm({
307
+ text: "Do you want to continue initializing this project?",
308
+ result: false,
309
+ });
310
+
311
+ await runWrangler("init path/to/worker");
312
+
313
+ expect(std.warn).toContain("wrangler.toml already exists!");
314
+ checkFiles({
315
+ items: {
316
+ "path/to/worker/wrangler.toml": wranglerToml({
317
+ compatibility_date: "something-else",
318
+ }),
319
+ "package.json": false,
320
+ "tsconfig.json": false,
321
+ },
322
+ });
323
+ expect(std).toMatchInlineSnapshot(`
324
+ Object {
325
+ "debug": "",
326
+ "err": "",
327
+ "out": "",
328
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
329
+
330
+ ",
331
+ }
332
+ `);
333
+ });
334
+
335
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
336
+ writeFiles({
337
+ items: {
338
+ "wrangler.toml": wranglerToml({
339
+ compatibility_date: "something-else",
340
+ }),
341
+ },
342
+ });
343
+ mockConfirm(
344
+ {
345
+ text: "Do you want to continue initializing this project?",
346
+ result: true,
347
+ },
348
+ {
349
+ text: "Would you like to use git to manage this Worker?",
350
+ result: true,
351
+ },
352
+ {
353
+ text: "No package.json found. Would you like to create one?",
354
+ result: true,
355
+ },
356
+ {
357
+ text: "Would you like to use TypeScript?",
358
+ result: true,
359
+ }
360
+ );
361
+
362
+ mockSelect({
363
+ text: `Would you like to create a Worker at ${path.join(
364
+ "src",
365
+ "index.ts"
366
+ )}?`,
367
+ result: "fetch",
368
+ });
369
+ mockConfirm({
370
+ text: "Would you like us to write your first test with Vitest?",
371
+ result: true,
372
+ });
373
+
374
+ await runWrangler("init");
375
+
376
+ checkFiles({
377
+ items: {
378
+ "wrangler.toml": wranglerToml({
379
+ compatibility_date: "something-else",
380
+ }),
381
+ },
382
+ });
383
+ });
384
+
385
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
386
+ writeFiles({
387
+ items: {
388
+ "wrangler.toml": wranglerToml({
389
+ compatibility_date: "something-else",
390
+ }),
391
+ },
392
+ });
393
+ mockConfirm(
394
+ {
395
+ text: "Do you want to continue initializing this project?",
396
+ result: true,
397
+ },
398
+ {
399
+ text: "Would you like to use git to manage this Worker?",
400
+ result: false,
401
+ },
402
+ {
403
+ text: "No package.json found. Would you like to create one?",
404
+ result: false,
405
+ }
406
+ );
407
+
408
+ await runWrangler("init");
409
+
410
+ expect(std.warn).toContain("wrangler.toml already exists!");
411
+ checkFiles({
412
+ items: {
413
+ "wrangler.toml": wranglerToml({
414
+ compatibility_date: "something-else",
415
+ }),
416
+ },
417
+ });
418
+ expect(std).toMatchInlineSnapshot(`
419
+ Object {
420
+ "debug": "",
421
+ "err": "",
422
+ "out": "",
423
+ "warn": "▲ [WARNING] wrangler.toml already exists!
424
+
425
+ ",
426
+ }
427
+ `);
428
+ });
429
+
430
+ it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
431
+ writeFiles({
432
+ items: {
433
+ "wrangler.toml": wranglerToml({
434
+ compatibility_date: "something-else",
435
+ }),
436
+ },
437
+ });
438
+ mockConfirm(
439
+ {
440
+ text: "Do you want to continue initializing this project?",
441
+ result: true,
442
+ },
443
+ {
444
+ text: "Would you like to use git to manage this Worker?",
445
+ result: true,
446
+ },
447
+
448
+ {
449
+ text: "No package.json found. Would you like to create one?",
450
+ result: true,
451
+ },
452
+ {
453
+ text: "Would you like to use TypeScript?",
454
+ result: true,
455
+ }
456
+ );
457
+ mockSelect({
458
+ text: `Would you like to create a Worker at ${path.join(
459
+ "src",
460
+ "index.ts"
461
+ )}?`,
462
+ result: "scheduled",
463
+ });
464
+
465
+ mockConfirm({
466
+ text: "Would you like us to write your first test with Vitest?",
467
+ result: true,
468
+ });
469
+ await runWrangler("init");
470
+
471
+ checkFiles({
472
+ items: {
473
+ "wrangler.toml": wranglerToml({
474
+ compatibility_date: "something-else",
475
+ }),
476
+ },
477
+ });
478
+ });
479
+
480
+ it("should add a Cron Trigger to wrangler.toml when creating a Scheduled Worker, but only if wrangler.toml is being created during init", async () => {
481
+ mockConfirm(
482
+ {
483
+ text: "Would you like to use git to manage this Worker?",
484
+ result: true,
485
+ },
486
+ {
487
+ text: "No package.json found. Would you like to create one?",
488
+ result: true,
489
+ },
490
+ {
491
+ text: "Would you like to use TypeScript?",
492
+ result: true,
493
+ }
494
+ );
495
+ mockSelect({
496
+ text: `Would you like to create a Worker at ${path.join(
497
+ "src",
498
+ "index.ts"
499
+ )}?`,
500
+ result: "scheduled",
501
+ });
502
+ mockConfirm({
503
+ text: "Would you like us to write your first test with Vitest?",
504
+ result: true,
505
+ });
506
+ await runWrangler("init");
507
+
508
+ checkFiles({
509
+ items: {
510
+ "wrangler.toml": wranglerToml({
511
+ ...MINIMAL_WRANGLER_TOML,
512
+ triggers: { crons: ["1 * * * *"] },
513
+ }),
514
+ },
515
+ });
516
+ });
517
+ });
518
+
519
+ describe("git init", () => {
520
+ it("should offer to initialize a git repository", async () => {
521
+ mockConfirm(
522
+ {
523
+ text: "Would you like to use git to manage this Worker?",
524
+ result: true,
525
+ },
526
+ {
527
+ text: "No package.json found. Would you like to create one?",
528
+ result: false,
529
+ }
530
+ );
531
+
532
+ await runWrangler("init");
533
+
534
+ checkFiles({
535
+ items: {
536
+ "wrangler.toml": wranglerToml({
537
+ compatibility_date: expect.any(String),
538
+ name: expect.stringContaining("wrangler-tests"),
539
+ }),
540
+ ".git": { items: {} },
541
+ ".gitignore": true,
542
+ },
543
+ });
544
+ expect(std).toMatchInlineSnapshot(`
545
+ Object {
546
+ "debug": "",
547
+ "err": "",
548
+ "out": "✨ Created wrangler.toml
549
+ ✨ Initialized git repository",
550
+ "warn": "",
551
+ }
552
+ `);
553
+ expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
554
+ getDefaultBranchName()
555
+ );
556
+ });
557
+
558
+ it("should not offer to initialize a git repo if it's already inside one", async () => {
559
+ await execa("git", ["init"]);
560
+ setWorkingDirectory("some-folder");
561
+
562
+ await runWrangler("init -y");
563
+
564
+ checkFiles({
565
+ items: {
566
+ "wrangler.toml": wranglerToml({
567
+ ...MINIMAL_WRANGLER_TOML,
568
+ name: "some-folder",
569
+ }),
570
+ ".git": { items: {} },
571
+ ".gitignore": false,
572
+ },
573
+ });
574
+
575
+ // Note the lack of "✨ Initialized git repository" in the log
576
+ expect(std).toMatchInlineSnapshot(`
577
+ Object {
578
+ "debug": "",
579
+ "err": "",
580
+ "out": "✨ Created wrangler.toml
581
+ ✨ Created package.json
582
+ ✨ Created tsconfig.json
583
+ ✨ Created src/index.ts
584
+ ✨ Created src/index.test.ts
585
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
586
+
587
+ To start developing your Worker, run \`npm start\`
588
+ To publish your Worker to the Internet, run \`npm run deploy\`",
589
+ "warn": "",
590
+ }
591
+ `);
592
+ });
593
+
594
+ it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
595
+ fs.mkdirSync("path/to/worker", { recursive: true });
596
+ await execa("git", ["init"], { cwd: "path/to/worker" });
597
+ expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
598
+
599
+ await runWrangler("init path/to/worker/my-worker -y");
600
+
601
+ // Note the lack of "✨ Initialized git repository" in the log
602
+ expect(std).toMatchInlineSnapshot(`
603
+ Object {
604
+ "debug": "",
605
+ "err": "",
606
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
607
+ ✨ Created path/to/worker/my-worker/package.json
608
+ ✨ Created path/to/worker/my-worker/tsconfig.json
609
+ ✨ Created path/to/worker/my-worker/src/index.ts
610
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
611
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
612
+
613
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
614
+ To publish your Worker to the Internet, run \`npm run deploy\`",
615
+ "warn": "",
616
+ }
617
+ `);
618
+ });
619
+
620
+ // I... don't know how to test this lol
621
+ it.todo(
622
+ "should not offer to initialize a git repo if git is not installed"
623
+ );
624
+
625
+ it("should initialize git repo with the user's default branch", async () => {
626
+ mockConfirm(
627
+ {
628
+ text: "Would you like to use git to manage this Worker?",
629
+ result: true,
630
+ },
631
+ {
632
+ text: "No package.json found. Would you like to create one?",
633
+ result: false,
634
+ }
635
+ );
636
+ await runWrangler("init");
637
+ expect(std).toMatchInlineSnapshot(`
638
+ Object {
639
+ "debug": "",
640
+ "err": "",
641
+ "out": "✨ Created wrangler.toml
642
+ ✨ Initialized git repository",
643
+ "warn": "",
644
+ }
645
+ `);
646
+
647
+ expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
648
+ `refs/heads/${getDefaultBranchName()}`
649
+ );
650
+ });
651
+ });
652
+
653
+ describe("package.json", () => {
654
+ it("should create a package.json if none is found and user confirms", async () => {
655
+ mockConfirm(
656
+ {
657
+ text: "Would you like to use git to manage this Worker?",
658
+ result: false,
659
+ },
660
+ {
661
+ text: "No package.json found. Would you like to create one?",
662
+ result: true,
663
+ },
664
+ {
665
+ text: "Would you like to use TypeScript?",
666
+ result: false,
667
+ }
668
+ );
669
+ mockSelect({
670
+ text: `Would you like to create a Worker at ${path.join(
671
+ "src",
672
+ "index.js"
673
+ )}?`,
674
+ result: "none",
675
+ });
676
+
677
+ await runWrangler("init");
678
+
679
+ checkFiles({
680
+ items: {
681
+ "package.json": {
682
+ contents: expect.objectContaining({
683
+ name: expect.stringContaining("wrangler-tests"),
684
+ version: "0.0.0",
685
+ devDependencies: {
686
+ wrangler: expect.any(String),
687
+ },
688
+ }),
689
+ },
690
+ "tsconfig.json": false,
691
+ },
692
+ });
693
+ expect(mockPackageManager.install).toHaveBeenCalled();
694
+ expect(std).toMatchInlineSnapshot(`
695
+ Object {
696
+ "debug": "",
697
+ "err": "",
698
+ "out": "✨ Created wrangler.toml
699
+ ✨ Created package.json",
700
+ "warn": "",
701
+ }
702
+ `);
703
+ });
704
+
705
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
706
+ mockConfirm(
707
+ {
708
+ text: "Would you like to use git to manage this Worker?",
709
+ result: false,
710
+ },
711
+ {
712
+ text: "No package.json found. Would you like to create one?",
713
+ result: true,
714
+ },
715
+ {
716
+ text: "Would you like to use TypeScript?",
717
+ result: false,
718
+ }
719
+ );
720
+ mockSelect({
721
+ text: `Would you like to create a Worker at ${path.join(
722
+ "my-worker",
723
+ "src",
724
+ "index.js"
725
+ )}?`,
726
+ result: "none",
727
+ });
728
+
729
+ await runWrangler("init my-worker");
730
+
731
+ checkFiles({
732
+ items: {
733
+ "my-worker/package.json": {
734
+ contents: expect.objectContaining({
735
+ name: "my-worker",
736
+ version: "0.0.0",
737
+ devDependencies: {
738
+ wrangler: expect.any(String),
739
+ },
740
+ }),
741
+ },
742
+ "my-worker/tsconfig.json": false,
743
+ },
744
+ });
745
+ expect(std).toMatchInlineSnapshot(`
746
+ Object {
747
+ "debug": "",
748
+ "err": "",
749
+ "out": "✨ Created my-worker/wrangler.toml
750
+ ✨ Created my-worker/package.json",
751
+ "warn": "",
752
+ }
753
+ `);
754
+ });
755
+
756
+ it("should not touch an existing package.json in the same directory", async () => {
757
+ mockConfirm(
758
+ {
759
+ text: "Would you like to use git to manage this Worker?",
760
+ result: false,
761
+ },
762
+ {
763
+ text: "Would you like to install wrangler into package.json?",
764
+ result: false,
765
+ },
766
+ {
767
+ text: "Would you like to use TypeScript?",
768
+ result: false,
769
+ }
770
+ );
771
+ mockSelect({
772
+ text: `Would you like to create a Worker at ${path.join(
773
+ "src",
774
+ "index.js"
775
+ )}?`,
776
+ result: "none",
777
+ });
778
+ writeFiles({
779
+ items: {
780
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
781
+ },
782
+ });
783
+
784
+ await runWrangler("init");
785
+
786
+ checkFiles({
787
+ items: {
788
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
789
+ },
790
+ });
791
+ expect(std).toMatchInlineSnapshot(`
792
+ Object {
793
+ "debug": "",
794
+ "err": "",
795
+ "out": "✨ Created wrangler.toml",
796
+ "warn": "",
797
+ }
798
+ `);
799
+ });
800
+
801
+ it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
802
+ mockConfirm(
803
+ {
804
+ text: "Would you like to use git to manage this Worker?",
805
+ result: false,
806
+ },
807
+ {
808
+ text: "No package.json found. Would you like to create one?",
809
+ result: true,
810
+ },
811
+ {
812
+ text: "Would you like to use TypeScript?",
813
+ result: false,
814
+ }
815
+ );
816
+ mockSelect({
817
+ text: `Would you like to create a Worker at ${path.join(
818
+ "path",
819
+ "to",
820
+ "worker",
821
+ "my-worker",
822
+ "src",
823
+ "index.js"
824
+ )}?`,
825
+ result: "none",
826
+ });
827
+ writeFiles({
828
+ items: {
829
+ "path/to/worker/package.json": {
830
+ contents: { name: "test", version: "1.0.0" },
831
+ },
832
+ },
833
+ });
834
+
835
+ await runWrangler("init path/to/worker/my-worker");
836
+
837
+ checkFiles({
838
+ items: {
839
+ "path/to/worker/package.json": {
840
+ contents: { name: "test", version: "1.0.0" },
841
+ },
842
+ },
843
+ });
844
+ expect(std).toMatchInlineSnapshot(`
845
+ Object {
846
+ "debug": "",
847
+ "err": "",
848
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
849
+ ✨ Created path/to/worker/my-worker/package.json",
850
+ "warn": "",
851
+ }
852
+ `);
853
+ });
854
+
855
+ it("should offer to install wrangler into an existing package.json", async () => {
856
+ mockConfirm(
857
+ {
858
+ text: "Would you like to use git to manage this Worker?",
859
+ result: false,
860
+ },
861
+ {
862
+ text: "Would you like to install wrangler into package.json?",
863
+ result: true,
864
+ },
865
+ {
866
+ text: "Would you like to use TypeScript?",
867
+ result: false,
868
+ }
869
+ );
870
+ mockSelect({
871
+ text: `Would you like to create a Worker at ${path.join(
872
+ "src",
873
+ "index.js"
874
+ )}?`,
875
+ result: "none",
876
+ });
877
+ writeFiles({
878
+ items: {
879
+ "package.json": {
880
+ contents: { name: "test", version: "1.0.0" },
881
+ },
882
+ },
883
+ });
884
+
885
+ await runWrangler("init");
886
+
887
+ checkFiles({
888
+ items: {
889
+ "package.json": {
890
+ contents: { name: "test", version: "1.0.0" },
891
+ },
892
+ },
893
+ });
894
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
895
+ `wrangler@${wranglerVersion}`
896
+ );
897
+ expect(std).toMatchInlineSnapshot(`
898
+ Object {
899
+ "debug": "",
900
+ "err": "",
901
+ "out": "✨ Created wrangler.toml
902
+ ✨ Installed wrangler into devDependencies",
903
+ "warn": "",
904
+ }
905
+ `);
906
+ });
907
+
908
+ it("should offer to install wrangler into a package.json relative to the target directory, if no name is provided", async () => {
909
+ mockConfirm(
910
+ {
911
+ text: "Would you like to use git to manage this Worker?",
912
+ result: false,
913
+ },
914
+ {
915
+ text: `Would you like to install wrangler into ${path.join(
916
+ "..",
917
+ "package.json"
918
+ )}?`,
919
+ result: true,
920
+ },
921
+ {
922
+ text: "Would you like to use TypeScript?",
923
+ result: false,
924
+ }
925
+ );
926
+ mockSelect({
927
+ text: `Would you like to create a Worker at ${path.join(
928
+ "src",
929
+ "index.js"
930
+ )}?`,
931
+ result: "none",
932
+ });
933
+ writeFiles({
934
+ items: {
935
+ "path/to/worker/package.json": {
936
+ contents: { name: "test", version: "1.0.0" },
937
+ },
938
+ },
939
+ });
940
+ setWorkingDirectory("path/to/worker/my-worker");
941
+
942
+ await runWrangler("init");
943
+
944
+ setWorkingDirectory("../../../..");
945
+ checkFiles({
946
+ items: {
947
+ "path/to/worker/package.json": {
948
+ contents: { name: "test", version: "1.0.0" },
949
+ },
950
+ "path/to/worker/my-worker/package.json": false,
951
+ },
952
+ });
953
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
954
+ `wrangler@${wranglerVersion}`
955
+ );
956
+ expect(mockPackageManager.cwd).toBe(process.cwd());
957
+ expect(std).toMatchInlineSnapshot(`
958
+ Object {
959
+ "debug": "",
960
+ "err": "",
961
+ "out": "✨ Created wrangler.toml
962
+ ✨ Installed wrangler into devDependencies",
963
+ "warn": "",
964
+ }
965
+ `);
966
+ });
967
+
968
+ it("should not touch an existing package.json in an ancestor directory", async () => {
969
+ mockConfirm(
970
+ {
971
+ text: "Would you like to use git to manage this Worker?",
972
+ result: false,
973
+ },
974
+ {
975
+ text: `Would you like to install wrangler into ${path.join(
976
+ "..",
977
+ "..",
978
+ "package.json"
979
+ )}?`,
980
+ result: false,
981
+ },
982
+ {
983
+ text: "Would you like to use TypeScript?",
984
+ result: false,
985
+ }
986
+ );
987
+ mockSelect({
988
+ text: `Would you like to create a Worker at ${path.join(
989
+ "src",
990
+ "index.js"
991
+ )}?`,
992
+ result: "none",
993
+ });
994
+ writeFiles({
995
+ items: {
996
+ "package.json": {
997
+ contents: { name: "test", version: "1.0.0" },
998
+ },
999
+ },
1000
+ });
1001
+ setWorkingDirectory("./sub-1/sub-2");
1002
+
1003
+ await runWrangler("init");
1004
+
1005
+ checkFiles({
1006
+ items: {
1007
+ "package.json": false,
1008
+ "../../package.json": {
1009
+ contents: { name: "test", version: "1.0.0" },
1010
+ },
1011
+ },
1012
+ });
1013
+ expect(std).toMatchInlineSnapshot(`
1014
+ Object {
1015
+ "debug": "",
1016
+ "err": "",
1017
+ "out": "✨ Created wrangler.toml",
1018
+ "warn": "",
1019
+ }
1020
+ `);
1021
+ });
1022
+ });
1023
+
1024
+ describe("typescript", () => {
1025
+ it("should offer to create a worker in a non-typescript project", async () => {
1026
+ mockConfirm(
1027
+ {
1028
+ text: "Would you like to use git to manage this Worker?",
1029
+ result: false,
1030
+ },
1031
+ {
1032
+ text: "Would you like to install wrangler into package.json?",
1033
+ result: false,
1034
+ },
1035
+ {
1036
+ text: "Would you like to use TypeScript?",
1037
+ result: false,
1038
+ }
1039
+ );
1040
+ mockSelect({
1041
+ text: `Would you like to create a Worker at ${path.join(
1042
+ "src",
1043
+ "index.js"
1044
+ )}?`,
1045
+ result: "fetch",
1046
+ });
1047
+ mockConfirm({
1048
+ text: "Would you like us to write your first test?",
1049
+ result: false,
1050
+ });
1051
+ writeFiles({
1052
+ items: {
1053
+ "package.json": {
1054
+ contents: { name: "test", version: "1.0.0" },
1055
+ },
1056
+ },
1057
+ });
1058
+
1059
+ await runWrangler("init");
1060
+
1061
+ checkFiles({
1062
+ items: {
1063
+ "wrangler.toml": wranglerToml({
1064
+ ...MINIMAL_WRANGLER_TOML,
1065
+ main: "src/index.js",
1066
+ }),
1067
+ "src/index.js": true,
1068
+ "src/index.ts": false,
1069
+ },
1070
+ });
1071
+ expect(std).toMatchInlineSnapshot(`
1072
+ Object {
1073
+ "debug": "",
1074
+ "err": "",
1075
+ "out": "✨ Created wrangler.toml
1076
+ ✨ Created src/index.js
1077
+
1078
+ To start developing your Worker, run \`npx wrangler dev\`
1079
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1080
+ "warn": "",
1081
+ }
1082
+ `);
1083
+ });
1084
+
1085
+ it("should offer to create a worker in a typescript project", async () => {
1086
+ mockConfirm(
1087
+ {
1088
+ text: "Would you like to use git to manage this Worker?",
1089
+ result: false,
1090
+ },
1091
+ {
1092
+ text: "Would you like to install wrangler into package.json?",
1093
+ result: false,
1094
+ },
1095
+ {
1096
+ text: "Would you like to use TypeScript?",
1097
+ result: true,
1098
+ }
1099
+ );
1100
+ mockSelect({
1101
+ text: `Would you like to create a Worker at ${path.join(
1102
+ "src",
1103
+ "index.ts"
1104
+ )}?`,
1105
+ result: "fetch",
1106
+ });
1107
+ mockConfirm({
1108
+ text: "Would you like us to write your first test with Vitest?",
1109
+ result: true,
1110
+ });
1111
+ writeFiles({
1112
+ items: {
1113
+ "package.json": {
1114
+ contents: { name: "test", version: "1.0.0" },
1115
+ },
1116
+ },
1117
+ });
1118
+
1119
+ await runWrangler("init");
1120
+
1121
+ checkFiles({
1122
+ items: {
1123
+ "src/index.js": false,
1124
+ "src/index.ts": true,
1125
+ },
1126
+ });
1127
+ expect(std).toMatchInlineSnapshot(`
1128
+ Object {
1129
+ "debug": "",
1130
+ "err": "",
1131
+ "out": "✨ Created wrangler.toml
1132
+ ✨ Created tsconfig.json
1133
+ ✨ Created src/index.ts
1134
+ ✨ Created src/index.test.ts
1135
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1136
+
1137
+ To start developing your Worker, run \`npx wrangler dev\`
1138
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1139
+ "warn": "",
1140
+ }
1141
+ `);
1142
+ });
1143
+
1144
+ it("should add scripts for a typescript project with .ts extension", async () => {
1145
+ mockConfirm(
1146
+ {
1147
+ text: "Would you like to use git to manage this Worker?",
1148
+ result: false,
1149
+ },
1150
+ {
1151
+ text: "No package.json found. Would you like to create one?",
1152
+ result: true,
1153
+ },
1154
+
1155
+ {
1156
+ text: "Would you like to use TypeScript?",
1157
+ result: true,
1158
+ }
1159
+ );
1160
+ mockSelect({
1161
+ text: `Would you like to create a Worker at ${path.join(
1162
+ "src",
1163
+ "index.ts"
1164
+ )}?`,
1165
+ result: "fetch",
1166
+ });
1167
+ mockConfirm({
1168
+ text: "Would you like us to write your first test with Vitest?",
1169
+ result: true,
1170
+ });
1171
+
1172
+ await runWrangler("init");
1173
+
1174
+ checkFiles({
1175
+ items: {
1176
+ "package.json": {
1177
+ contents: expect.objectContaining({
1178
+ name: expect.stringContaining("wrangler-tests"),
1179
+ version: "0.0.0",
1180
+ scripts: {
1181
+ start: "wrangler dev",
1182
+ deploy: "wrangler publish",
1183
+ },
1184
+ }),
1185
+ },
1186
+ "src/index.js": false,
1187
+ "src/index.ts": true,
1188
+ },
1189
+ });
1190
+ expect(std.out).toMatchInlineSnapshot(`
1191
+ "✨ Created wrangler.toml
1192
+ ✨ Created package.json
1193
+ ✨ Created tsconfig.json
1194
+ ✨ Created src/index.ts
1195
+ ✨ Created src/index.test.ts
1196
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1197
+
1198
+ To start developing your Worker, run \`npm start\`
1199
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1200
+ `);
1201
+ });
1202
+
1203
+ it("should not overwrite package.json scripts for a typescript project", async () => {
1204
+ mockConfirm(
1205
+ {
1206
+ text: "Would you like to use git to manage this Worker?",
1207
+ result: false,
1208
+ },
1209
+ {
1210
+ text: "Would you like to install wrangler into package.json?",
1211
+ result: false,
1212
+ },
1213
+ {
1214
+ text: "Would you like to use TypeScript?",
1215
+ result: true,
1216
+ }
1217
+ );
1218
+ mockSelect({
1219
+ text: `Would you like to create a Worker at ${path.join(
1220
+ "src",
1221
+ "index.ts"
1222
+ )}?`,
1223
+ result: "fetch",
1224
+ });
1225
+ mockConfirm({
1226
+ text: "Would you like us to write your first test with Vitest?",
1227
+ result: true,
1228
+ });
1229
+ writeFiles({
1230
+ items: {
1231
+ "package.json": {
1232
+ contents: {
1233
+ scripts: {
1234
+ start: "test-start",
1235
+ deploy: "test-publish",
1236
+ },
1237
+ },
1238
+ },
1239
+ },
1240
+ });
1241
+
1242
+ await runWrangler("init");
1243
+
1244
+ checkFiles({
1245
+ items: {
1246
+ "package.json": {
1247
+ contents: {
1248
+ scripts: {
1249
+ start: "test-start",
1250
+ deploy: "test-publish",
1251
+ },
1252
+ },
1253
+ },
1254
+ "src/index.js": false,
1255
+ "src/index.ts": true,
1256
+ },
1257
+ });
1258
+ expect(std.out).toMatchInlineSnapshot(`
1259
+ "✨ Created wrangler.toml
1260
+ ✨ Created tsconfig.json
1261
+ ✨ Created src/index.ts
1262
+ ✨ Created src/index.test.ts
1263
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1264
+
1265
+ To start developing your Worker, run \`npx wrangler dev\`
1266
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1267
+ `);
1268
+ });
1269
+
1270
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
1271
+ mockConfirm(
1272
+ {
1273
+ text: "Would you like to use git to manage this Worker?",
1274
+ result: false,
1275
+ },
1276
+ {
1277
+ text: "Would you like to install wrangler into package.json?",
1278
+ result: false,
1279
+ },
1280
+ {
1281
+ text: "Would you like to use TypeScript?",
1282
+ result: true,
1283
+ }
1284
+ );
1285
+ const PLACEHOLDER = "/* placeholder text */";
1286
+ writeFiles({
1287
+ items: {
1288
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1289
+ "src/index.ts": { contents: PLACEHOLDER },
1290
+ },
1291
+ });
1292
+
1293
+ await runWrangler("init");
1294
+
1295
+ checkFiles({
1296
+ items: {
1297
+ "src/index.js": false,
1298
+ "src/index.ts": { contents: PLACEHOLDER },
1299
+ },
1300
+ });
1301
+ expect(std).toMatchInlineSnapshot(`
1302
+ Object {
1303
+ "debug": "",
1304
+ "err": "",
1305
+ "out": "✨ Created wrangler.toml
1306
+ ✨ Created tsconfig.json
1307
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1308
+ "warn": "",
1309
+ }
1310
+ `);
1311
+ });
1312
+
1313
+ 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 () => {
1314
+ mockConfirm(
1315
+ {
1316
+ text: "Would you like to use git to manage this Worker?",
1317
+ result: false,
1318
+ },
1319
+ {
1320
+ text: "No package.json found. Would you like to create one?",
1321
+ result: true,
1322
+ },
1323
+
1324
+ {
1325
+ text: "Would you like to use TypeScript?",
1326
+ result: true,
1327
+ }
1328
+ );
1329
+ const PLACEHOLDER = "/* placeholder text */";
1330
+ writeFiles({
1331
+ items: {
1332
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1333
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1334
+ },
1335
+ });
1336
+
1337
+ await runWrangler("init my-worker");
1338
+
1339
+ checkFiles({
1340
+ items: {
1341
+ "my-worker/src/index.js": false,
1342
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1343
+ },
1344
+ });
1345
+ expect(std).toMatchInlineSnapshot(`
1346
+ Object {
1347
+ "debug": "",
1348
+ "err": "",
1349
+ "out": "✨ Created my-worker/wrangler.toml
1350
+ ✨ Created my-worker/package.json
1351
+ ✨ Created my-worker/tsconfig.json
1352
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1353
+ "warn": "",
1354
+ }
1355
+ `);
1356
+ });
1357
+
1358
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
1359
+ mockConfirm(
1360
+ {
1361
+ text: "Would you like to use git to manage this Worker?",
1362
+ result: false,
1363
+ },
1364
+ {
1365
+ text: "No package.json found. Would you like to create one?",
1366
+ result: true,
1367
+ },
1368
+ {
1369
+ text: "Would you like to use TypeScript?",
1370
+ result: true,
1371
+ }
1372
+ );
1373
+ mockSelect({
1374
+ text: `Would you like to create a Worker at ${path.join(
1375
+ "src",
1376
+ "index.ts"
1377
+ )}?`,
1378
+ result: "none",
1379
+ });
1380
+
1381
+ await runWrangler("init");
1382
+
1383
+ checkFiles({
1384
+ items: {
1385
+ "tsconfig.json": {
1386
+ contents: {
1387
+ config: {
1388
+ compilerOptions: expect.objectContaining({
1389
+ types: ["@cloudflare/workers-types", "jest"],
1390
+ }),
1391
+ },
1392
+ error: undefined,
1393
+ },
1394
+ },
1395
+ },
1396
+ });
1397
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1398
+ "@cloudflare/workers-types",
1399
+ "typescript"
1400
+ );
1401
+ expect(std).toMatchInlineSnapshot(`
1402
+ Object {
1403
+ "debug": "",
1404
+ "err": "",
1405
+ "out": "✨ Created wrangler.toml
1406
+ ✨ Created package.json
1407
+ ✨ Created tsconfig.json
1408
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1409
+ "warn": "",
1410
+ }
1411
+ `);
1412
+ });
1413
+
1414
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
1415
+ mockConfirm({
1416
+ text: "Would you like to use git to manage this Worker?",
1417
+ result: false,
1418
+ });
1419
+ mockSelect({
1420
+ text: `Would you like to create a Worker at ${path.join(
1421
+ "src",
1422
+ "index.ts"
1423
+ )}?`,
1424
+ result: "fetch",
1425
+ });
1426
+ mockConfirm({
1427
+ text: "Would you like us to write your first test with Vitest?",
1428
+ result: true,
1429
+ });
1430
+ writeFiles({
1431
+ items: {
1432
+ "package.json": {
1433
+ contents: {
1434
+ name: "test",
1435
+ version: "1.0.0",
1436
+ devDependencies: {
1437
+ wrangler: "0.0.0",
1438
+ "@cloudflare/workers-types": "0.0.0",
1439
+ },
1440
+ },
1441
+ },
1442
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1443
+ },
1444
+ });
1445
+
1446
+ await runWrangler("init");
1447
+
1448
+ checkFiles({
1449
+ items: {
1450
+ "tsconfig.json": {
1451
+ contents: { config: { compilerOptions: {} }, error: undefined },
1452
+ },
1453
+ },
1454
+ });
1455
+ expect(std).toMatchInlineSnapshot(`
1456
+ Object {
1457
+ "debug": "",
1458
+ "err": "",
1459
+ "out": "✨ Created wrangler.toml
1460
+ ✨ Created src/index.ts
1461
+ ✨ Created src/index.test.ts
1462
+ ✨ Installed vitest into devDependencies
1463
+
1464
+ To start developing your Worker, run \`npx wrangler dev\`
1465
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1466
+ "warn": "",
1467
+ }
1468
+ `);
1469
+ });
1470
+
1471
+ it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
1472
+ mockConfirm(
1473
+ {
1474
+ text: "Would you like to use git to manage this Worker?",
1475
+ result: false,
1476
+ },
1477
+ {
1478
+ text: "No package.json found. Would you like to create one?",
1479
+ result: true,
1480
+ },
1481
+ {
1482
+ text: "Would you like to use TypeScript?",
1483
+ result: true,
1484
+ }
1485
+ );
1486
+ mockSelect({
1487
+ text: `Would you like to create a Worker at ${path.join(
1488
+ "path",
1489
+ "to",
1490
+ "worker",
1491
+ "my-worker",
1492
+ "src",
1493
+ "index.ts"
1494
+ )}?`,
1495
+ result: "fetch",
1496
+ });
1497
+ mockConfirm({
1498
+ text: "Would you like us to write your first test with Vitest?",
1499
+ result: true,
1500
+ });
1501
+ writeFiles({
1502
+ items: {
1503
+ "path/to/worker/package.json": {
1504
+ contents: {
1505
+ name: "test",
1506
+ version: "1.0.0",
1507
+ devDependencies: {
1508
+ wrangler: "0.0.0",
1509
+ "@cloudflare/workers-types": "0.0.0",
1510
+ },
1511
+ },
1512
+ },
1513
+ "path/to/worker/tsconfig.json": { contents: { compilerOptions: {} } },
1514
+ },
1515
+ });
1516
+
1517
+ await runWrangler("init path/to/worker/my-worker");
1518
+
1519
+ checkFiles({
1520
+ items: {
1521
+ "path/to/worker/tsconfig.json": {
1522
+ contents: { config: { compilerOptions: {}, error: undefined } },
1523
+ },
1524
+ },
1525
+ });
1526
+ expect(std).toMatchInlineSnapshot(`
1527
+ Object {
1528
+ "debug": "",
1529
+ "err": "",
1530
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1531
+ ✨ Created path/to/worker/my-worker/package.json
1532
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1533
+ ✨ Created path/to/worker/my-worker/src/index.ts
1534
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
1535
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1536
+
1537
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1538
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1539
+ "warn": "",
1540
+ }
1541
+ `);
1542
+ });
1543
+
1544
+ it("should offer to install type definitions in an existing typescript project", async () => {
1545
+ mockConfirm(
1546
+ {
1547
+ text: "Would you like to use git to manage this Worker?",
1548
+ result: false,
1549
+ },
1550
+ {
1551
+ text: "Would you like to install wrangler into package.json?",
1552
+ result: false,
1553
+ },
1554
+ {
1555
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1556
+ result: true,
1557
+ }
1558
+ );
1559
+ mockSelect({
1560
+ text: `Would you like to create a Worker at ${path.join(
1561
+ "src",
1562
+ "index.ts"
1563
+ )}?`,
1564
+ result: "none",
1565
+ });
1566
+ writeFiles({
1567
+ items: {
1568
+ "./package.json": {
1569
+ contents: {
1570
+ name: "test",
1571
+ version: "1.0.0",
1572
+ },
1573
+ },
1574
+ "./tsconfig.json": { contents: { compilerOptions: {} } },
1575
+ },
1576
+ });
1577
+
1578
+ await runWrangler("init");
1579
+
1580
+ checkFiles({
1581
+ items: {
1582
+ // unchanged tsconfig
1583
+ "tsconfig.json": { contents: { config: { compilerOptions: {} } } },
1584
+ },
1585
+ });
1586
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1587
+ "@cloudflare/workers-types"
1588
+ );
1589
+ expect(std).toMatchInlineSnapshot(`
1590
+ Object {
1591
+ "debug": "",
1592
+ "err": "",
1593
+ "out": "✨ Created wrangler.toml
1594
+ ✨ Installed @cloudflare/workers-types into devDependencies
1595
+ 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1596
+ "warn": "",
1597
+ }
1598
+ `);
1599
+ });
1600
+
1601
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1602
+ mockConfirm({
1603
+ text: "Would you like to use git to manage this Worker?",
1604
+ result: false,
1605
+ });
1606
+ mockSelect({
1607
+ text: `Would you like to create a Worker at ${path.join(
1608
+ "src",
1609
+ "index.ts"
1610
+ )}?`,
1611
+ result: "fetch",
1612
+ });
1613
+ mockConfirm({
1614
+ text: "Would you like us to write your first test with Vitest?",
1615
+ result: true,
1616
+ });
1617
+ writeFiles({
1618
+ items: {
1619
+ "package.json": {
1620
+ contents: {
1621
+ name: "test",
1622
+ version: "1.0.0",
1623
+ devDependencies: {
1624
+ wrangler: "0.0.0",
1625
+ "@cloudflare/workers-types": "0.0.0",
1626
+ },
1627
+ },
1628
+ },
1629
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1630
+ },
1631
+ });
1632
+ setWorkingDirectory("./sub-1/sub-2");
1633
+
1634
+ await runWrangler("init");
1635
+
1636
+ checkFiles({
1637
+ items: {
1638
+ "tsconfig.json": false,
1639
+ "../../tsconfig.json": {
1640
+ contents: { config: { compilerOptions: {} } },
1641
+ },
1642
+ },
1643
+ });
1644
+ expect(std).toMatchInlineSnapshot(`
1645
+ Object {
1646
+ "debug": "",
1647
+ "err": "",
1648
+ "out": "✨ Created wrangler.toml
1649
+ ✨ Created src/index.ts
1650
+ ✨ Created src/index.test.ts
1651
+ ✨ Installed vitest into devDependencies
1652
+
1653
+ To start developing your Worker, run \`npx wrangler dev\`
1654
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1655
+ "warn": "",
1656
+ }
1657
+ `);
1658
+ });
1659
+ });
1660
+
1661
+ describe("javascript", () => {
1662
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
1663
+ mockConfirm(
1664
+ {
1665
+ text: "Would you like to use git to manage this Worker?",
1666
+ result: false,
1667
+ },
1668
+ {
1669
+ text: "No package.json found. Would you like to create one?",
1670
+ result: true,
1671
+ },
1672
+
1673
+ {
1674
+ text: "Would you like to use TypeScript?",
1675
+ result: false,
1676
+ }
1677
+ );
1678
+ mockSelect({
1679
+ text: `Would you like to create a Worker at ${path.join(
1680
+ "src",
1681
+ "index.js"
1682
+ )}?`,
1683
+ result: "fetch",
1684
+ });
1685
+ mockConfirm({
1686
+ text: "Would you like us to write your first test?",
1687
+ result: false,
1688
+ });
1689
+ await runWrangler("init");
1690
+
1691
+ checkFiles({
1692
+ items: {
1693
+ "src/index.js": true,
1694
+ "src/index.ts": false,
1695
+ "package.json": {
1696
+ contents: expect.objectContaining({
1697
+ name: expect.stringContaining("wrangler-tests"),
1698
+ version: "0.0.0",
1699
+ scripts: {
1700
+ start: "wrangler dev",
1701
+ deploy: "wrangler publish",
1702
+ },
1703
+ }),
1704
+ },
1705
+ },
1706
+ });
1707
+ expect(std.out).toMatchInlineSnapshot(`
1708
+ "✨ Created wrangler.toml
1709
+ ✨ Created package.json
1710
+ ✨ Created src/index.js
1711
+
1712
+ To start developing your Worker, run \`npm start\`
1713
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1714
+ `);
1715
+ });
1716
+ it("should add a jest test for a non-ts project with .js extension", async () => {
1717
+ mockConfirm(
1718
+ {
1719
+ text: "Would you like to use git to manage this Worker?",
1720
+ result: false,
1721
+ },
1722
+ {
1723
+ text: "No package.json found. Would you like to create one?",
1724
+ result: true,
1725
+ },
1726
+ {
1727
+ text: "Would you like to use TypeScript?",
1728
+ result: false,
1729
+ }
1730
+ );
1731
+ mockSelect({
1732
+ text: `Would you like to create a Worker at ${path.join(
1733
+ "src",
1734
+ "index.js"
1735
+ )}?`,
1736
+ result: "fetch",
1737
+ });
1738
+ mockConfirm({
1739
+ text: "Would you like us to write your first test?",
1740
+ result: true,
1741
+ });
1742
+ mockSelect({
1743
+ text: "Which test runner would you like to use?",
1744
+ result: "jest",
1745
+ });
1746
+ await runWrangler("init");
1747
+
1748
+ checkFiles({
1749
+ items: {
1750
+ "src/index.js": true,
1751
+ "src/index.test.js": true,
1752
+ "src/index.ts": false,
1753
+ "package.json": {
1754
+ contents: expect.objectContaining({
1755
+ name: expect.stringContaining("wrangler-tests"),
1756
+ version: "0.0.0",
1757
+ scripts: {
1758
+ start: "wrangler dev",
1759
+ deploy: "wrangler publish",
1760
+ test: "jest",
1761
+ },
1762
+ }),
1763
+ },
1764
+ },
1765
+ });
1766
+ expect(std.out).toMatchInlineSnapshot(`
1767
+ "✨ Created wrangler.toml
1768
+ ✨ Created package.json
1769
+ ✨ Created src/index.js
1770
+ ✨ Created src/index.test.js
1771
+ ✨ Installed jest into devDependencies
1772
+
1773
+ To start developing your Worker, run \`npm start\`
1774
+ To start testing your Worker, run \`npm test\`
1775
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1776
+ `);
1777
+ });
1778
+
1779
+ it("should add a vitest test for a non-ts project with .js extension", async () => {
1780
+ mockConfirm(
1781
+ {
1782
+ text: "Would you like to use git to manage this Worker?",
1783
+ result: false,
1784
+ },
1785
+ {
1786
+ text: "No package.json found. Would you like to create one?",
1787
+ result: true,
1788
+ },
1789
+ {
1790
+ text: "Would you like to use TypeScript?",
1791
+ result: false,
1792
+ }
1793
+ );
1794
+ mockSelect({
1795
+ text: `Would you like to create a Worker at ${path.join(
1796
+ "src",
1797
+ "index.js"
1798
+ )}?`,
1799
+ result: "fetch",
1800
+ });
1801
+ mockConfirm({
1802
+ text: "Would you like us to write your first test?",
1803
+ result: true,
1804
+ });
1805
+ mockSelect({
1806
+ text: "Which test runner would you like to use?",
1807
+ result: "vitest",
1808
+ });
1809
+ await runWrangler("init");
1810
+
1811
+ checkFiles({
1812
+ items: {
1813
+ "src/index.js": true,
1814
+ "src/index.test.js": true,
1815
+ "src/index.ts": false,
1816
+ "package.json": {
1817
+ contents: expect.objectContaining({
1818
+ name: expect.stringContaining("wrangler-tests"),
1819
+ version: "0.0.0",
1820
+ scripts: {
1821
+ start: "wrangler dev",
1822
+ deploy: "wrangler publish",
1823
+ test: "vitest",
1824
+ },
1825
+ }),
1826
+ },
1827
+ },
1828
+ });
1829
+ expect(std.out).toMatchInlineSnapshot(`
1830
+ "✨ Created wrangler.toml
1831
+ ✨ Created package.json
1832
+ ✨ Created src/index.js
1833
+ ✨ Created src/index.test.js
1834
+ ✨ Installed vitest into devDependencies
1835
+
1836
+ To start developing your Worker, run \`npm start\`
1837
+ To start testing your Worker, run \`npm test\`
1838
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1839
+ `);
1840
+ });
1841
+
1842
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1843
+ mockConfirm(
1844
+ {
1845
+ text: "Would you like to use git to manage this Worker?",
1846
+ result: false,
1847
+ },
1848
+ {
1849
+ text: "Would you like to install wrangler into package.json?",
1850
+ result: false,
1851
+ },
1852
+ {
1853
+ text: "Would you like to use TypeScript?",
1854
+ result: false,
1855
+ }
1856
+ );
1857
+ mockSelect({
1858
+ text: `Would you like to create a Worker at ${path.join(
1859
+ "src",
1860
+ "index.js"
1861
+ )}?`,
1862
+ result: "fetch",
1863
+ });
1864
+ mockConfirm({
1865
+ text: "Would you like us to write your first test?",
1866
+ result: false,
1867
+ });
1868
+ writeFiles({
1869
+ items: {
1870
+ "package.json": {
1871
+ contents: {
1872
+ scripts: {
1873
+ start: "test-start",
1874
+ deploy: "test-publish",
1875
+ },
1876
+ },
1877
+ },
1878
+ },
1879
+ });
1880
+
1881
+ await runWrangler("init");
1882
+
1883
+ checkFiles({
1884
+ items: {
1885
+ "src/index.js": true,
1886
+ "src/index.ts": false,
1887
+ "package.json": {
1888
+ contents: expect.objectContaining({
1889
+ scripts: {
1890
+ start: "test-start",
1891
+ deploy: "test-publish",
1892
+ },
1893
+ }),
1894
+ },
1895
+ },
1896
+ });
1897
+ expect(std.out).toMatchInlineSnapshot(`
1898
+ "✨ Created wrangler.toml
1899
+ ✨ Created src/index.js
1900
+
1901
+ To start developing your Worker, run \`npx wrangler dev\`
1902
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1903
+ `);
1904
+ });
1905
+
1906
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1907
+ mockConfirm(
1908
+ {
1909
+ text: "Would you like to use git to manage this Worker?",
1910
+ result: false,
1911
+ },
1912
+ {
1913
+ text: "Would you like to install wrangler into package.json?",
1914
+ result: false,
1915
+ },
1916
+ {
1917
+ text: "Would you like to use TypeScript?",
1918
+ result: false,
1919
+ }
1920
+ );
1921
+ const PLACEHOLDER = "/* placeholder text */";
1922
+ writeFiles({
1923
+ items: {
1924
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1925
+ "src/index.js": { contents: PLACEHOLDER },
1926
+ },
1927
+ });
1928
+
1929
+ await runWrangler("init");
1930
+
1931
+ checkFiles({
1932
+ items: {
1933
+ "src/index.js": { contents: PLACEHOLDER },
1934
+ "src/index.ts": false,
1935
+ },
1936
+ });
1937
+ expect(std).toMatchInlineSnapshot(`
1938
+ Object {
1939
+ "debug": "",
1940
+ "err": "",
1941
+ "out": "✨ Created wrangler.toml",
1942
+ "warn": "",
1943
+ }
1944
+ `);
1945
+ });
1946
+
1947
+ it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1948
+ mockConfirm(
1949
+ {
1950
+ text: "Would you like to use git to manage this Worker?",
1951
+ result: false,
1952
+ },
1953
+ {
1954
+ text: `Would you like to install wrangler into ${path.join(
1955
+ "my-worker",
1956
+ "package.json"
1957
+ )}?`,
1958
+ result: false,
1959
+ },
1960
+ {
1961
+ text: "Would you like to use TypeScript?",
1962
+ result: false,
1963
+ }
1964
+ );
1965
+ const PLACEHOLDER = "/* placeholder text */";
1966
+ writeFiles({
1967
+ items: {
1968
+ "my-worker/package.json": {
1969
+ contents: { name: "test", version: "1.0.0" },
1970
+ },
1971
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
1972
+ },
1973
+ });
1974
+
1975
+ await runWrangler("init my-worker");
1976
+
1977
+ checkFiles({
1978
+ items: {
1979
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
1980
+ "my-worker/src/index.ts": false,
1981
+ },
1982
+ });
1983
+ expect(std).toMatchInlineSnapshot(`
1984
+ Object {
1985
+ "debug": "",
1986
+ "err": "",
1987
+ "out": "✨ Created my-worker/wrangler.toml",
1988
+ "warn": "",
1989
+ }
1990
+ `);
1991
+ });
1992
+ });
1993
+
1994
+ describe("worker names", () => {
1995
+ it("should create a worker with a given name", async () => {
1996
+ await runWrangler("init my-worker -y");
1997
+
1998
+ checkFiles({
1999
+ items: {
2000
+ "my-worker/wrangler.toml": wranglerToml({
2001
+ ...MINIMAL_WRANGLER_TOML,
2002
+ name: "my-worker",
2003
+ }),
2004
+ },
2005
+ });
2006
+ });
2007
+
2008
+ it('should create a worker with the name of the current directory if "name" is .', async () => {
2009
+ await runWrangler("init . -y");
2010
+
2011
+ const workerName = path.basename(process.cwd()).toLowerCase();
2012
+ checkFiles({
2013
+ items: {
2014
+ "wrangler.toml": wranglerToml({
2015
+ ...MINIMAL_WRANGLER_TOML,
2016
+ name: workerName,
2017
+ }),
2018
+ },
2019
+ });
2020
+ expect(std).toMatchInlineSnapshot(`
2021
+ Object {
2022
+ "debug": "",
2023
+ "err": "",
2024
+ "out": "✨ Created wrangler.toml
2025
+ ✨ Initialized git repository
2026
+ ✨ Created package.json
2027
+ ✨ Created tsconfig.json
2028
+ ✨ Created src/index.ts
2029
+ ✨ Created src/index.test.ts
2030
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2031
+
2032
+ To start developing your Worker, run \`npm start\`
2033
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2034
+ "warn": "",
2035
+ }
2036
+ `);
2037
+ });
2038
+
2039
+ it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
2040
+ await runWrangler("init path/to/worker -y");
2041
+
2042
+ checkFiles({
2043
+ items: {
2044
+ "path/to/worker/wrangler.toml": wranglerToml({
2045
+ ...MINIMAL_WRANGLER_TOML,
2046
+ name: "worker",
2047
+ }),
2048
+ },
2049
+ });
2050
+ expect(std).toMatchInlineSnapshot(`
2051
+ Object {
2052
+ "debug": "",
2053
+ "err": "",
2054
+ "out": "✨ Created path/to/worker/wrangler.toml
2055
+ ✨ Initialized git repository at path/to/worker
2056
+ ✨ Created path/to/worker/package.json
2057
+ ✨ Created path/to/worker/tsconfig.json
2058
+ ✨ Created path/to/worker/src/index.ts
2059
+ ✨ Created path/to/worker/src/index.test.ts
2060
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2061
+
2062
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
2063
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2064
+ "warn": "",
2065
+ }
2066
+ `);
2067
+ });
2068
+
2069
+ it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
2070
+ await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
2071
+
2072
+ checkFiles({
2073
+ items: {
2074
+ "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml": wranglerToml({
2075
+ ...MINIMAL_WRANGLER_TOML,
2076
+ name: "weird_w0rkr_n4m3-js-tsx-tar-gz",
2077
+ }),
2078
+ },
2079
+ });
2080
+ expect(std).toMatchInlineSnapshot(`
2081
+ Object {
2082
+ "debug": "",
2083
+ "err": "",
2084
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
2085
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
2086
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
2087
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
2088
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
2089
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.test.ts
2090
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2091
+
2092
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
2093
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2094
+ "warn": "",
2095
+ }
2096
+ `);
2097
+ });
2098
+
2099
+ it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
2100
+ mockConfirm(
2101
+ {
2102
+ text: "Would you like to use git to manage this Worker?",
2103
+ result: false,
2104
+ },
2105
+ {
2106
+ text: "No package.json found. Would you like to create one?",
2107
+ result: true,
2108
+ },
2109
+ {
2110
+ text: "Would you like to use TypeScript?",
2111
+ result: true,
2112
+ }
2113
+ );
2114
+ mockSelect({
2115
+ text: `Would you like to create a Worker at ${path.join(
2116
+ "sub",
2117
+ "folder",
2118
+ "worker",
2119
+ "src",
2120
+ "index.ts"
2121
+ )}?`,
2122
+ result: "fetch",
2123
+ });
2124
+ mockConfirm({
2125
+ text: "Would you like us to write your first test with Vitest?",
2126
+ result: true,
2127
+ });
2128
+ writeFiles({
2129
+ items: {
2130
+ "package.json": { contents: { name: "top-level" } },
2131
+ "tsconfig.json": { contents: { compilerOptions: {} } },
2132
+ "wrangler.toml": wranglerToml({
2133
+ name: "top-level",
2134
+ compatibility_date: "some-date",
2135
+ }),
2136
+ },
2137
+ });
2138
+
2139
+ await runWrangler("init sub/folder/worker");
2140
+
2141
+ // Ancestor files are untouched.
2142
+ checkFiles({
2143
+ items: {
2144
+ "package.json": { contents: { name: "top-level" } },
2145
+ "tsconfig.json": {
2146
+ contents: { config: { compilerOptions: {} }, error: undefined },
2147
+ },
2148
+ "wrangler.toml": wranglerToml({
2149
+ name: "top-level",
2150
+ compatibility_date: "some-date",
2151
+ }),
2152
+ },
2153
+ });
2154
+ // New initialized Worker has its own files.
2155
+ checkFiles({
2156
+ items: {
2157
+ "sub/folder/worker/package.json": {
2158
+ contents: expect.objectContaining({
2159
+ name: "worker",
2160
+ }),
2161
+ },
2162
+ "sub/folder/worker/tsconfig.json": true,
2163
+ "sub/folder/worker/wrangler.toml": wranglerToml({
2164
+ ...MINIMAL_WRANGLER_TOML,
2165
+ name: "worker",
2166
+ }),
2167
+ },
2168
+ });
2169
+ });
2170
+ });
2171
+
2172
+ describe("from dashboard", () => {
2173
+ mockApiToken();
2174
+ mockAccountId({ accountId: "LCARS" });
2175
+
2176
+ const mockDashboardScript = `
2177
+ export default {
2178
+ async fetch(request, env, ctx) {
2179
+ return new Response("Hello World!");
2180
+ },
2181
+ };
2182
+ `;
2183
+ const mockServiceMetadata = {
2184
+ id: "memory-crystal",
2185
+ default_environment: {
2186
+ environment: "test",
2187
+ created_on: "1987-9-27",
2188
+ modified_on: "1987-9-27",
2189
+ script: {
2190
+ id: "memory-crystal",
2191
+ tag: "test-tag",
2192
+ etag: "some-etag",
2193
+ handlers: [],
2194
+ modified_on: "1987-9-27",
2195
+ created_on: "1987-9-27",
2196
+ migration_tag: "some-migration-tag",
2197
+ usage_model: "bundled",
2198
+ compatibility_date: "1987-9-27",
2199
+ },
2200
+ },
2201
+ created_on: "1987-9-27",
2202
+ modified_on: "1987-9-27",
2203
+ usage_model: "bundled",
2204
+ environments: [
2205
+ {
2206
+ environment: "test",
2207
+ created_on: "1987-9-27",
2208
+ modified_on: "1987-9-27",
2209
+ },
2210
+ {
2211
+ environment: "staging",
2212
+ created_on: "1987-9-27",
2213
+ modified_on: "1987-9-27",
2214
+ },
2215
+ ],
2216
+ };
2217
+ const mockBindingsRes = [
2218
+ {
2219
+ type: "secret_text",
2220
+ name: "ABC",
2221
+ },
2222
+ {
2223
+ type: "plain_text",
2224
+ name: "ANOTHER-NAME",
2225
+ text: "thing-TEXT",
2226
+ },
2227
+ {
2228
+ type: "durable_object_namespace",
2229
+ name: "DURABLE_TEST",
2230
+ class_name: "Durability",
2231
+ script_name: "another-durable-object-worker",
2232
+ environment: "production",
2233
+ },
2234
+ {
2235
+ type: "kv_namespace",
2236
+ name: "kv_testing",
2237
+ namespace_id: "some-namespace-id",
2238
+ },
2239
+ {
2240
+ type: "r2_bucket",
2241
+ bucket_name: "test-bucket",
2242
+ name: "test-bucket",
2243
+ },
2244
+ {
2245
+ environment: "production",
2246
+ name: "website",
2247
+ service: "website",
2248
+ type: "service",
2249
+ },
2250
+ {
2251
+ type: "dispatch_namespace",
2252
+ name: "name-namespace-mock",
2253
+ namespace: "namespace-mock",
2254
+ },
2255
+ {
2256
+ name: "httplogs",
2257
+ type: "logfwdr",
2258
+ destination: "httplogs",
2259
+ },
2260
+ {
2261
+ name: "trace",
2262
+ type: "logfwdr",
2263
+ destination: "trace",
2264
+ },
2265
+ {
2266
+ type: "wasm_module",
2267
+ name: "WASM_MODULE_ONE",
2268
+ part: "./some_wasm.wasm",
2269
+ },
2270
+ {
2271
+ type: "wasm_module",
2272
+ name: "WASM_MODULE_TWO",
2273
+ part: "./more_wasm.wasm",
2274
+ },
2275
+ {
2276
+ type: "text_blob",
2277
+ name: "TEXT_BLOB_ONE",
2278
+ part: "./my-entire-app-depends-on-this.cfg",
2279
+ },
2280
+ {
2281
+ type: "text_blob",
2282
+ name: "TEXT_BLOB_TWO",
2283
+ part: "./the-entirety-of-human-knowledge.txt",
2284
+ },
2285
+ { type: "data_blob", name: "DATA_BLOB_ONE", part: "DATA_BLOB_ONE" },
2286
+ { type: "data_blob", name: "DATA_BLOB_TWO", part: "DATA_BLOB_TWO" },
2287
+ {
2288
+ type: "some unsafe thing",
2289
+ name: "UNSAFE_BINDING_ONE",
2290
+ data: { some: { unsafe: "thing" } },
2291
+ },
2292
+ {
2293
+ type: "another unsafe thing",
2294
+ name: "UNSAFE_BINDING_TWO",
2295
+ data: 1337,
2296
+ },
2297
+ ];
2298
+ const mockRoutesRes = [
2299
+ {
2300
+ id: "some-route-id",
2301
+ pattern: "delta.quadrant",
2302
+ },
2303
+ ];
2304
+ const mockConfigExpected = {
2305
+ main: "src/index.ts",
2306
+ compatibility_date: "1987-9-27",
2307
+ name: "isolinear-optical-chip",
2308
+ migrations: [
2309
+ {
2310
+ new_classes: ["Durability"],
2311
+ tag: "some-migration-tag",
2312
+ },
2313
+ ],
2314
+ durable_objects: {
2315
+ bindings: [
2316
+ {
2317
+ class_name: "Durability",
2318
+ name: "DURABLE_TEST",
2319
+ script_name: "another-durable-object-worker",
2320
+ environment: "production",
2321
+ },
2322
+ ],
2323
+ },
2324
+ kv_namespaces: [
2325
+ {
2326
+ binding: "kv_testing",
2327
+ id: "some-namespace-id",
2328
+ },
2329
+ ],
2330
+ r2_buckets: [
2331
+ {
2332
+ bucket_name: "test-bucket",
2333
+ binding: "test-bucket",
2334
+ },
2335
+ ],
2336
+ dispatch_namespaces: [
2337
+ {
2338
+ binding: "name-namespace-mock",
2339
+ namespace: "namespace-mock",
2340
+ },
2341
+ ],
2342
+ route: "delta.quadrant",
2343
+ services: [
2344
+ {
2345
+ environment: "production",
2346
+ binding: "website",
2347
+ service: "website",
2348
+ },
2349
+ ],
2350
+ triggers: {
2351
+ crons: ["0 0 0 * * *"],
2352
+ },
2353
+ usage_model: "bundled",
2354
+ vars: {
2355
+ "ANOTHER-NAME": "thing-TEXT",
2356
+ },
2357
+ env: {
2358
+ test: {},
2359
+ staging: {},
2360
+ },
2361
+ unsafe: {
2362
+ bindings: [
2363
+ {
2364
+ name: "UNSAFE_BINDING_ONE",
2365
+ type: "some unsafe thing",
2366
+ data: { some: { unsafe: "thing" } },
2367
+ },
2368
+ {
2369
+ name: "UNSAFE_BINDING_TWO",
2370
+ type: "another unsafe thing",
2371
+ data: 1337,
2372
+ },
2373
+ ],
2374
+ },
2375
+ wasm_modules: {
2376
+ WASM_MODULE_ONE: "./some_wasm.wasm",
2377
+ WASM_MODULE_TWO: "./more_wasm.wasm",
2378
+ },
2379
+ text_blobs: {
2380
+ TEXT_BLOB_ONE: "./my-entire-app-depends-on-this.cfg",
2381
+ TEXT_BLOB_TWO: "./the-entirety-of-human-knowledge.txt",
2382
+ },
2383
+ data_blobs: {
2384
+ DATA_BLOB_ONE: "DATA_BLOB_ONE",
2385
+ DATA_BLOB_TWO: "DATA_BLOB_TWO",
2386
+ },
2387
+ logfwdr: {
2388
+ schema: "",
2389
+ bindings: [
2390
+ {
2391
+ name: "httplogs",
2392
+ destination: "httplogs",
2393
+ },
2394
+ {
2395
+ name: "trace",
2396
+ destination: "trace",
2397
+ },
2398
+ ],
2399
+ },
2400
+ };
2401
+
2402
+ function mockSupportingDashRequests({
2403
+ expectedAccountId = "",
2404
+ expectedScriptName = "",
2405
+ expectedEnvironment = "",
2406
+ expectedCompatDate,
2407
+ }: {
2408
+ expectedAccountId: string;
2409
+ expectedScriptName: string;
2410
+ expectedEnvironment: string;
2411
+ expectedCompatDate: string | undefined;
2412
+ }) {
2413
+ msw.use(
2414
+ rest.get(
2415
+ `*/accounts/:accountId/workers/services/:scriptName`,
2416
+ (req, res, ctx) => {
2417
+ expect(req.params.accountId).toEqual(expectedAccountId);
2418
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2419
+
2420
+ if (expectedCompatDate === undefined)
2421
+ (mockServiceMetadata.default_environment.script
2422
+ .compatibility_date as unknown) = expectedCompatDate;
2423
+
2424
+ return res.once(
2425
+ ctx.status(200),
2426
+ ctx.json({
2427
+ success: true,
2428
+ errors: [],
2429
+ messages: [],
2430
+ result: mockServiceMetadata,
2431
+ })
2432
+ );
2433
+ }
2434
+ ),
2435
+ rest.get(
2436
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2437
+ (req, res, ctx) => {
2438
+ expect(req.params.accountId).toEqual(expectedAccountId);
2439
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2440
+ expect(req.params.environment).toEqual(expectedEnvironment);
2441
+
2442
+ return res.once(
2443
+ ctx.status(200),
2444
+ ctx.json({
2445
+ success: true,
2446
+ errors: [],
2447
+ messages: [],
2448
+ result: mockBindingsRes,
2449
+ })
2450
+ );
2451
+ }
2452
+ ),
2453
+ rest.get(
2454
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2455
+ (req, res, ctx) => {
2456
+ expect(req.params.accountId).toEqual(expectedAccountId);
2457
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2458
+ expect(req.params.environment).toEqual(expectedEnvironment);
2459
+
2460
+ return res.once(
2461
+ ctx.status(200),
2462
+ ctx.json({
2463
+ success: true,
2464
+ errors: [],
2465
+ messages: [],
2466
+ result: mockRoutesRes,
2467
+ })
2468
+ );
2469
+ }
2470
+ ),
2471
+ rest.get(
2472
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2473
+
2474
+ (req, res, ctx) => {
2475
+ expect(req.params.accountId).toEqual(expectedAccountId);
2476
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2477
+ expect(req.params.environment).toEqual(expectedEnvironment);
2478
+
2479
+ return res.once(
2480
+ ctx.status(200),
2481
+ ctx.json({
2482
+ success: true,
2483
+ errors: [],
2484
+ messages: [],
2485
+ result: mockServiceMetadata.default_environment,
2486
+ })
2487
+ );
2488
+ }
2489
+ ),
2490
+ rest.get(
2491
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2492
+ (req, res, ctx) => {
2493
+ expect(req.params.accountId).toEqual(expectedAccountId);
2494
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2495
+
2496
+ return res.once(
2497
+ ctx.status(200),
2498
+ ctx.json({
2499
+ success: true,
2500
+ errors: [],
2501
+ messages: [],
2502
+ result: {
2503
+ schedules: [
2504
+ {
2505
+ cron: "0 0 0 * * *",
2506
+ created_on: new Date(1987, 9, 27),
2507
+ modified_on: new Date(1987, 9, 27),
2508
+ },
2509
+ ],
2510
+ },
2511
+ })
2512
+ );
2513
+ }
2514
+ )
2515
+ );
2516
+ }
2517
+
2518
+ //TODO: Tests for a case when a worker name doesn't exist - JACOB & CASS
2519
+ it("should download source script from dashboard w/ positional <name> in TypeScript project", async () => {
2520
+ mockSupportingDashRequests({
2521
+ expectedAccountId: "LCARS",
2522
+ expectedScriptName: "memory-crystal",
2523
+ expectedEnvironment: "test",
2524
+ expectedCompatDate: "1987-9-27",
2525
+ });
2526
+ setMockFetchDashScript(mockDashboardScript);
2527
+ mockConfirm(
2528
+ {
2529
+ text: "Would you like to use git to manage this Worker?",
2530
+ result: false,
2531
+ },
2532
+ {
2533
+ text: "No package.json found. Would you like to create one?",
2534
+ result: true,
2535
+ },
2536
+ {
2537
+ text: "Would you like to use TypeScript?",
2538
+ result: true,
2539
+ }
2540
+ );
2541
+
2542
+ await runWrangler(
2543
+ "init isolinear-optical-chip --from-dash memory-crystal"
2544
+ );
2545
+
2546
+ expect(std.out).toContain("cd isolinear-optical-chip");
2547
+
2548
+ checkFiles({
2549
+ items: {
2550
+ "isolinear-optical-chip/src/index.js": false,
2551
+ "isolinear-optical-chip/src/index.ts": {
2552
+ contents: mockDashboardScript,
2553
+ },
2554
+ "isolinear-optical-chip/package.json": {
2555
+ contents: expect.objectContaining({
2556
+ name: "isolinear-optical-chip",
2557
+ }),
2558
+ },
2559
+ "isolinear-optical-chip/tsconfig.json": true,
2560
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2561
+ ...mockConfigExpected,
2562
+ }),
2563
+ },
2564
+ });
2565
+ });
2566
+
2567
+ it("should fail on init --from-dash on non-existent worker name", async () => {
2568
+ msw.use(
2569
+ rest.get(
2570
+ `*/accounts/:accountId/workers/services/:scriptName`,
2571
+ (req, res, ctx) => {
2572
+ return res.once(
2573
+ ctx.status(200),
2574
+ ctx.json({
2575
+ success: true,
2576
+ errors: [],
2577
+ messages: [],
2578
+ result: mockServiceMetadata,
2579
+ })
2580
+ );
2581
+ }
2582
+ )
2583
+ );
2584
+ setMockFetchDashScript(mockDashboardScript);
2585
+ mockConfirm(
2586
+ {
2587
+ text: "Would you like to use git to manage this Worker?",
2588
+ result: false,
2589
+ },
2590
+ {
2591
+ text: "No package.json found. Would you like to create one?",
2592
+ result: true,
2593
+ },
2594
+ {
2595
+ text: "Would you like to use TypeScript?",
2596
+ result: true,
2597
+ }
2598
+ );
2599
+
2600
+ await expect(
2601
+ runWrangler("init isolinear-optical-chip --from-dash i-dont-exist")
2602
+ ).rejects.toThrowError();
2603
+ });
2604
+
2605
+ it("should download source script from dashboard w/ out positional <name>", async () => {
2606
+ mockSupportingDashRequests({
2607
+ expectedAccountId: "LCARS",
2608
+ expectedScriptName: "isolinear-optical-chip",
2609
+ expectedEnvironment: "test",
2610
+ expectedCompatDate: "1987-9-27",
2611
+ });
2612
+ setMockFetchDashScript(mockDashboardScript);
2613
+ mockConfirm(
2614
+ {
2615
+ text: "Would you like to use git to manage this Worker?",
2616
+ result: false,
2617
+ },
2618
+ {
2619
+ text: "No package.json found. Would you like to create one?",
2620
+ result: true,
2621
+ },
2622
+ {
2623
+ text: "Would you like to use TypeScript?",
2624
+ result: true,
2625
+ }
2626
+ );
2627
+
2628
+ await runWrangler("init --from-dash isolinear-optical-chip");
2629
+
2630
+ expect(fs.readFileSync("./isolinear-optical-chip/wrangler.toml", "utf8"))
2631
+ .toMatchInlineSnapshot(`
2632
+ "name = \\"isolinear-optical-chip\\"
2633
+ main = \\"src/index.ts\\"
2634
+ compatibility_date = \\"1987-9-27\\"
2635
+ route = \\"delta.quadrant\\"
2636
+ usage_model = \\"bundled\\"
2637
+
2638
+ [[migrations]]
2639
+ tag = \\"some-migration-tag\\"
2640
+ new_classes = [ \\"Durability\\" ]
2641
+
2642
+ [triggers]
2643
+ crons = [ \\"0 0 0 * * *\\" ]
2644
+
2645
+ [env]
2646
+ test = { }
2647
+ staging = { }
2648
+
2649
+ [vars]
2650
+ ANOTHER-NAME = \\"thing-TEXT\\"
2651
+
2652
+ [[durable_objects.bindings]]
2653
+ name = \\"DURABLE_TEST\\"
2654
+ class_name = \\"Durability\\"
2655
+ script_name = \\"another-durable-object-worker\\"
2656
+ environment = \\"production\\"
2657
+
2658
+ [[kv_namespaces]]
2659
+ id = \\"some-namespace-id\\"
2660
+ binding = \\"kv_testing\\"
2661
+
2662
+ [[r2_buckets]]
2663
+ binding = \\"test-bucket\\"
2664
+ bucket_name = \\"test-bucket\\"
2665
+
2666
+ [[services]]
2667
+ binding = \\"website\\"
2668
+ service = \\"website\\"
2669
+ environment = \\"production\\"
2670
+
2671
+ [[dispatch_namespaces]]
2672
+ binding = \\"name-namespace-mock\\"
2673
+ namespace = \\"namespace-mock\\"
2674
+
2675
+ [logfwdr]
2676
+ schema = \\"\\"
2677
+
2678
+ [[logfwdr.bindings]]
2679
+ name = \\"httplogs\\"
2680
+ destination = \\"httplogs\\"
2681
+
2682
+ [[logfwdr.bindings]]
2683
+ name = \\"trace\\"
2684
+ destination = \\"trace\\"
2685
+
2686
+ [wasm_modules]
2687
+ WASM_MODULE_ONE = \\"./some_wasm.wasm\\"
2688
+ WASM_MODULE_TWO = \\"./more_wasm.wasm\\"
2689
+
2690
+ [text_blobs]
2691
+ TEXT_BLOB_ONE = \\"./my-entire-app-depends-on-this.cfg\\"
2692
+ TEXT_BLOB_TWO = \\"./the-entirety-of-human-knowledge.txt\\"
2693
+
2694
+ [data_blobs]
2695
+ DATA_BLOB_ONE = \\"DATA_BLOB_ONE\\"
2696
+ DATA_BLOB_TWO = \\"DATA_BLOB_TWO\\"
2697
+
2698
+ [[unsafe.bindings]]
2699
+ type = \\"some unsafe thing\\"
2700
+ name = \\"UNSAFE_BINDING_ONE\\"
2701
+
2702
+ [unsafe.bindings.data.some]
2703
+ unsafe = \\"thing\\"
2704
+
2705
+ [[unsafe.bindings]]
2706
+ type = \\"another unsafe thing\\"
2707
+ name = \\"UNSAFE_BINDING_TWO\\"
2708
+ data = 1_337
2709
+ "
2710
+ `);
2711
+ expect(std.out).toContain("cd isolinear-optical-chip");
2712
+
2713
+ checkFiles({
2714
+ items: {
2715
+ "isolinear-optical-chip/src/index.js": false,
2716
+ "isolinear-optical-chip/src/index.ts": {
2717
+ contents: mockDashboardScript,
2718
+ },
2719
+ "isolinear-optical-chip/package.json": {
2720
+ contents: expect.objectContaining({
2721
+ name: "isolinear-optical-chip",
2722
+ }),
2723
+ },
2724
+ "isolinear-optical-chip/tsconfig.json": true,
2725
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2726
+ ...mockConfigExpected,
2727
+ name: "isolinear-optical-chip",
2728
+ }),
2729
+ },
2730
+ });
2731
+ });
2732
+
2733
+ it("should download source script from dashboard as plain JavaScript", async () => {
2734
+ mockSupportingDashRequests({
2735
+ expectedAccountId: "LCARS",
2736
+ expectedScriptName: "isolinear-optical-chip",
2737
+ expectedEnvironment: "test",
2738
+ expectedCompatDate: "1987-9-27",
2739
+ });
2740
+ setMockFetchDashScript(mockDashboardScript);
2741
+ mockConfirm(
2742
+ {
2743
+ text: "Would you like to use git to manage this Worker?",
2744
+ result: false,
2745
+ },
2746
+ {
2747
+ text: "No package.json found. Would you like to create one?",
2748
+ result: true,
2749
+ },
2750
+ {
2751
+ text: "Would you like to use TypeScript?",
2752
+ result: false,
2753
+ }
2754
+ );
2755
+
2756
+ await runWrangler("init --from-dash isolinear-optical-chip");
2757
+
2758
+ checkFiles({
2759
+ items: {
2760
+ "isolinear-optical-chip/src/index.js": {
2761
+ contents: mockDashboardScript,
2762
+ },
2763
+ "isolinear-optical-chip/src/index.ts": false,
2764
+ "isolinear-optical-chip/package.json": {
2765
+ contents: expect.objectContaining({
2766
+ name: "isolinear-optical-chip",
2767
+ }),
2768
+ },
2769
+ "isolinear-optical-chip/tsconfig.json": false,
2770
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2771
+ ...mockConfigExpected,
2772
+ name: "isolinear-optical-chip",
2773
+ }),
2774
+ },
2775
+ });
2776
+ });
2777
+
2778
+ it("should use fallback compatibility date if none is upstream", async () => {
2779
+ const mockDate = "1988-08-07";
2780
+ jest
2781
+ .spyOn(Date.prototype, "toISOString")
2782
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2783
+
2784
+ mockSupportingDashRequests({
2785
+ expectedAccountId: "LCARS",
2786
+ expectedScriptName: "isolinear-optical-chip",
2787
+ expectedEnvironment: "test",
2788
+ expectedCompatDate: undefined,
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: "No package.json found. Would you like to create one?",
2798
+ result: true,
2799
+ },
2800
+ {
2801
+ text: "Would you like to use TypeScript?",
2802
+ result: true,
2803
+ }
2804
+ );
2805
+
2806
+ await runWrangler("init --from-dash isolinear-optical-chip");
2807
+
2808
+ mockConfigExpected.compatibility_date = "1988-08-07";
2809
+ checkFiles({
2810
+ items: {
2811
+ "isolinear-optical-chip/src/index.ts": {
2812
+ contents: mockDashboardScript,
2813
+ },
2814
+ "isolinear-optical-chip/package.json": {
2815
+ contents: expect.objectContaining({
2816
+ name: "isolinear-optical-chip",
2817
+ }),
2818
+ },
2819
+ "isolinear-optical-chip/tsconfig.json": true,
2820
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2821
+ ...mockConfigExpected,
2822
+ name: "isolinear-optical-chip",
2823
+ }),
2824
+ },
2825
+ });
2826
+ });
2827
+
2828
+ it("should throw an error to retry if a request fails", async () => {
2829
+ msw.use(
2830
+ rest.get(
2831
+ `*/accounts/:accountId/workers/services/:scriptName`,
2832
+ (req, res, ctx) => {
2833
+ expect(req.params.accountId).toBe("LCARS");
2834
+ expect(req.params.scriptName).toBe("isolinear-optical-chip");
2835
+ return res.once(
2836
+ ctx.status(200),
2837
+ ctx.json({
2838
+ success: true,
2839
+ errors: [],
2840
+ messages: [],
2841
+ result: mockServiceMetadata,
2842
+ })
2843
+ );
2844
+ }
2845
+ ),
2846
+ rest.get(
2847
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2848
+ (req, res) => {
2849
+ return res.networkError("Mock Network Error");
2850
+ }
2851
+ )
2852
+ );
2853
+
2854
+ setMockFetchDashScript(mockDashboardScript);
2855
+ mockConfirm(
2856
+ {
2857
+ text: "Would you like to use git to manage this Worker?",
2858
+ result: false,
2859
+ },
2860
+ {
2861
+ text: "Would you like to use TypeScript?",
2862
+ result: false,
2863
+ }
2864
+ );
2865
+
2866
+ await expect(
2867
+ runWrangler("init --from-dash isolinear-optical-chip")
2868
+ ).rejects.toThrowError();
2869
+ });
2870
+
2871
+ it("should not include migrations in config file when none are necessary", async () => {
2872
+ const mockDate = "1988-08-07";
2873
+ jest
2874
+ .spyOn(Date.prototype, "toISOString")
2875
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2876
+ const mockData = {
2877
+ id: "memory-crystal",
2878
+ default_environment: {
2879
+ environment: "test",
2880
+ created_on: "1988-08-07",
2881
+ modified_on: "1988-08-07",
2882
+ script: {
2883
+ id: "memory-crystal",
2884
+ tag: "test-tag",
2885
+ etag: "some-etag",
2886
+ handlers: [],
2887
+ modified_on: "1988-08-07",
2888
+ created_on: "1988-08-07",
2889
+ usage_model: "bundled",
2890
+ compatibility_date: "1988-08-07",
2891
+ },
2892
+ },
2893
+ environments: [],
2894
+ };
2895
+ msw.use(
2896
+ rest.get(
2897
+ `*/accounts/:accountId/workers/services/:scriptName`,
2898
+ (req, res, ctx) => {
2899
+ return res.once(
2900
+ ctx.status(200),
2901
+ ctx.json({
2902
+ success: true,
2903
+ errors: [],
2904
+ messages: [],
2905
+ result: mockData,
2906
+ })
2907
+ );
2908
+ }
2909
+ ),
2910
+ rest.get(
2911
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2912
+ (req, res, ctx) => {
2913
+ return res.once(
2914
+ ctx.status(200),
2915
+ ctx.json({
2916
+ success: true,
2917
+ errors: [],
2918
+ messages: [],
2919
+ result: [],
2920
+ })
2921
+ );
2922
+ }
2923
+ ),
2924
+ rest.get(
2925
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2926
+ (req, res, ctx) => {
2927
+ return res.once(
2928
+ ctx.status(200),
2929
+ ctx.json({
2930
+ success: true,
2931
+ errors: [],
2932
+ messages: [],
2933
+ result: [],
2934
+ })
2935
+ );
2936
+ }
2937
+ ),
2938
+ rest.get(
2939
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2940
+ (req, res, ctx) => {
2941
+ return res.once(
2942
+ ctx.status(200),
2943
+ ctx.json({
2944
+ success: true,
2945
+ errors: [],
2946
+ messages: [],
2947
+ result: mockServiceMetadata.default_environment,
2948
+ })
2949
+ );
2950
+ }
2951
+ ),
2952
+ rest.get(
2953
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2954
+ (req, res, ctx) => {
2955
+ return res.once(
2956
+ ctx.status(200),
2957
+ ctx.json({
2958
+ success: true,
2959
+ errors: [],
2960
+ messages: [],
2961
+ result: { schedules: [] },
2962
+ })
2963
+ );
2964
+ }
2965
+ )
2966
+ );
2967
+
2968
+ setMockFetchDashScript(mockDashboardScript);
2969
+
2970
+ mockConfirm(
2971
+ {
2972
+ text: "Would you like to use git to manage this Worker?",
2973
+ result: false,
2974
+ },
2975
+
2976
+ {
2977
+ text: "No package.json found. Would you like to create one?",
2978
+ result: true,
2979
+ },
2980
+ {
2981
+ text: "Would you like to use TypeScript?",
2982
+ result: true,
2983
+ }
2984
+ );
2985
+
2986
+ await runWrangler("init --from-dash isolinear-optical-chip");
2987
+
2988
+ checkFiles({
2989
+ items: {
2990
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2991
+ compatibility_date: "1988-08-07",
2992
+ env: {},
2993
+ main: "src/index.ts",
2994
+ triggers: {
2995
+ crons: [],
2996
+ },
2997
+ usage_model: "bundled",
2998
+ name: "isolinear-optical-chip",
2999
+ }),
3000
+ },
3001
+ });
3002
+ });
3003
+
3004
+ it("should not continue if no worker name is provided", async () => {
3005
+ await expect(
3006
+ runWrangler("init --from-dash")
3007
+ ).rejects.toMatchInlineSnapshot(
3008
+ `[Error: Not enough arguments following: from-dash]`
3009
+ );
3010
+ checkFiles({
3011
+ items: {
3012
+ "isolinear-optical-chip/src/index.js": false,
3013
+ "isolinear-optical-chip/src/index.ts": false,
3014
+ "isolinear-optical-chip/package.json": false,
3015
+ "isolinear-optical-chip/tsconfig.json": false,
3016
+ "isolinear-optical-chip/wrangler.toml": false,
3017
+ },
3018
+ });
3019
+ });
3020
+ });
3021
+ });
3022
+
3023
+ function getDefaultBranchName() {
3024
+ try {
3025
+ const { stdout: defaultBranchName } = execaSync("git", [
3026
+ "config",
3027
+ "--get",
3028
+ "init.defaultBranch",
3029
+ ]);
3030
+
3031
+ return defaultBranchName;
3032
+ } catch {
3033
+ // ew
3034
+ return "master";
3035
+ }
3036
+ }
3037
+
3038
+ /**
3039
+ * Mock setter for usage within test blocks for dashboard script
3040
+ */
3041
+ export function setMockFetchDashScript(mockResponse: string) {
3042
+ msw.use(
3043
+ rest.get(
3044
+ `*/accounts/:accountId/workers/services/:fromDashScriptName/environments/:environment/content`,
3045
+ (req, res, ctx) => {
3046
+ // This is a fake FormData object, until we can get MSW (beta) that supports FormData
3047
+ const mockForm = new FormData();
3048
+ mockForm.append("name", mockResponse);
3049
+ // Sending it as JSON will result in empty object, but cause the fetchDashboardScript to execute its code
3050
+ return res(ctx.status(200), ctx.json(mockForm));
3051
+ }
3052
+ )
3053
+ );
3054
+ // Mock the actual return value that FormData would return
3055
+ (fetchDashboardScript as jest.Mock).mockImplementationOnce(() => {
3056
+ return mockResponse;
3057
+ });
3058
+ }
3059
+
3060
+ /**
3061
+ * Change the current working directory, ensuring that this exists.
3062
+ */
3063
+ function setWorkingDirectory(directory: string) {
3064
+ fs.mkdirSync(directory, { recursive: true });
3065
+ process.chdir(directory);
3066
+ }
3067
+
3068
+ /**
3069
+ * Write the given folder/files to disk at the given `cwd`.
3070
+ */
3071
+ function writeFiles(folder: TestFolder, cwd = process.cwd()) {
3072
+ for (const name in folder.items) {
3073
+ const item = folder.items[name];
3074
+ const itemPath = path.resolve(cwd, name);
3075
+ if (typeof item !== "boolean") {
3076
+ if ("contents" in item) {
3077
+ fs.mkdirSync(path.dirname(itemPath), { recursive: true });
3078
+ fs.writeFileSync(itemPath, stringify(name, item.contents));
3079
+ } else {
3080
+ fs.mkdirSync(itemPath, { recursive: true });
3081
+ writeFiles(item, itemPath);
3082
+ }
3083
+ } else {
3084
+ throw new Error("Cannot write boolean flags to disk: " + itemPath);
3085
+ }
3086
+ }
3087
+ }
3088
+
3089
+ /**
3090
+ * Check that the given test folders/files match what is in on disk.
3091
+ */
3092
+ function checkFiles(folder: TestFolder, cwd = process.cwd()) {
3093
+ for (const name in folder.items) {
3094
+ const item = folder.items[name];
3095
+ const itemPath = path.resolve(cwd, name);
3096
+ if (typeof item === "boolean") {
3097
+ if (fs.existsSync(itemPath) !== item) {
3098
+ throw new Error(`Expected ${itemPath} ${item ? "" : "not "}to exist.`);
3099
+ }
3100
+ } else if ("contents" in item) {
3101
+ const actualContents = parse(name, fs.readFileSync(itemPath, "utf-8"));
3102
+ expect(actualContents).toEqual(item.contents);
3103
+ } else if ("items" in item) {
3104
+ checkFiles(item, itemPath);
3105
+ } else {
3106
+ throw new Error("Unexpected TestFile object.");
3107
+ }
3108
+ }
3109
+ }
3110
+
3111
+ function stringify(name: string, value: unknown) {
3112
+ if (name.endsWith(".toml")) {
3113
+ return TOML.stringify(value as TOML.JsonMap);
3114
+ }
3115
+ if (name.endsWith(".json")) {
3116
+ return JSON.stringify(value);
3117
+ }
3118
+ return `${value}`;
3119
+ }
3120
+
3121
+ function parse(name: string, value: string): unknown {
3122
+ if (name.endsWith(".toml")) {
3123
+ return TOML.parse(value);
3124
+ }
3125
+ if (name.endsWith("tsconfig.json")) {
3126
+ return parseConfigFileTextToJson(name, value);
3127
+ }
3128
+ if (name.endsWith(".json")) {
3129
+ return JSON.parse(value);
3130
+ }
3131
+ return value;
3132
+ }
3133
+
3134
+ function wranglerToml(options: TOML.JsonMap = {}): TestFile {
3135
+ return {
3136
+ contents: options,
3137
+ };
3138
+ }
3139
+
3140
+ interface TestFile {
3141
+ contents: unknown;
3142
+ }
3143
+ interface TestFolder {
3144
+ items: {
3145
+ [id: string]: TestFile | TestFolder | boolean;
3146
+ };
3147
+ }