wrangler 0.0.0-e95e5a0 → 0.0.0-e98613f8

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 (305) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +26 -4
  3. package/bin/wrangler.js +166 -34
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +1456 -21
  6. package/package.json +191 -148
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +209 -0
  9. package/src/__tests__/api-devregistry.test.js +56 -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 +30 -24
  13. package/src/__tests__/configuration.test.ts +3970 -3114
  14. package/src/__tests__/d1.test.ts +80 -0
  15. package/src/__tests__/delete.test.ts +263 -0
  16. package/src/__tests__/deployments.test.ts +91 -0
  17. package/src/__tests__/dev.test.tsx +1528 -737
  18. package/src/__tests__/dialogs.test.tsx +40 -0
  19. package/src/__tests__/generate.test.ts +235 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +68 -68
  22. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  23. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-cfetch.ts +216 -94
  26. package/src/__tests__/helpers/mock-console.ts +58 -39
  27. package/src/__tests__/helpers/mock-dialogs.ts +114 -51
  28. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  29. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  30. package/src/__tests__/helpers/mock-istty.ts +65 -18
  31. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  32. package/src/__tests__/helpers/mock-kv.ts +27 -27
  33. package/src/__tests__/helpers/mock-oauth-flow.ts +229 -228
  34. package/src/__tests__/helpers/mock-process.ts +39 -0
  35. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  36. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  45. package/src/__tests__/helpers/msw/index.ts +21 -0
  46. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  47. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  48. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  49. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  51. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  53. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  54. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  55. package/src/__tests__/https-options.test.ts +142 -109
  56. package/src/__tests__/index.test.ts +246 -231
  57. package/src/__tests__/init.test.ts +2887 -1488
  58. package/src/__tests__/jest.setup.ts +163 -30
  59. package/src/__tests__/kv.test.ts +1660 -1439
  60. package/src/__tests__/logger.test.ts +100 -74
  61. package/src/__tests__/logout.test.ts +47 -0
  62. package/src/__tests__/metrics.test.ts +450 -0
  63. package/src/__tests__/middleware.scheduled.test.ts +135 -0
  64. package/src/__tests__/middleware.test.ts +726 -0
  65. package/src/__tests__/package-manager.test.ts +312 -165
  66. package/src/__tests__/pages-deployment-tail.test.ts +820 -0
  67. package/src/__tests__/pages.test.ts +2435 -322
  68. package/src/__tests__/parse.test.ts +253 -245
  69. package/src/__tests__/paths.test.ts +39 -0
  70. package/src/__tests__/publish.test.ts +7536 -4778
  71. package/src/__tests__/pubsub.test.ts +370 -0
  72. package/src/__tests__/queues.test.ts +522 -0
  73. package/src/__tests__/r2.test.ts +388 -209
  74. package/src/__tests__/route.test.ts +18 -18
  75. package/src/__tests__/secret.test.ts +624 -426
  76. package/src/__tests__/tail.test.ts +776 -567
  77. package/src/__tests__/test-old-node-version.js +31 -0
  78. package/src/__tests__/type-generation.test.ts +226 -0
  79. package/src/__tests__/user.test.ts +117 -119
  80. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  81. package/src/__tests__/validate-dev-props.test.ts +56 -0
  82. package/src/__tests__/version.test.ts +35 -0
  83. package/src/__tests__/whoami.test.tsx +264 -91
  84. package/src/__tests__/worker-namespace.test.ts +326 -0
  85. package/src/abort.d.ts +3 -0
  86. package/src/api/dev.ts +210 -0
  87. package/src/api/index.ts +2 -0
  88. package/src/bundle-reporter.tsx +68 -0
  89. package/src/bundle.ts +789 -135
  90. package/src/cfetch/index.ts +123 -67
  91. package/src/cfetch/internal.ts +216 -90
  92. package/src/cli.ts +23 -8
  93. package/src/config/config.ts +236 -195
  94. package/src/config/diagnostics.ts +61 -61
  95. package/src/config/environment.ts +506 -339
  96. package/src/config/index.ts +309 -38
  97. package/src/config/validation-helpers.ts +379 -357
  98. package/src/config/validation.ts +1942 -1263
  99. package/src/config-cache.ts +81 -41
  100. package/src/create-worker-preview.ts +239 -132
  101. package/src/create-worker-upload-form.ts +300 -224
  102. package/src/d1/backups.tsx +223 -0
  103. package/src/d1/constants.ts +2 -0
  104. package/src/d1/create.tsx +63 -0
  105. package/src/d1/delete.tsx +56 -0
  106. package/src/d1/execute.tsx +352 -0
  107. package/src/d1/formatTimeAgo.ts +14 -0
  108. package/src/d1/index.ts +101 -0
  109. package/src/d1/list.tsx +48 -0
  110. package/src/d1/migrations/apply.tsx +184 -0
  111. package/src/d1/migrations/create.tsx +77 -0
  112. package/src/d1/migrations/helpers.ts +147 -0
  113. package/src/d1/migrations/index.tsx +3 -0
  114. package/src/d1/migrations/list.tsx +79 -0
  115. package/src/d1/migrations/options.ts +18 -0
  116. package/src/d1/options.ts +22 -0
  117. package/src/d1/types.tsx +24 -0
  118. package/src/d1/utils.ts +48 -0
  119. package/src/delete.ts +108 -0
  120. package/src/deployments.ts +77 -0
  121. package/src/deprecated/index.ts +438 -0
  122. package/src/dev/dev-vars.ts +20 -17
  123. package/src/dev/dev.tsx +549 -353
  124. package/src/dev/get-local-persistence-path.tsx +31 -0
  125. package/src/dev/local.tsx +870 -292
  126. package/src/dev/remote.tsx +591 -201
  127. package/src/dev/start-server.ts +539 -0
  128. package/src/dev/use-esbuild.ts +183 -97
  129. package/src/dev/validate-dev-props.ts +31 -0
  130. package/src/dev-registry.tsx +201 -0
  131. package/src/dev.tsx +913 -0
  132. package/src/dialogs.tsx +146 -65
  133. package/src/dispatch-namespace.ts +207 -0
  134. package/src/durable.ts +102 -0
  135. package/src/entry.ts +248 -186
  136. package/src/environment-variables.ts +38 -23
  137. package/src/errors.ts +6 -6
  138. package/src/git-client.ts +135 -0
  139. package/src/global-wrangler-config-path.ts +26 -0
  140. package/src/https-options.ts +84 -80
  141. package/src/index.tsx +636 -2617
  142. package/src/init.ts +970 -0
  143. package/src/inspect.ts +706 -562
  144. package/src/intl-polyfill.d.ts +123 -123
  145. package/src/is-ci.ts +14 -0
  146. package/src/is-interactive.ts +16 -0
  147. package/src/kv/helpers.ts +430 -0
  148. package/src/kv/index.ts +596 -0
  149. package/src/logger.ts +67 -41
  150. package/src/metrics/index.ts +5 -0
  151. package/src/metrics/metrics-config.ts +239 -0
  152. package/src/metrics/metrics-dispatcher.ts +96 -0
  153. package/src/metrics/metrics-usage-headers.ts +24 -0
  154. package/src/metrics/send-event.ts +96 -0
  155. package/src/miniflare-cli/assets.ts +220 -0
  156. package/src/miniflare-cli/index.ts +205 -33
  157. package/src/miniflare-cli/request-context.ts +18 -18
  158. package/src/module-collection.ts +221 -212
  159. package/src/open-in-browser.ts +5 -14
  160. package/src/package-manager.ts +182 -90
  161. package/src/pages/build.tsx +270 -0
  162. package/src/pages/constants.ts +18 -0
  163. package/src/pages/deployment-tails.tsx +284 -0
  164. package/src/pages/deployments.tsx +84 -0
  165. package/src/pages/dev.tsx +706 -0
  166. package/src/pages/errors.ts +66 -0
  167. package/src/pages/functions/buildPlugin.ts +107 -0
  168. package/src/pages/functions/buildWorker.ts +166 -0
  169. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  170. package/src/pages/functions/filepath-routing.ts +189 -0
  171. package/src/pages/functions/identifiers.ts +78 -0
  172. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  173. package/src/pages/functions/routes-consolidation.ts +73 -0
  174. package/src/pages/functions/routes-transformation.test.ts +282 -0
  175. package/src/pages/functions/routes-transformation.ts +115 -0
  176. package/src/pages/functions/routes-validation.test.ts +403 -0
  177. package/src/pages/functions/routes-validation.ts +202 -0
  178. package/src/pages/functions/routes.ts +151 -0
  179. package/src/pages/functions.tsx +82 -0
  180. package/src/pages/hash.tsx +13 -0
  181. package/src/pages/index.tsx +102 -0
  182. package/src/pages/projects.tsx +169 -0
  183. package/src/pages/prompt-select-project.tsx +31 -0
  184. package/src/pages/publish.tsx +451 -0
  185. package/src/pages/types.ts +41 -0
  186. package/src/pages/upload.tsx +426 -0
  187. package/src/pages/utils.ts +23 -0
  188. package/src/parse.ts +223 -120
  189. package/src/paths.ts +53 -8
  190. package/src/proxy.ts +628 -383
  191. package/src/publish/index.ts +270 -0
  192. package/src/publish/publish.ts +1035 -0
  193. package/src/pubsub/index.ts +286 -0
  194. package/src/pubsub/pubsub-commands.tsx +624 -0
  195. package/src/queues/cli/commands/consumer/add.ts +71 -0
  196. package/src/queues/cli/commands/consumer/index.ts +22 -0
  197. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  198. package/src/queues/cli/commands/create.ts +25 -0
  199. package/src/queues/cli/commands/delete.ts +26 -0
  200. package/src/queues/cli/commands/index.ts +36 -0
  201. package/src/queues/cli/commands/list.ts +25 -0
  202. package/src/queues/client.ts +135 -0
  203. package/src/queues/utils.ts +18 -0
  204. package/src/r2/constants.ts +4 -0
  205. package/src/r2/helpers.ts +131 -0
  206. package/src/r2/index.ts +289 -0
  207. package/src/routes.ts +140 -0
  208. package/src/secret/index.ts +378 -0
  209. package/src/selfsigned.d.ts +23 -23
  210. package/src/sites.tsx +281 -202
  211. package/src/tail/createTail.ts +379 -0
  212. package/src/tail/filters.ts +108 -110
  213. package/src/tail/index.ts +211 -296
  214. package/src/tail/printing.ts +86 -46
  215. package/src/type-generation.ts +173 -0
  216. package/src/update-check.ts +11 -11
  217. package/src/user/access.ts +69 -0
  218. package/src/user/choose-account.tsx +69 -0
  219. package/src/user/env-vars.ts +46 -0
  220. package/src/user/generate-auth-url.ts +33 -0
  221. package/src/user/generate-random-state.ts +16 -0
  222. package/src/user/index.ts +3 -0
  223. package/src/user/user.tsx +1198 -0
  224. package/src/utils/collectKeyValues.ts +14 -0
  225. package/src/whoami.tsx +141 -42
  226. package/src/worker.ts +174 -101
  227. package/src/yargs-types.ts +24 -0
  228. package/src/zones.ts +191 -0
  229. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  230. package/templates/checked-fetch.js +17 -0
  231. package/templates/d1-beta-facade.js +205 -0
  232. package/templates/first-party-worker-module-facade.ts +18 -0
  233. package/templates/format-dev-errors.ts +32 -0
  234. package/templates/init-tests/test-jest-new-worker.js +25 -0
  235. package/templates/init-tests/test-vitest-new-worker.js +26 -0
  236. package/templates/middleware/common.ts +62 -0
  237. package/templates/middleware/loader-modules.ts +84 -0
  238. package/templates/middleware/loader-sw.ts +227 -0
  239. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  240. package/templates/middleware/middleware-pretty-error.ts +40 -0
  241. package/templates/middleware/middleware-scheduled.ts +15 -0
  242. package/templates/new-worker-scheduled.js +17 -0
  243. package/templates/new-worker-scheduled.ts +32 -0
  244. package/templates/new-worker.js +3 -3
  245. package/templates/new-worker.ts +18 -3
  246. package/templates/no-op-worker.js +10 -0
  247. package/templates/pages-dev-pipeline.ts +28 -0
  248. package/templates/pages-dev-util.ts +52 -0
  249. package/templates/pages-shim.ts +9 -0
  250. package/templates/pages-template-plugin.ts +171 -0
  251. package/templates/pages-template-worker.ts +189 -0
  252. package/templates/serve-static-assets.ts +63 -0
  253. package/templates/service-bindings-module-facade.js +54 -0
  254. package/templates/service-bindings-sw-facade.js +42 -0
  255. package/templates/tsconfig.json +96 -95
  256. package/wrangler-dist/cli.d.ts +893 -0
  257. package/wrangler-dist/cli.js +95201 -44118
  258. package/pages/functions/buildPlugin.ts +0 -92
  259. package/pages/functions/buildWorker.ts +0 -138
  260. package/pages/functions/filepath-routing.ts +0 -189
  261. package/pages/functions/identifiers.ts +0 -78
  262. package/pages/functions/routes.ts +0 -156
  263. package/pages/functions/template-plugin.ts +0 -146
  264. package/pages/functions/template-worker.ts +0 -143
  265. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  266. package/src/kv.ts +0 -319
  267. package/src/miniflare-cli/enum-keys.ts +0 -17
  268. package/src/pages.tsx +0 -1866
  269. package/src/publish.ts +0 -469
  270. package/src/r2.ts +0 -50
  271. package/src/user.tsx +0 -1193
  272. package/templates/static-asset-facade.js +0 -47
  273. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  274. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  275. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  276. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  277. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  278. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  279. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  280. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  281. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  282. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  283. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  284. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  285. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  286. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  287. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  288. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  289. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  290. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  291. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  292. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  293. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  294. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  295. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  296. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  297. package/vendor/wrangler-mime/LICENSE +0 -21
  298. package/vendor/wrangler-mime/Mime.js +0 -97
  299. package/vendor/wrangler-mime/README.md +0 -187
  300. package/vendor/wrangler-mime/cli.js +0 -46
  301. package/vendor/wrangler-mime/index.js +0 -4
  302. package/vendor/wrangler-mime/lite.js +0 -4
  303. package/vendor/wrangler-mime/package.json +0 -52
  304. package/vendor/wrangler-mime/types/other.js +0 -1
  305. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,630 +1,1098 @@
1
1
  import * as fs from "node:fs";
2
+ import getPort from "get-port";
2
3
  import patchConsole from "patch-console";
3
4
  import dedent from "ts-dedent";
4
5
  import Dev from "../dev/dev";
6
+ import { CI } from "../is-ci";
5
7
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
6
8
  import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
7
9
  import { mockConsoleMethods } from "./helpers/mock-console";
10
+ import {
11
+ msw,
12
+ mswSuccessOauthHandlers,
13
+ mswSuccessUserHandlers,
14
+ mswZoneHandlers,
15
+ } from "./helpers/msw";
8
16
  import { runInTempDir } from "./helpers/run-in-tmp";
9
17
  import { runWrangler } from "./helpers/run-wrangler";
10
18
  import writeWranglerToml from "./helpers/write-wrangler-toml";
11
19
 
12
20
  describe("wrangler dev", () => {
13
- mockAccountId();
14
- mockApiToken();
15
- runInTempDir();
16
- const std = mockConsoleMethods();
17
- afterEach(() => {
18
- (Dev as jest.Mock).mockClear();
19
- patchConsole(() => {});
20
- unsetAllMocks();
21
- });
22
-
23
- describe("compatibility-date", () => {
24
- it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
25
- fs.writeFileSync("index.js", `export default {};`);
26
- await runWrangler("dev index.js");
27
- expect(std.out).toMatchInlineSnapshot(`""`);
28
- expect(std.warn).toMatchInlineSnapshot(`""`);
29
- expect(std.err).toMatchInlineSnapshot(`""`);
30
- });
31
-
32
- it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
33
- writeWranglerToml({
34
- main: "index.js",
35
- compatibility_date: undefined,
36
- });
37
- fs.writeFileSync("index.js", `export default {};`);
38
- await runWrangler("dev");
39
- const currentDate = new Date().toISOString().substring(0, 10);
40
- expect(std.out).toMatchInlineSnapshot(`""`);
41
- expect(std.warn.replaceAll(currentDate, "<current-date>"))
42
- .toMatchInlineSnapshot(`
43
- "▲ [WARNING] No compatibility_date was specified. Using today's date: <current-date>.
44
-
45
- Add one to your wrangler.toml file:
46
- \`\`\`
47
- compatibility_date = \\"<current-date>\\"
48
- \`\`\`
49
- or pass it in your terminal:
50
- \`\`\`
51
- --compatibility-date=<current-date>
52
- \`\`\`
53
- See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.
54
-
55
- "
56
- `);
57
- expect(std.err).toMatchInlineSnapshot(`""`);
58
- });
59
-
60
- it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
61
- writeWranglerToml({
62
- main: "index.js",
63
- compatibility_date: undefined,
64
- });
65
- fs.writeFileSync("index.js", `export default {};`);
66
- await runWrangler("dev --compatibility-date=2020-01-01");
67
- expect(std.out).toMatchInlineSnapshot(`""`);
68
- expect(std.warn).toMatchInlineSnapshot(`""`);
69
- expect(std.err).toMatchInlineSnapshot(`""`);
70
- });
71
- });
72
-
73
- describe("entry-points", () => {
74
- it("should error if there is no entry-point specified", async () => {
75
- writeWranglerToml();
76
-
77
- await expect(
78
- runWrangler("dev")
79
- ).rejects.toThrowErrorMatchingInlineSnapshot(
80
- `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
81
- );
82
-
83
- expect(std.out).toMatchInlineSnapshot(`
84
- "
85
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
86
- `);
87
- expect(std.err).toMatchInlineSnapshot(`
88
- "X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
89
-
90
- "
91
- `);
92
- });
93
-
94
- it("should use `main` from the top-level environment", async () => {
95
- writeWranglerToml({
96
- main: "index.js",
97
- });
98
- fs.writeFileSync("index.js", `export default {};`);
99
- await runWrangler("dev");
100
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
101
- /index\.js$/
102
- );
103
- expect(std.out).toMatchInlineSnapshot(`""`);
104
- expect(std.warn).toMatchInlineSnapshot(`""`);
105
- expect(std.err).toMatchInlineSnapshot(`""`);
106
- });
107
-
108
- it("should use `main` from a named environment", async () => {
109
- writeWranglerToml({
110
- env: {
111
- ENV1: {
112
- main: "index.js",
113
- },
114
- },
115
- });
116
- fs.writeFileSync("index.js", `export default {};`);
117
- await runWrangler("dev --env=ENV1");
118
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
119
- /index\.js$/
120
- );
121
- expect(std.out).toMatchInlineSnapshot(`""`);
122
- expect(std.warn).toMatchInlineSnapshot(`""`);
123
- expect(std.err).toMatchInlineSnapshot(`""`);
124
- });
125
-
126
- it("should use `main` from a named environment, rather than the top-level", async () => {
127
- writeWranglerToml({
128
- main: "other.js",
129
- env: {
130
- ENV1: {
131
- main: "index.js",
132
- },
133
- },
134
- });
135
- fs.writeFileSync("index.js", `export default {};`);
136
- await runWrangler("dev --env=ENV1");
137
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
138
- /index\.js$/
139
- );
140
- expect(std.out).toMatchInlineSnapshot(`""`);
141
- expect(std.warn).toMatchInlineSnapshot(`""`);
142
- expect(std.err).toMatchInlineSnapshot(`""`);
143
- });
144
- });
145
-
146
- describe("host", () => {
147
- it("should resolve a host to its zone", async () => {
148
- writeWranglerToml({
149
- main: "index.js",
150
- });
151
- fs.writeFileSync("index.js", `export default {};`);
152
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
153
- await runWrangler("dev --host some-host.com");
154
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
155
- host: "some-host.com",
156
- id: "some-zone-id",
157
- });
158
- });
159
-
160
- it("should read wrangler.toml's dev.host", async () => {
161
- writeWranglerToml({
162
- main: "index.js",
163
- dev: {
164
- host: "some-host.com",
165
- },
166
- });
167
- fs.writeFileSync("index.js", `export default {};`);
168
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
169
- await runWrangler("dev");
170
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
171
- "some-host.com"
172
- );
173
- });
174
-
175
- it("should read --route", async () => {
176
- writeWranglerToml({
177
- main: "index.js",
178
- });
179
- fs.writeFileSync("index.js", `export default {};`);
180
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
181
- await runWrangler("dev --route http://some-host.com/some/path/*");
182
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
183
- "some-host.com"
184
- );
185
- });
186
-
187
- it("should read wrangler.toml's routes", async () => {
188
- writeWranglerToml({
189
- main: "index.js",
190
- routes: [
191
- "http://some-host.com/some/path/*",
192
- "http://some-other-host.com/path/*",
193
- ],
194
- });
195
- fs.writeFileSync("index.js", `export default {};`);
196
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
197
- await runWrangler("dev");
198
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
199
- "some-host.com"
200
- );
201
- });
202
-
203
- it("should read wrangler.toml's environment specific routes", async () => {
204
- writeWranglerToml({
205
- main: "index.js",
206
- routes: [
207
- "http://a-host.com/some/path/*",
208
- "http://another-host.com/path/*",
209
- ],
210
- env: {
211
- staging: {
212
- routes: [
213
- "http://some-host.com/some/path/*",
214
- "http://some-other-host.com/path/*",
215
- ],
216
- },
217
- },
218
- });
219
- fs.writeFileSync("index.js", `export default {};`);
220
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
221
- await runWrangler("dev --env staging");
222
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
223
- "some-host.com"
224
- );
225
- });
226
-
227
- it("should, when provided, use a configured zone_id", async () => {
228
- writeWranglerToml({
229
- main: "index.js",
230
- routes: [
231
- { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
232
- ],
233
- });
234
- fs.writeFileSync("index.js", `export default {};`);
235
- await runWrangler("dev");
236
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
237
- host: "some-domain.com",
238
- id: "some-zone-id",
239
- });
240
- });
241
-
242
- it("should, when provided, use a zone_name to get a zone_id", async () => {
243
- writeWranglerToml({
244
- main: "index.js",
245
- routes: [
246
- { pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
247
- ],
248
- });
249
- fs.writeFileSync("index.js", `export default {};`);
250
- mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
251
- await runWrangler("dev");
252
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
253
- // note that it uses the provided zone_name as a host too
254
- host: "some-zone.com",
255
- id: "a-zone-id",
256
- });
257
- });
258
-
259
- it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
260
- writeWranglerToml({
261
- main: "index.js",
262
- });
263
- fs.writeFileSync("index.js", `export default {};`);
264
- mockGetZones("111.222.333.some-host.com", []);
265
- mockGetZones("222.333.some-host.com", []);
266
- mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
267
- await runWrangler("dev --host 111.222.333.some-host.com");
268
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
269
- host: "111.222.333.some-host.com",
270
- id: "some-zone-id",
271
- });
272
- });
273
-
274
- it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
275
- // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
276
-
277
- fs.writeFileSync("index.js", `export default {};`);
278
-
279
- // config.routes
280
- mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
281
- writeWranglerToml({
282
- main: "index.js",
283
- routes: ["http://5.some-host.com/some/path/*"],
284
- });
285
- await runWrangler("dev");
286
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
287
- host: "5.some-host.com",
288
- id: "some-zone-id-5",
289
- });
290
- (Dev as jest.Mock).mockClear();
291
-
292
- // config.route
293
- mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
294
- writeWranglerToml({
295
- main: "index.js",
296
- route: "https://4.some-host.com/some/path/*",
297
- });
298
- await runWrangler("dev");
299
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
300
- host: "4.some-host.com",
301
- id: "some-zone-id-4",
302
- });
303
- (Dev as jest.Mock).mockClear();
304
-
305
- // --routes
306
- mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
307
- writeWranglerToml({
308
- main: "index.js",
309
- route: "https://4.some-host.com/some/path/*",
310
- });
311
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
312
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
313
- host: "3.some-host.com",
314
- id: "some-zone-id-3",
315
- });
316
- (Dev as jest.Mock).mockClear();
317
-
318
- // config.dev.host
319
- mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
320
- writeWranglerToml({
321
- main: "index.js",
322
- dev: {
323
- host: `2.some-host.com`,
324
- },
325
- route: "4.some-host.com/some/path/*",
326
- });
327
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
328
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
329
- host: "2.some-host.com",
330
- id: "some-zone-id-2",
331
- });
332
- (Dev as jest.Mock).mockClear();
333
-
334
- // --host
335
- mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
336
- writeWranglerToml({
337
- main: "index.js",
338
- dev: {
339
- host: `2.some-host.com`,
340
- },
341
- route: "4.some-host.com/some/path/*",
342
- });
343
- await runWrangler(
344
- "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
345
- );
346
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
347
- host: "1.some-host.com",
348
- id: "some-zone-id-1",
349
- });
350
- (Dev as jest.Mock).mockClear();
351
- });
352
-
353
- it("should error if a host can't resolve to a zone", async () => {
354
- writeWranglerToml({
355
- main: "index.js",
356
- });
357
- fs.writeFileSync("index.js", `export default {};`);
358
- mockGetZones("some-host.com", []);
359
- await expect(
360
- runWrangler("dev --host some-host.com")
361
- ).rejects.toThrowErrorMatchingInlineSnapshot(
362
- `"Could not find zone for some-host.com"`
363
- );
364
- });
365
-
366
- it("should not try to resolve a zone when starting in local mode", async () => {
367
- writeWranglerToml({
368
- main: "index.js",
369
- });
370
- fs.writeFileSync("index.js", `export default {};`);
371
- await runWrangler("dev --host some-host.com --local");
372
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
373
- });
374
- });
375
-
376
- describe("custom builds", () => {
377
- it("should run a custom build before starting `dev`", async () => {
378
- writeWranglerToml({
379
- build: {
380
- command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
381
- },
382
- });
383
-
384
- await runWrangler("dev index.js");
385
-
386
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
387
- `"export default { fetch(){ return new Response(123) } }"`
388
- );
389
-
390
- // and the command would pass through
391
- expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
392
- command:
393
- "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
394
- cwd: undefined,
395
- watch_dir: "src",
396
- });
397
- expect(std.out).toMatchInlineSnapshot(
398
- `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
399
- );
400
- expect(std.err).toMatchInlineSnapshot(`""`);
401
- expect(std.warn).toMatchInlineSnapshot(`""`);
402
- });
403
-
404
- if (process.platform !== "win32") {
405
- it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
406
- writeWranglerToml({
407
- build: {
408
- command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
409
- },
410
- });
411
-
412
- await runWrangler("dev index.js");
413
-
414
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
415
- "export default { fetch(){ return new Response(123) } }
416
- "
417
- `);
418
-
419
- expect(std.out).toMatchInlineSnapshot(
420
- `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
421
- );
422
- expect(std.err).toMatchInlineSnapshot(`""`);
423
- expect(std.warn).toMatchInlineSnapshot(`""`);
424
- });
425
- }
426
-
427
- it("should throw an error if the entry doesn't exist after the build finishes", async () => {
428
- writeWranglerToml({
429
- main: "index.js",
430
- build: {
431
- command: `node -e "console.log('custom build');"`,
432
- },
433
- });
434
-
435
- await expect(
436
- runWrangler("dev")
437
- ).rejects.toThrowErrorMatchingInlineSnapshot(
438
- `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\""`
439
- );
440
- expect(std.out).toMatchInlineSnapshot(`
441
- "Running custom build: node -e \\"console.log('custom build');\\"
442
-
443
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
444
- `);
445
- expect(std.err).toMatchInlineSnapshot(`
446
- "X [ERROR] Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
447
-
448
- "
449
- `);
450
- expect(std.warn).toMatchInlineSnapshot(`""`);
451
- });
452
- });
453
-
454
- describe("upstream-protocol", () => {
455
- it("should default upstream-protocol to `https`", async () => {
456
- writeWranglerToml({
457
- main: "index.js",
458
- });
459
- fs.writeFileSync("index.js", `export default {};`);
460
- await runWrangler("dev");
461
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
462
- "https"
463
- );
464
- expect(std.out).toMatchInlineSnapshot(`""`);
465
- expect(std.warn).toMatchInlineSnapshot(`""`);
466
- expect(std.err).toMatchInlineSnapshot(`""`);
467
- });
468
-
469
- it("should warn if `--upstream-protocol=http` is used", async () => {
470
- writeWranglerToml({
471
- main: "index.js",
472
- });
473
- fs.writeFileSync("index.js", `export default {};`);
474
- await runWrangler("dev --upstream-protocol=http");
475
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
476
- "http"
477
- );
478
- expect(std.out).toMatchInlineSnapshot(`""`);
479
- expect(std.warn).toMatchInlineSnapshot(`
480
- "▲ [WARNING] Setting upstream-protocol to http is not currently implemented.
481
-
482
- If this is required in your project, please add your use case to the following issue:
483
- https://github.com/cloudflare/wrangler2/issues/583.
484
-
485
- "
486
- `);
487
- expect(std.err).toMatchInlineSnapshot(`""`);
488
- });
489
- });
490
-
491
- describe("local-protocol", () => {
492
- it("should default local-protocol to `http`", async () => {
493
- writeWranglerToml({
494
- main: "index.js",
495
- });
496
- fs.writeFileSync("index.js", `export default {};`);
497
- await runWrangler("dev");
498
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
499
- expect(std.out).toMatchInlineSnapshot(`""`);
500
- expect(std.warn).toMatchInlineSnapshot(`""`);
501
- expect(std.err).toMatchInlineSnapshot(`""`);
502
- });
503
-
504
- it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
505
- writeWranglerToml({
506
- main: "index.js",
507
- dev: {
508
- local_protocol: "https",
509
- },
510
- });
511
- fs.writeFileSync("index.js", `export default {};`);
512
- await runWrangler("dev");
513
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
514
- "https"
515
- );
516
- expect(std.out).toMatchInlineSnapshot(`""`);
517
- expect(std.warn).toMatchInlineSnapshot(`""`);
518
- expect(std.err).toMatchInlineSnapshot(`""`);
519
- });
520
-
521
- it("should use --local-protocol command line arg, if provided", async () => {
522
- // Here we show that the command line overrides the wrangler.toml by
523
- // setting the config to https, and then setting it back to http on the command line.
524
- writeWranglerToml({
525
- main: "index.js",
526
- dev: {
527
- local_protocol: "https",
528
- },
529
- });
530
- fs.writeFileSync("index.js", `export default {};`);
531
- await runWrangler("dev --local-protocol=http");
532
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
533
- expect(std.out).toMatchInlineSnapshot(`""`);
534
- expect(std.warn).toMatchInlineSnapshot(`""`);
535
- expect(std.err).toMatchInlineSnapshot(`""`);
536
- });
537
- });
538
-
539
- describe("ip", () => {
540
- it("should default ip to localhost", async () => {
541
- writeWranglerToml({
542
- main: "index.js",
543
- });
544
- fs.writeFileSync("index.js", `export default {};`);
545
- await runWrangler("dev");
546
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
547
- expect(std.out).toMatchInlineSnapshot(`""`);
548
- expect(std.warn).toMatchInlineSnapshot(`""`);
549
- expect(std.err).toMatchInlineSnapshot(`""`);
550
- });
551
-
552
- it("should use to `ip` from `wrangler.toml`, if available", async () => {
553
- writeWranglerToml({
554
- main: "index.js",
555
- dev: {
556
- ip: "0.0.0.0",
557
- },
558
- });
559
- fs.writeFileSync("index.js", `export default {};`);
560
- await runWrangler("dev");
561
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
562
- expect(std.out).toMatchInlineSnapshot(`""`);
563
- expect(std.warn).toMatchInlineSnapshot(`""`);
564
- expect(std.err).toMatchInlineSnapshot(`""`);
565
- });
566
-
567
- it("should use --ip command line arg, if provided", async () => {
568
- writeWranglerToml({
569
- main: "index.js",
570
- dev: {
571
- ip: "1.1.1.1",
572
- },
573
- });
574
- fs.writeFileSync("index.js", `export default {};`);
575
- await runWrangler("dev --ip=0.0.0.0");
576
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
577
- expect(std.out).toMatchInlineSnapshot(`""`);
578
- expect(std.warn).toMatchInlineSnapshot(`""`);
579
- expect(std.err).toMatchInlineSnapshot(`""`);
580
- });
581
- });
582
-
583
- describe("durable_objects", () => {
584
- it("should warn if there is one or more remote Durable Object", async () => {
585
- writeWranglerToml({
586
- main: "index.js",
587
- durable_objects: {
588
- bindings: [
589
- { name: "NAME_1", class_name: "CLASS_1" },
590
- {
591
- name: "NAME_2",
592
- class_name: "CLASS_2",
593
- script_name: "SCRIPT_A",
594
- },
595
- { name: "NAME_3", class_name: "CLASS_3" },
596
- {
597
- name: "NAME_4",
598
- class_name: "CLASS_4",
599
- script_name: "SCRIPT_B",
600
- },
601
- ],
602
- },
603
- });
604
- fs.writeFileSync("index.js", `export default {};`);
605
- await runWrangler("dev");
606
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
607
- expect(std.out).toMatchInlineSnapshot(`""`);
608
- expect(std.warn).toMatchInlineSnapshot(`
609
- "▲ [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
610
-
611
- Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
612
- live instances.
613
- Remote Durable Objects that are affected:
614
- - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
615
- - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
616
-
617
- "
618
- `);
619
- expect(std.err).toMatchInlineSnapshot(`""`);
620
- });
621
- });
622
-
623
- describe(".dev.vars", () => {
624
- it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
625
- fs.writeFileSync("index.js", `export default {};`);
626
-
627
- const localVarsEnvContent = dedent`
21
+ beforeEach(() => {
22
+ msw.use(
23
+ ...mswZoneHandlers,
24
+ ...mswSuccessOauthHandlers,
25
+ ...mswSuccessUserHandlers
26
+ );
27
+ });
28
+
29
+ runInTempDir();
30
+ mockAccountId();
31
+ mockApiToken();
32
+ const std = mockConsoleMethods();
33
+ afterEach(() => {
34
+ (Dev as jest.Mock).mockClear();
35
+ patchConsole(() => {});
36
+ unsetAllMocks();
37
+ });
38
+
39
+ describe("authorization", () => {
40
+ mockApiToken({ apiToken: null });
41
+ const isCISpy = jest.spyOn(CI, "isCI").mockReturnValue(true);
42
+
43
+ it("should kick you to the login flow when running wrangler dev in remote mode without authorization", async () => {
44
+ fs.writeFileSync("index.js", `export default {};`);
45
+ await expect(
46
+ runWrangler("dev index.js")
47
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
48
+ `"You must be logged in to use wrangler dev in remote mode. Try logging in, or run wrangler dev --local."`
49
+ );
50
+ });
51
+
52
+ isCISpy.mockClear();
53
+ });
54
+
55
+ describe("compatibility-date", () => {
56
+ it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
57
+ fs.writeFileSync("index.js", `export default {};`);
58
+ await runWrangler("dev index.js");
59
+ expect(std.out).toMatchInlineSnapshot(`""`);
60
+ expect(std.warn).toMatchInlineSnapshot(`""`);
61
+ expect(std.err).toMatchInlineSnapshot(`""`);
62
+ });
63
+
64
+ it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
65
+ writeWranglerToml({
66
+ main: "index.js",
67
+ compatibility_date: undefined,
68
+ });
69
+ fs.writeFileSync("index.js", `export default {};`);
70
+ await runWrangler("dev");
71
+ const currentDate = new Date().toISOString().substring(0, 10);
72
+ expect(std.out).toMatchInlineSnapshot(`""`);
73
+ expect(std.warn.replaceAll(currentDate, "<current-date>"))
74
+ .toMatchInlineSnapshot(`
75
+ "▲ [WARNING] No compatibility_date was specified. Using today's date: <current-date>.
76
+
77
+ Add one to your wrangler.toml file:
78
+ \`\`\`
79
+ compatibility_date = \\"<current-date>\\"
80
+ \`\`\`
81
+ or pass it in your terminal:
82
+ \`\`\`
83
+ --compatibility-date=<current-date>
84
+ \`\`\`
85
+ See https://developers.cloudflare.com/workers/platform/compatibility-dates/ for more information.
86
+
87
+ "
88
+ `);
89
+ expect(std.err).toMatchInlineSnapshot(`""`);
90
+ });
91
+
92
+ it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
93
+ writeWranglerToml({
94
+ main: "index.js",
95
+ compatibility_date: undefined,
96
+ });
97
+ fs.writeFileSync("index.js", `export default {};`);
98
+ await runWrangler("dev --compatibility-date=2020-01-01");
99
+ expect(std.out).toMatchInlineSnapshot(`""`);
100
+ expect(std.warn).toMatchInlineSnapshot(`""`);
101
+ expect(std.err).toMatchInlineSnapshot(`""`);
102
+ });
103
+ });
104
+
105
+ describe("usage-model", () => {
106
+ it("should read wrangler.toml's usage_model", async () => {
107
+ writeWranglerToml({
108
+ main: "index.js",
109
+ usage_model: "unbound",
110
+ });
111
+ fs.writeFileSync("index.js", `export default {};`);
112
+ await runWrangler("dev");
113
+ expect((Dev as jest.Mock).mock.calls[0][0].usageModel).toEqual("unbound");
114
+ });
115
+
116
+ it("should read wrangler.toml's usage_model in local mode", async () => {
117
+ writeWranglerToml({
118
+ main: "index.js",
119
+ usage_model: "unbound",
120
+ });
121
+ fs.writeFileSync("index.js", `export default {};`);
122
+ await runWrangler("dev --local");
123
+ expect((Dev as jest.Mock).mock.calls[0][0].usageModel).toEqual("unbound");
124
+ });
125
+ });
126
+
127
+ describe("entry-points", () => {
128
+ it("should error if there is no entry-point specified", async () => {
129
+ writeWranglerToml();
130
+
131
+ await expect(
132
+ runWrangler("dev")
133
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
134
+ `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
135
+ );
136
+
137
+ expect(std.out).toMatchInlineSnapshot(`
138
+ "
139
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
140
+ `);
141
+ expect(std.err).toMatchInlineSnapshot(`
142
+ "X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
143
+
144
+ "
145
+ `);
146
+ });
147
+
148
+ it("should use `main` from the top-level environment", async () => {
149
+ writeWranglerToml({
150
+ main: "index.js",
151
+ });
152
+ fs.writeFileSync("index.js", `export default {};`);
153
+ await runWrangler("dev");
154
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
155
+ /index\.js$/
156
+ );
157
+ expect(std.out).toMatchInlineSnapshot(`""`);
158
+ expect(std.warn).toMatchInlineSnapshot(`""`);
159
+ expect(std.err).toMatchInlineSnapshot(`""`);
160
+ });
161
+
162
+ it("should use `main` from a named environment", async () => {
163
+ writeWranglerToml({
164
+ env: {
165
+ ENV1: {
166
+ main: "index.js",
167
+ },
168
+ },
169
+ });
170
+ fs.writeFileSync("index.js", `export default {};`);
171
+ await runWrangler("dev --env=ENV1");
172
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
173
+ /index\.js$/
174
+ );
175
+ expect(std.out).toMatchInlineSnapshot(`""`);
176
+ expect(std.warn).toMatchInlineSnapshot(`""`);
177
+ expect(std.err).toMatchInlineSnapshot(`""`);
178
+ });
179
+
180
+ it("should use `main` from a named environment, rather than the top-level", async () => {
181
+ writeWranglerToml({
182
+ main: "other.js",
183
+ env: {
184
+ ENV1: {
185
+ main: "index.js",
186
+ },
187
+ },
188
+ });
189
+ fs.writeFileSync("index.js", `export default {};`);
190
+ await runWrangler("dev --env=ENV1");
191
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
192
+ /index\.js$/
193
+ );
194
+ expect(std.out).toMatchInlineSnapshot(`""`);
195
+ expect(std.warn).toMatchInlineSnapshot(`""`);
196
+ expect(std.err).toMatchInlineSnapshot(`""`);
197
+ });
198
+ });
199
+
200
+ describe("routes", () => {
201
+ it("should pass routes to <Dev/>", async () => {
202
+ fs.writeFileSync("index.js", `export default {};`);
203
+
204
+ // config.routes
205
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
206
+ writeWranglerToml({
207
+ main: "index.js",
208
+ routes: ["http://5.some-host.com/some/path/*"],
209
+ });
210
+ await runWrangler("dev");
211
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
212
+ expect.objectContaining({
213
+ host: "5.some-host.com",
214
+ zone: "some-zone-id-5",
215
+ routes: ["http://5.some-host.com/some/path/*"],
216
+ })
217
+ );
218
+ });
219
+ });
220
+ describe("host", () => {
221
+ it("should resolve a host to its zone", async () => {
222
+ writeWranglerToml({
223
+ main: "index.js",
224
+ });
225
+ fs.writeFileSync("index.js", `export default {};`);
226
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
227
+ await runWrangler("dev --host some-host.com");
228
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
229
+ expect.objectContaining({
230
+ host: "some-host.com",
231
+ zone: "some-zone-id",
232
+ })
233
+ );
234
+ });
235
+
236
+ it("should read wrangler.toml's dev.host", async () => {
237
+ writeWranglerToml({
238
+ main: "index.js",
239
+ dev: {
240
+ host: "some-host.com",
241
+ },
242
+ });
243
+ fs.writeFileSync("index.js", `export default {};`);
244
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
245
+ await runWrangler("dev");
246
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
247
+ });
248
+
249
+ it("should read --route", async () => {
250
+ writeWranglerToml({
251
+ main: "index.js",
252
+ });
253
+ fs.writeFileSync("index.js", `export default {};`);
254
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
255
+ await runWrangler("dev --route http://some-host.com/some/path/*");
256
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
257
+ });
258
+
259
+ it("should read wrangler.toml's routes", async () => {
260
+ writeWranglerToml({
261
+ main: "index.js",
262
+ routes: [
263
+ "http://some-host.com/some/path/*",
264
+ "http://some-other-host.com/path/*",
265
+ ],
266
+ });
267
+ fs.writeFileSync("index.js", `export default {};`);
268
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
269
+ await runWrangler("dev");
270
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
271
+ });
272
+
273
+ it("should read wrangler.toml's environment specific routes", async () => {
274
+ writeWranglerToml({
275
+ main: "index.js",
276
+ routes: [
277
+ "http://a-host.com/some/path/*",
278
+ "http://another-host.com/path/*",
279
+ ],
280
+ env: {
281
+ staging: {
282
+ routes: [
283
+ "http://some-host.com/some/path/*",
284
+ "http://some-other-host.com/path/*",
285
+ ],
286
+ },
287
+ },
288
+ });
289
+ fs.writeFileSync("index.js", `export default {};`);
290
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
291
+ await runWrangler("dev --env staging");
292
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
293
+ });
294
+
295
+ it("should strip leading `*` from given host when deducing a zone id", async () => {
296
+ writeWranglerToml({
297
+ main: "index.js",
298
+ route: "*some-host.com/some/path/*",
299
+ });
300
+ fs.writeFileSync("index.js", `export default {};`);
301
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
302
+ await runWrangler("dev");
303
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
304
+ });
305
+
306
+ it("should strip leading `*.` from given host when deducing a zone id", async () => {
307
+ writeWranglerToml({
308
+ main: "index.js",
309
+ route: "*.some-host.com/some/path/*",
310
+ });
311
+ fs.writeFileSync("index.js", `export default {};`);
312
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
313
+ await runWrangler("dev");
314
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
315
+ });
316
+
317
+ it("should, when provided, use a configured zone_id", async () => {
318
+ writeWranglerToml({
319
+ main: "index.js",
320
+ routes: [
321
+ { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
322
+ ],
323
+ });
324
+ fs.writeFileSync("index.js", `export default {};`);
325
+ await runWrangler("dev");
326
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
327
+ expect.objectContaining({
328
+ host: "some-domain.com",
329
+ zone: "some-zone-id",
330
+ })
331
+ );
332
+ });
333
+
334
+ it("should, when provided, use a zone_name to get a zone_id", async () => {
335
+ writeWranglerToml({
336
+ main: "index.js",
337
+ routes: [
338
+ {
339
+ pattern: "https://some-zone.com/*",
340
+ zone_name: "some-zone.com",
341
+ },
342
+ ],
343
+ });
344
+ fs.writeFileSync("index.js", `export default {};`);
345
+ mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
346
+ await runWrangler("dev");
347
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
348
+ expect.objectContaining({
349
+ // note that it uses the provided zone_name as a host too
350
+ host: "some-zone.com",
351
+ zone: "a-zone-id",
352
+ })
353
+ );
354
+ });
355
+
356
+ it("should find the host from the given pattern, not zone_name", async () => {
357
+ writeWranglerToml({
358
+ main: "index.js",
359
+ routes: [
360
+ {
361
+ pattern: "https://subdomain.exists.com/*",
362
+ zone_name: "does-not-exist.com",
363
+ },
364
+ ],
365
+ });
366
+ await fs.promises.writeFile("index.js", `export default {};`);
367
+ await runWrangler("dev");
368
+ expect(std.out).toMatchInlineSnapshot(`""`);
369
+ expect(std.err).toMatchInlineSnapshot(`""`);
370
+ });
371
+
372
+ it("should fail for non-existing zones", async () => {
373
+ writeWranglerToml({
374
+ main: "index.js",
375
+ routes: [
376
+ {
377
+ pattern: "https://subdomain.does-not-exist.com/*",
378
+ zone_name: "exists.com",
379
+ },
380
+ ],
381
+ });
382
+ await fs.promises.writeFile("index.js", `export default {};`);
383
+ await expect(runWrangler("dev")).rejects.toEqual(
384
+ new Error("Could not find zone for subdomain.does-not-exist.com")
385
+ );
386
+ });
387
+
388
+ it("should fail for non-existing zones, when falling back from */*", async () => {
389
+ writeWranglerToml({
390
+ main: "index.js",
391
+ routes: [
392
+ {
393
+ pattern: "*/*",
394
+ zone_name: "does-not-exist.com",
395
+ },
396
+ ],
397
+ });
398
+ await fs.promises.writeFile("index.js", `export default {};`);
399
+ await expect(runWrangler("dev")).rejects.toEqual(
400
+ new Error("Could not find zone for does-not-exist.com")
401
+ );
402
+ });
403
+
404
+ it("should fallback to zone_name when given the pattern */*", async () => {
405
+ writeWranglerToml({
406
+ main: "index.js",
407
+ routes: [
408
+ {
409
+ pattern: "*/*",
410
+ zone_name: "exists.com",
411
+ },
412
+ ],
413
+ });
414
+ await fs.promises.writeFile("index.js", `export default {};`);
415
+ await runWrangler("dev");
416
+ expect(std.out).toMatchInlineSnapshot(`""`);
417
+ expect(std.err).toMatchInlineSnapshot(`""`);
418
+ });
419
+ it("fails when given the pattern */* and no zone_name", async () => {
420
+ writeWranglerToml({
421
+ main: "index.js",
422
+ routes: [
423
+ {
424
+ pattern: "*/*",
425
+ zone_id: "exists-com",
426
+ },
427
+ ],
428
+ });
429
+ await fs.promises.writeFile("index.js", `export default {};`);
430
+ const err = new TypeError() as unknown as { code: string; input: string };
431
+ err.code = "ERR_INVALID_URL";
432
+ err.input = "http:///";
433
+
434
+ await expect(runWrangler("dev")).rejects.toEqual(err);
435
+ });
436
+
437
+ it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
438
+ writeWranglerToml({
439
+ main: "index.js",
440
+ });
441
+ fs.writeFileSync("index.js", `export default {};`);
442
+ mockGetZones("111.222.333.some-host.com", []);
443
+ mockGetZones("222.333.some-host.com", []);
444
+ mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
445
+ await runWrangler("dev --host 111.222.333.some-host.com");
446
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
447
+ expect.objectContaining({
448
+ host: "111.222.333.some-host.com",
449
+ zone: "some-zone-id",
450
+ })
451
+ );
452
+ });
453
+
454
+ it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
455
+ // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
456
+
457
+ fs.writeFileSync("index.js", `export default {};`);
458
+
459
+ // config.routes
460
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
461
+ writeWranglerToml({
462
+ main: "index.js",
463
+ routes: ["http://5.some-host.com/some/path/*"],
464
+ });
465
+ await runWrangler("dev");
466
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
467
+ expect.objectContaining({
468
+ host: "5.some-host.com",
469
+ zone: "some-zone-id-5",
470
+ })
471
+ );
472
+ (Dev as jest.Mock).mockClear();
473
+
474
+ // config.route
475
+ mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
476
+ writeWranglerToml({
477
+ main: "index.js",
478
+ route: "https://4.some-host.com/some/path/*",
479
+ });
480
+ await runWrangler("dev");
481
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
482
+ expect.objectContaining({
483
+ host: "4.some-host.com",
484
+ zone: "some-zone-id-4",
485
+ })
486
+ );
487
+ (Dev as jest.Mock).mockClear();
488
+
489
+ // --routes
490
+ mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
491
+ writeWranglerToml({
492
+ main: "index.js",
493
+ route: "https://4.some-host.com/some/path/*",
494
+ });
495
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
496
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
497
+ expect.objectContaining({
498
+ host: "3.some-host.com",
499
+ zone: "some-zone-id-3",
500
+ })
501
+ );
502
+ (Dev as jest.Mock).mockClear();
503
+
504
+ // config.dev.host
505
+ mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
506
+ writeWranglerToml({
507
+ main: "index.js",
508
+ dev: {
509
+ host: `2.some-host.com`,
510
+ },
511
+ route: "4.some-host.com/some/path/*",
512
+ });
513
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
514
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
515
+ expect.objectContaining({
516
+ host: "2.some-host.com",
517
+ zone: "some-zone-id-2",
518
+ })
519
+ );
520
+ (Dev as jest.Mock).mockClear();
521
+
522
+ // --host
523
+ mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
524
+ writeWranglerToml({
525
+ main: "index.js",
526
+ dev: {
527
+ host: `2.some-host.com`,
528
+ },
529
+ route: "4.some-host.com/some/path/*",
530
+ });
531
+ await runWrangler(
532
+ "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
533
+ );
534
+ expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
535
+ expect.objectContaining({
536
+ host: "1.some-host.com",
537
+ zone: "some-zone-id-1",
538
+ })
539
+ );
540
+ (Dev as jest.Mock).mockClear();
541
+ });
542
+
543
+ it("should error if a host can't resolve to a zone", async () => {
544
+ writeWranglerToml({
545
+ main: "index.js",
546
+ });
547
+ fs.writeFileSync("index.js", `export default {};`);
548
+ mockGetZones("some-host.com", []);
549
+ await expect(
550
+ runWrangler("dev --host some-host.com")
551
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
552
+ `"Could not find zone for some-host.com"`
553
+ );
554
+ });
555
+
556
+ it("should not try to resolve a zone when starting in local mode", async () => {
557
+ writeWranglerToml({
558
+ main: "index.js",
559
+ });
560
+ fs.writeFileSync("index.js", `export default {};`);
561
+ await runWrangler("dev --host some-host.com --local");
562
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
563
+ });
564
+ });
565
+
566
+ describe("local upstream", () => {
567
+ it("should use dev.host from toml by default", async () => {
568
+ writeWranglerToml({
569
+ main: "index.js",
570
+ dev: {
571
+ host: `2.some-host.com`,
572
+ },
573
+ });
574
+ fs.writeFileSync("index.js", `export default {};`);
575
+ await runWrangler("dev --local");
576
+ expect((Dev as jest.Mock).mock.calls[0][0].localUpstream).toEqual(
577
+ "2.some-host.com"
578
+ );
579
+ });
580
+
581
+ it("should use route from toml by default", async () => {
582
+ writeWranglerToml({
583
+ main: "index.js",
584
+ route: "https://4.some-host.com/some/path/*",
585
+ });
586
+ fs.writeFileSync("index.js", `export default {};`);
587
+ await runWrangler("dev --local");
588
+ expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual(
589
+ "4.some-host.com"
590
+ );
591
+ });
592
+
593
+ it("should respect the option when provided", async () => {
594
+ writeWranglerToml({
595
+ main: "index.js",
596
+ route: `2.some-host.com`,
597
+ });
598
+ fs.writeFileSync("index.js", `export default {};`);
599
+ await runWrangler("dev --local-upstream some-host.com --local");
600
+ expect((Dev as jest.Mock).mock.calls[0][0].localUpstream).toEqual(
601
+ "some-host.com"
602
+ );
603
+ });
604
+ });
605
+
606
+ describe("custom builds", () => {
607
+ it("should run a custom build before starting `dev`", async () => {
608
+ writeWranglerToml({
609
+ build: {
610
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
611
+ },
612
+ });
613
+
614
+ await runWrangler("dev index.js");
615
+
616
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
617
+ `"export default { fetch(){ return new Response(123) } }"`
618
+ );
619
+
620
+ // and the command would pass through
621
+ expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
622
+ command:
623
+ "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
624
+ cwd: undefined,
625
+ watch_dir: "src",
626
+ });
627
+ expect(std.out).toMatchInlineSnapshot(
628
+ `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
629
+ );
630
+ expect(std.err).toMatchInlineSnapshot(`""`);
631
+ expect(std.warn).toMatchInlineSnapshot(`""`);
632
+ });
633
+
634
+ if (process.platform !== "win32") {
635
+ it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
636
+ writeWranglerToml({
637
+ build: {
638
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
639
+ },
640
+ });
641
+
642
+ await runWrangler("dev index.js");
643
+
644
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
645
+ "export default { fetch(){ return new Response(123) } }
646
+ "
647
+ `);
648
+
649
+ expect(std.out).toMatchInlineSnapshot(
650
+ `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
651
+ );
652
+ expect(std.err).toMatchInlineSnapshot(`""`);
653
+ expect(std.warn).toMatchInlineSnapshot(`""`);
654
+ });
655
+ }
656
+
657
+ it("should throw an error if the entry doesn't exist after the build finishes", async () => {
658
+ writeWranglerToml({
659
+ main: "index.js",
660
+ build: {
661
+ command: `node -e "console.log('custom build');"`,
662
+ },
663
+ });
664
+
665
+ await expect(runWrangler("dev")).rejects
666
+ .toThrowErrorMatchingInlineSnapshot(`
667
+ "The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
668
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build."
669
+ `);
670
+ expect(std.out).toMatchInlineSnapshot(`
671
+ "Running custom build: node -e \\"console.log('custom build');\\"
672
+
673
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
674
+ `);
675
+ expect(std.err).toMatchInlineSnapshot(`
676
+ "X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
677
+
678
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build.
679
+
680
+ "
681
+ `);
682
+ expect(std.warn).toMatchInlineSnapshot(`""`);
683
+ });
684
+
685
+ describe(".env", () => {
686
+ beforeEach(() => {
687
+ fs.writeFileSync(".env", "CUSTOM_BUILD_VAR=default");
688
+ fs.writeFileSync(".env.custom", "CUSTOM_BUILD_VAR=custom");
689
+ fs.writeFileSync("index.js", `export default {};`);
690
+ writeWranglerToml({
691
+ main: "index.js",
692
+ env: { custom: {} },
693
+ build: {
694
+ // Ideally, we'd just log the var here and match it in `std.out`,
695
+ // but stdout from custom builds is piped directly to
696
+ // `process.stdout` which we don't capture.
697
+ command: `node -e "require('fs').writeFileSync('var.txt', process.env.CUSTOM_BUILD_VAR)"`,
698
+ },
699
+ });
700
+
701
+ // We won't overwrite existing process.env keys with .env values (to
702
+ // allow .env overrides to specified on then shell), so make sure this
703
+ // key definitely doesn't exist.
704
+ delete process.env.CUSTOM_BUILD_VAR;
705
+ });
706
+
707
+ it("should load environment variables from `.env`", async () => {
708
+ await runWrangler("dev");
709
+ const output = fs.readFileSync("var.txt", "utf8");
710
+ expect(output).toMatch("default");
711
+ });
712
+ it("should prefer to load environment variables from `.env.<environment>` if `--env <environment>` is set", async () => {
713
+ await runWrangler("dev --env custom");
714
+ const output = fs.readFileSync("var.txt", "utf8");
715
+ expect(output).toMatch("custom");
716
+ });
717
+ });
718
+ });
719
+
720
+ describe("upstream-protocol", () => {
721
+ it("should default upstream-protocol to `https`", async () => {
722
+ writeWranglerToml({
723
+ main: "index.js",
724
+ });
725
+ fs.writeFileSync("index.js", `export default {};`);
726
+ await runWrangler("dev");
727
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
728
+ "https"
729
+ );
730
+ expect(std.out).toMatchInlineSnapshot(`""`);
731
+ expect(std.warn).toMatchInlineSnapshot(`""`);
732
+ expect(std.err).toMatchInlineSnapshot(`""`);
733
+ });
734
+
735
+ it("should warn if `--upstream-protocol=http` is used", async () => {
736
+ writeWranglerToml({
737
+ main: "index.js",
738
+ });
739
+ fs.writeFileSync("index.js", `export default {};`);
740
+ await runWrangler("dev --upstream-protocol=http");
741
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
742
+ "http"
743
+ );
744
+ expect(std.out).toMatchInlineSnapshot(`""`);
745
+ expect(std.warn).toMatchInlineSnapshot(`
746
+ "▲ [WARNING] Setting upstream-protocol to http is not currently implemented.
747
+
748
+ If this is required in your project, please add your use case to the following issue:
749
+ https://github.com/cloudflare/wrangler2/issues/583.
750
+
751
+ "
752
+ `);
753
+ expect(std.err).toMatchInlineSnapshot(`""`);
754
+ });
755
+ });
756
+
757
+ describe("local-protocol", () => {
758
+ it("should default local-protocol to `http`", async () => {
759
+ writeWranglerToml({
760
+ main: "index.js",
761
+ });
762
+ fs.writeFileSync("index.js", `export default {};`);
763
+ await runWrangler("dev");
764
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
765
+ expect(std.out).toMatchInlineSnapshot(`""`);
766
+ expect(std.warn).toMatchInlineSnapshot(`""`);
767
+ expect(std.err).toMatchInlineSnapshot(`""`);
768
+ });
769
+
770
+ it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
771
+ writeWranglerToml({
772
+ main: "index.js",
773
+ dev: {
774
+ local_protocol: "https",
775
+ },
776
+ });
777
+ fs.writeFileSync("index.js", `export default {};`);
778
+ await runWrangler("dev");
779
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
780
+ "https"
781
+ );
782
+ expect(std.out).toMatchInlineSnapshot(`""`);
783
+ expect(std.warn).toMatchInlineSnapshot(`""`);
784
+ expect(std.err).toMatchInlineSnapshot(`""`);
785
+ });
786
+
787
+ it("should use --local-protocol command line arg, if provided", async () => {
788
+ // Here we show that the command line overrides the wrangler.toml by
789
+ // setting the config to https, and then setting it back to http on the command line.
790
+ writeWranglerToml({
791
+ main: "index.js",
792
+ dev: {
793
+ local_protocol: "https",
794
+ },
795
+ });
796
+ fs.writeFileSync("index.js", `export default {};`);
797
+ await runWrangler("dev --local-protocol=http");
798
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
799
+ expect(std.out).toMatchInlineSnapshot(`""`);
800
+ expect(std.warn).toMatchInlineSnapshot(`""`);
801
+ expect(std.err).toMatchInlineSnapshot(`""`);
802
+ });
803
+ });
804
+
805
+ describe("ip", () => {
806
+ it("should default ip to 0.0.0.0", async () => {
807
+ writeWranglerToml({
808
+ main: "index.js",
809
+ });
810
+ fs.writeFileSync("index.js", `export default {};`);
811
+ await runWrangler("dev");
812
+ expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual("0.0.0.0");
813
+ expect(std.out).toMatchInlineSnapshot(`""`);
814
+ expect(std.warn).toMatchInlineSnapshot(`""`);
815
+ expect(std.err).toMatchInlineSnapshot(`""`);
816
+ });
817
+
818
+ it("should use to `ip` from `wrangler.toml`, if available", async () => {
819
+ writeWranglerToml({
820
+ main: "index.js",
821
+ dev: {
822
+ ip: "1.2.3.4",
823
+ },
824
+ });
825
+ fs.writeFileSync("index.js", `export default {};`);
826
+ await runWrangler("dev");
827
+ expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual("1.2.3.4");
828
+ expect(std.out).toMatchInlineSnapshot(`""`);
829
+ expect(std.warn).toMatchInlineSnapshot(`""`);
830
+ expect(std.err).toMatchInlineSnapshot(`""`);
831
+ });
832
+
833
+ it("should use --ip command line arg, if provided", async () => {
834
+ writeWranglerToml({
835
+ main: "index.js",
836
+ dev: {
837
+ ip: "1.2.3.4",
838
+ },
839
+ });
840
+ fs.writeFileSync("index.js", `export default {};`);
841
+ await runWrangler("dev --ip=5.6.7.8");
842
+ expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual("5.6.7.8");
843
+ expect(std.out).toMatchInlineSnapshot(`""`);
844
+ expect(std.warn).toMatchInlineSnapshot(`""`);
845
+ expect(std.err).toMatchInlineSnapshot(`""`);
846
+ });
847
+ });
848
+
849
+ describe("inspector port", () => {
850
+ it("should connect WebSocket server with --experimental-local", async () => {
851
+ writeWranglerToml({
852
+ main: "./index.js",
853
+ });
854
+ fs.writeFileSync(
855
+ "index.js",
856
+ `export default {
857
+ async fetch(request, env, ctx ){
858
+ console.log('Hello World LOGGING');
859
+ },
860
+ };`
861
+ );
862
+ await runWrangler("dev --experimental-local");
863
+
864
+ expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9229);
865
+ expect(std).toMatchInlineSnapshot(`
866
+ Object {
867
+ "debug": "",
868
+ "err": "",
869
+ "out": "",
870
+ "warn": "",
871
+ }
872
+ `);
873
+ });
874
+
875
+ it("should use 9229 as the default port", async () => {
876
+ writeWranglerToml({
877
+ main: "index.js",
878
+ });
879
+ fs.writeFileSync("index.js", `export default {};`);
880
+ await runWrangler("dev");
881
+ expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9229);
882
+ expect(std).toMatchInlineSnapshot(`
883
+ Object {
884
+ "debug": "",
885
+ "err": "",
886
+ "out": "",
887
+ "warn": "",
888
+ }
889
+ `);
890
+ });
891
+
892
+ it("should read --inspector-port", async () => {
893
+ writeWranglerToml({
894
+ main: "index.js",
895
+ });
896
+ fs.writeFileSync("index.js", `export default {};`);
897
+ await runWrangler("dev --inspector-port=9999");
898
+ expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9999);
899
+ expect(std).toMatchInlineSnapshot(`
900
+ Object {
901
+ "debug": "",
902
+ "err": "",
903
+ "out": "",
904
+ "warn": "",
905
+ }
906
+ `);
907
+ });
908
+
909
+ it("should read dev.inspector_port from wrangler.toml", async () => {
910
+ writeWranglerToml({
911
+ main: "index.js",
912
+ dev: {
913
+ inspector_port: 9999,
914
+ },
915
+ });
916
+ fs.writeFileSync("index.js", `export default {};`);
917
+ await runWrangler("dev");
918
+ expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9999);
919
+ expect(std).toMatchInlineSnapshot(`
920
+ Object {
921
+ "debug": "",
922
+ "err": "",
923
+ "out": "",
924
+ "warn": "",
925
+ }
926
+ `);
927
+ });
928
+
929
+ it("should error if a bad dev.inspector_port config is provided", async () => {
930
+ writeWranglerToml({
931
+ main: "index.js",
932
+ dev: {
933
+ // @ts-expect-error intentionally bad port
934
+ inspector_port: "some string",
935
+ },
936
+ });
937
+ fs.writeFileSync("index.js", `export default {};`);
938
+ await expect(runWrangler("dev")).rejects
939
+ .toThrowErrorMatchingInlineSnapshot(`
940
+ "Processing wrangler.toml configuration:
941
+ - Expected \\"dev.inspector_port\\" to be of type number but got \\"some string\\"."
942
+ `);
943
+ });
944
+ });
945
+
946
+ describe("port", () => {
947
+ it("should default port to 8787 if it is not in use", async () => {
948
+ writeWranglerToml({
949
+ main: "index.js",
950
+ });
951
+ fs.writeFileSync("index.js", `export default {};`);
952
+ await runWrangler("dev");
953
+ expect((Dev as jest.Mock).mock.calls[0][0].initialPort).toEqual(8787);
954
+ expect(std.out).toMatchInlineSnapshot(`""`);
955
+ expect(std.warn).toMatchInlineSnapshot(`""`);
956
+ expect(std.err).toMatchInlineSnapshot(`""`);
957
+ });
958
+
959
+ it("should use `port` from `wrangler.toml`, if available", async () => {
960
+ writeWranglerToml({
961
+ main: "index.js",
962
+ dev: {
963
+ port: 8888,
964
+ },
965
+ });
966
+ fs.writeFileSync("index.js", `export default {};`);
967
+ // Mock `getPort()` to resolve to a completely different port.
968
+ (getPort as jest.Mock).mockResolvedValue(98765);
969
+
970
+ await runWrangler("dev");
971
+ expect((Dev as jest.Mock).mock.calls[0][0].initialPort).toEqual(8888);
972
+ expect(std.out).toMatchInlineSnapshot(`""`);
973
+ expect(std.warn).toMatchInlineSnapshot(`""`);
974
+ expect(std.err).toMatchInlineSnapshot(`""`);
975
+ });
976
+
977
+ it("should error if a bad dev.port config is provided", async () => {
978
+ writeWranglerToml({
979
+ main: "index.js",
980
+ dev: {
981
+ // @ts-expect-error intentionally bad port
982
+ port: "some string",
983
+ },
984
+ });
985
+ fs.writeFileSync("index.js", `export default {};`);
986
+ await expect(runWrangler("dev")).rejects
987
+ .toThrowErrorMatchingInlineSnapshot(`
988
+ "Processing wrangler.toml configuration:
989
+ - Expected \\"dev.port\\" to be of type number but got \\"some string\\"."
990
+ `);
991
+ });
992
+
993
+ it("should use --port command line arg, if provided", async () => {
994
+ writeWranglerToml({
995
+ main: "index.js",
996
+ dev: {
997
+ port: 8888,
998
+ },
999
+ });
1000
+ fs.writeFileSync("index.js", `export default {};`);
1001
+ // Mock `getPort()` to resolve to a completely different port.
1002
+ (getPort as jest.Mock).mockResolvedValue(98765);
1003
+
1004
+ await runWrangler("dev --port=9999");
1005
+ expect((Dev as jest.Mock).mock.calls[0][0].initialPort).toEqual(9999);
1006
+ expect(std.out).toMatchInlineSnapshot(`""`);
1007
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1008
+ expect(std.err).toMatchInlineSnapshot(`""`);
1009
+ });
1010
+
1011
+ it("should use a different port to the default if it is in use", async () => {
1012
+ writeWranglerToml({
1013
+ main: "index.js",
1014
+ });
1015
+ fs.writeFileSync("index.js", `export default {};`);
1016
+ // Mock `getPort()` to resolve to a completely different port.
1017
+ (getPort as jest.Mock).mockResolvedValue(98765);
1018
+
1019
+ await runWrangler("dev");
1020
+ expect((Dev as jest.Mock).mock.calls[0][0].initialPort).toEqual(98765);
1021
+ expect(std.out).toMatchInlineSnapshot(`""`);
1022
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1023
+ expect(std.err).toMatchInlineSnapshot(`""`);
1024
+ });
1025
+ });
1026
+
1027
+ describe("durable_objects", () => {
1028
+ it("should warn if there are remote Durable Objects, or missing migrations for local Durable Objects", async () => {
1029
+ writeWranglerToml({
1030
+ main: "index.js",
1031
+ durable_objects: {
1032
+ bindings: [
1033
+ { name: "NAME_1", class_name: "CLASS_1" },
1034
+ {
1035
+ name: "NAME_2",
1036
+ class_name: "CLASS_2",
1037
+ script_name: "SCRIPT_A",
1038
+ },
1039
+ { name: "NAME_3", class_name: "CLASS_3" },
1040
+ {
1041
+ name: "NAME_4",
1042
+ class_name: "CLASS_4",
1043
+ script_name: "SCRIPT_B",
1044
+ },
1045
+ ],
1046
+ },
1047
+ });
1048
+ fs.writeFileSync("index.js", `export default {};`);
1049
+ await runWrangler("dev");
1050
+ expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual("0.0.0.0");
1051
+ expect(std.out).toMatchInlineSnapshot(`
1052
+ "Your worker has access to the following bindings:
1053
+ - Durable Objects:
1054
+ - NAME_1: CLASS_1
1055
+ - NAME_2: CLASS_2 (defined in SCRIPT_A)
1056
+ - NAME_3: CLASS_3
1057
+ - NAME_4: CLASS_4 (defined in SCRIPT_B)"
1058
+ `);
1059
+ expect(std.warn).toMatchInlineSnapshot(`
1060
+ "▲ [WARNING] Processing wrangler.toml configuration:
1061
+
1062
+ - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS_1,
1063
+ CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
1064
+ section to your wrangler.toml. Add this configuration to your wrangler.toml:
1065
+
1066
+ \`\`\`
1067
+ [[migrations]]
1068
+ tag = \\"v1\\" # Should be unique for each entry
1069
+ new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
1070
+ \`\`\`
1071
+
1072
+ Refer to
1073
+ https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
1074
+ for more details.
1075
+
1076
+
1077
+ ▲ [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
1078
+
1079
+ Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
1080
+ live instances.
1081
+ Remote Durable Objects that are affected:
1082
+ - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
1083
+ - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
1084
+
1085
+ "
1086
+ `);
1087
+ expect(std.err).toMatchInlineSnapshot(`""`);
1088
+ });
1089
+ });
1090
+
1091
+ describe(".dev.vars", () => {
1092
+ it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
1093
+ fs.writeFileSync("index.js", `export default {};`);
1094
+
1095
+ const localVarsEnvContent = dedent`
628
1096
  # Preceding comment
629
1097
  VAR_1="var #1 value" # End of line comment
630
1098
  VAR_3="var #3 value"
@@ -634,129 +1102,452 @@ describe("wrangler dev", () => {
634
1102
  EMPTY=
635
1103
  UNQUOTED= unquoted value
636
1104
  `;
637
- fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
638
-
639
- writeWranglerToml({
640
- main: "index.js",
641
- vars: {
642
- VAR_1: "original value 1",
643
- VAR_2: "original value 2", // should not get overridden
644
- VAR_3: "original value 3",
645
- VAR_MULTI_LINE_1: "original multi-line 1",
646
- VAR_MULTI_LINE_2: "original multi-line 2",
647
- EMPTY: "original empty",
648
- UNQUOTED: "original unquoted",
649
- },
650
- });
651
- await runWrangler("dev");
652
- const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
653
- .calls[0][0].bindings.vars;
654
-
655
- expect(varBindings).toEqual({
656
- VAR_1: "var #1 value",
657
- VAR_2: "original value 2",
658
- VAR_3: "var #3 value",
659
- VAR_MULTI_LINE_1: "A: line 1\nline 2",
660
- VAR_MULTI_LINE_2: "B: line 1\nline 2",
661
- EMPTY: "",
662
- UNQUOTED: "unquoted value", // Note that whitespace is trimmed
663
- });
664
- expect(std.out).toMatchInlineSnapshot(
665
- `"Using vars defined in .dev.vars"`
666
- );
667
- expect(std.warn).toMatchInlineSnapshot(`""`);
668
- expect(std.err).toMatchInlineSnapshot(`""`);
669
- });
670
- });
671
-
672
- describe("site", () => {
673
- it("should error if --site is used with no value", async () => {
674
- await expect(
675
- runWrangler("dev --site")
676
- ).rejects.toThrowErrorMatchingInlineSnapshot(
677
- `"Not enough arguments following: site"`
678
- );
679
-
680
- expect(std).toMatchInlineSnapshot(`
681
- Object {
682
- "debug": "",
683
- "err": "wrangler dev [script]
684
-
685
- 👂 Start a local server for developing your worker
686
-
687
- Positionals:
688
- script The path to an entry point for your worker [string]
689
-
690
- Flags:
691
- -c, --config Path to .toml configuration file [string]
692
- -h, --help Show help [boolean]
693
- -v, --version Show version number [boolean]
694
- --legacy-env Use legacy environments [boolean]
695
-
696
- Options:
697
- --name Name of the worker [string]
698
- --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
699
- -e, --env Perform on a specific environment [string]
700
- --compatibility-date Date to use for compatibility checks [string]
701
- --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
702
- --latest Use the latest version of the worker runtime [boolean] [default: true]
703
- --ip IP address to listen on, defaults to \`localhost\` [string]
704
- --port Port to listen on [number]
705
- --inspector-port Port for devtools to connect to [number]
706
- --routes, --route Routes to upload [array]
707
- --host Host to forward requests to, defaults to the zone of project [string]
708
- --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
709
- --experimental-public Static assets to be served [string]
710
- --site Root folder of static assets for Workers Sites [string]
711
- --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
712
- --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
713
- --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
714
- --jsx-factory The function that is called for each JSX element [string]
715
- --jsx-fragment The function that is called for each JSX fragment [string]
716
- --tsconfig Path to a custom tsconfig.json file [string]
717
- -l, --local Run on my machine [boolean] [default: false]
718
- --minify Minify the script [boolean]
719
- --node-compat Enable node.js compatibility [boolean]
720
- --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
721
- --inspect Enable dev tools [deprecated] [boolean]
722
- X [ERROR] Not enough arguments following: site
723
-
724
- ",
725
- "out": "
726
- ",
727
- "warn": "",
728
- }
729
- `);
730
- });
731
- });
732
-
733
- describe("--inspect", () => {
734
- it("should warn if --inspect is used", async () => {
735
- fs.writeFileSync("index.js", `export default {};`);
736
- await runWrangler("dev index.js --inspect");
737
- expect(std).toMatchInlineSnapshot(`
738
- Object {
739
- "debug": "",
740
- "err": "",
741
- "out": "",
742
- "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
743
-
744
- ",
745
- }
746
- `);
747
- });
748
- });
1105
+ fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
1106
+
1107
+ writeWranglerToml({
1108
+ main: "index.js",
1109
+ vars: {
1110
+ VAR_1: "original value 1",
1111
+ VAR_2: "original value 2", // should not get overridden
1112
+ VAR_3: "original value 3",
1113
+ VAR_MULTI_LINE_1: "original multi-line 1",
1114
+ VAR_MULTI_LINE_2: "original multi-line 2",
1115
+ EMPTY: "original empty",
1116
+ UNQUOTED: "original unquoted",
1117
+ },
1118
+ });
1119
+ await runWrangler("dev");
1120
+ const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
1121
+ .calls[0][0].bindings.vars;
1122
+
1123
+ expect(varBindings).toEqual({
1124
+ VAR_1: "var #1 value",
1125
+ VAR_2: "original value 2",
1126
+ VAR_3: "var #3 value",
1127
+ VAR_MULTI_LINE_1: "A: line 1\nline 2",
1128
+ VAR_MULTI_LINE_2: "B: line 1\nline 2",
1129
+ EMPTY: "",
1130
+ UNQUOTED: "unquoted value", // Note that whitespace is trimmed
1131
+ });
1132
+ expect(std.out).toMatchInlineSnapshot(`
1133
+ "Using vars defined in .dev.vars
1134
+ Your worker has access to the following bindings:
1135
+ - Vars:
1136
+ - VAR_1: \\"(hidden)\\"
1137
+ - VAR_2: \\"original value 2\\"
1138
+ - VAR_3: \\"(hidden)\\"
1139
+ - VAR_MULTI_LINE_1: \\"(hidden)\\"
1140
+ - VAR_MULTI_LINE_2: \\"(hidden)\\"
1141
+ - EMPTY: \\"(hidden)\\"
1142
+ - UNQUOTED: \\"(hidden)\\""
1143
+ `);
1144
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1145
+ expect(std.err).toMatchInlineSnapshot(`""`);
1146
+ });
1147
+
1148
+ it("should prefer `.dev.vars.<environment>` if `--env <environment> set`", async () => {
1149
+ fs.writeFileSync("index.js", `export default {};`);
1150
+ fs.writeFileSync(".dev.vars", "DEFAULT_VAR=default");
1151
+ fs.writeFileSync(".dev.vars.custom", "CUSTOM_VAR=custom");
1152
+
1153
+ writeWranglerToml({ main: "index.js", env: { custom: {} } });
1154
+ await runWrangler("dev --env custom");
1155
+ const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
1156
+ .calls[0][0].bindings.vars;
1157
+
1158
+ expect(varBindings).toEqual({ CUSTOM_VAR: "custom" });
1159
+ expect(std.out).toMatchInlineSnapshot(`
1160
+ "Using vars defined in .dev.vars.custom
1161
+ Your worker has access to the following bindings:
1162
+ - Vars:
1163
+ - CUSTOM_VAR: \\"(hidden)\\""
1164
+ `);
1165
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1166
+ expect(std.err).toMatchInlineSnapshot(`""`);
1167
+ });
1168
+ });
1169
+
1170
+ describe("serve static assets", () => {
1171
+ it("should error if --site is used with no value", async () => {
1172
+ await expect(
1173
+ runWrangler("dev --site")
1174
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1175
+ `"Not enough arguments following: site"`
1176
+ );
1177
+
1178
+ expect(std).toMatchInlineSnapshot(`
1179
+ Object {
1180
+ "debug": "",
1181
+ "err": "X [ERROR] Not enough arguments following: site
1182
+
1183
+ ",
1184
+ "out": "
1185
+ wrangler dev [script]
1186
+
1187
+ 👂 Start a local server for developing your worker
1188
+
1189
+ Positionals:
1190
+ script The path to an entry point for your worker [string]
1191
+
1192
+ Flags:
1193
+ -c, --config Path to .toml configuration file [string]
1194
+ -e, --env Environment to use for operations and .env files [string]
1195
+ -h, --help Show help [boolean]
1196
+ -v, --version Show version number [boolean]
1197
+
1198
+ Options:
1199
+ --name Name of the worker [string]
1200
+ --no-bundle Skip internal build steps and directly publish script [boolean] [default: false]
1201
+ --compatibility-date Date to use for compatibility checks [string]
1202
+ --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
1203
+ --latest Use the latest version of the worker runtime [boolean] [default: true]
1204
+ --ip IP address to listen on [string]
1205
+ --port Port to listen on [number]
1206
+ --inspector-port Port for devtools to connect to [number]
1207
+ --routes, --route Routes to upload [array]
1208
+ --host Host to forward requests to, defaults to the zone of project [string]
1209
+ --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
1210
+ --local-upstream Host to act as origin in local mode, defaults to dev.host or route [string]
1211
+ --assets Static assets to be served [string]
1212
+ --site Root folder of static assets for Workers Sites [string]
1213
+ --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
1214
+ --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
1215
+ --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
1216
+ --var A key-value pair to be injected into the script as a variable [array]
1217
+ --define A key-value pair to be substituted in the script [array]
1218
+ --jsx-factory The function that is called for each JSX element [string]
1219
+ --jsx-fragment The function that is called for each JSX fragment [string]
1220
+ --tsconfig Path to a custom tsconfig.json file [string]
1221
+ -l, --local Run on my machine [boolean] [default: false]
1222
+ --experimental-local Run on my machine using the Cloudflare Workers runtime [boolean] [default: false]
1223
+ --experimental-local-remote-kv Read/write KV data from/to real namespaces on the Cloudflare network [boolean] [default: false]
1224
+ --minify Minify the script [boolean]
1225
+ --node-compat Enable node.js compatibility [boolean]
1226
+ --persist Enable persistence for local mode, using default path: .wrangler/state [boolean]
1227
+ --persist-to Specify directory to use for local persistence (implies --persist) [string]
1228
+ --live-reload Auto reload HTML pages when change is detected in local mode [boolean]
1229
+ --test-scheduled Test scheduled events by visiting /__scheduled in browser [boolean] [default: false]
1230
+ --log-level Specify logging level [choices: \\"debug\\", \\"info\\", \\"log\\", \\"warn\\", \\"error\\", \\"none\\"] [default: \\"log\\"]",
1231
+ "warn": "",
1232
+ }
1233
+ `);
1234
+ });
1235
+
1236
+ it("should error if --assets and --site are used together", async () => {
1237
+ writeWranglerToml({
1238
+ main: "./index.js",
1239
+ });
1240
+ fs.writeFileSync("index.js", `export default {};`);
1241
+ await expect(
1242
+ runWrangler("dev --assets abc --site xyz")
1243
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1244
+ `"Cannot use Assets and Workers Sites in the same Worker."`
1245
+ );
1246
+ });
1247
+
1248
+ it("should error if --assets and config.site are used together", async () => {
1249
+ writeWranglerToml({
1250
+ main: "./index.js",
1251
+ site: {
1252
+ bucket: "xyz",
1253
+ },
1254
+ });
1255
+ fs.writeFileSync("index.js", `export default {};`);
1256
+ await expect(
1257
+ runWrangler("dev --assets abc")
1258
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1259
+ `"Cannot use Assets and Workers Sites in the same Worker."`
1260
+ );
1261
+ });
1262
+
1263
+ it("should error if config.assets and --site are used together", async () => {
1264
+ writeWranglerToml({
1265
+ main: "./index.js",
1266
+ // @ts-expect-error we allow string inputs here
1267
+ assets: "abc",
1268
+ });
1269
+ fs.writeFileSync("index.js", `export default {};`);
1270
+ await expect(
1271
+ runWrangler("dev --site xyz")
1272
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1273
+ `"Cannot use Assets and Workers Sites in the same Worker."`
1274
+ );
1275
+ });
1276
+
1277
+ it("should error if config.assets and config.site are used together", async () => {
1278
+ writeWranglerToml({
1279
+ main: "./index.js",
1280
+ // @ts-expect-error we allow string inputs here
1281
+ assets: "abc",
1282
+ site: {
1283
+ bucket: "xyz",
1284
+ },
1285
+ });
1286
+ fs.writeFileSync("index.js", `export default {};`);
1287
+ await expect(
1288
+ runWrangler("dev --assets abc")
1289
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1290
+ `"Cannot use Assets and Workers Sites in the same Worker."`
1291
+ );
1292
+ });
1293
+
1294
+ it("should indicate whether Sites is being used", async () => {
1295
+ writeWranglerToml({
1296
+ main: "index.js",
1297
+ });
1298
+ fs.writeFileSync("index.js", `export default {};`);
1299
+
1300
+ await runWrangler("dev");
1301
+ expect((Dev as jest.Mock).mock.calls[0][0].isWorkersSite).toEqual(false);
1302
+
1303
+ await runWrangler("dev --site abc");
1304
+ expect((Dev as jest.Mock).mock.calls[1][0].isWorkersSite).toEqual(true);
1305
+
1306
+ await runWrangler("dev --assets abc");
1307
+ expect((Dev as jest.Mock).mock.calls[2][0].isWorkersSite).toEqual(false);
1308
+ });
1309
+ it("should warn if --assets is used", async () => {
1310
+ writeWranglerToml({
1311
+ main: "./index.js",
1312
+ });
1313
+ fs.writeFileSync("index.js", `export default {};`);
1314
+
1315
+ await runWrangler('dev --assets "./assets"');
1316
+ expect(std).toMatchInlineSnapshot(`
1317
+ Object {
1318
+ "debug": "",
1319
+ "err": "",
1320
+ "out": "",
1321
+ "warn": "▲ [WARNING] The --assets argument is experimental and may change or break at any time
1322
+
1323
+ ",
1324
+ }
1325
+ `);
1326
+ });
1327
+
1328
+ it("should warn if config.assets is used", async () => {
1329
+ writeWranglerToml({
1330
+ main: "./index.js",
1331
+ // @ts-expect-error we allow string inputs here
1332
+ assets: "./assets",
1333
+ });
1334
+ fs.writeFileSync("index.js", `export default {};`);
1335
+
1336
+ await runWrangler("dev");
1337
+ expect(std).toMatchInlineSnapshot(`
1338
+ Object {
1339
+ "debug": "",
1340
+ "err": "",
1341
+ "out": "",
1342
+ "warn": "▲ [WARNING] Processing wrangler.toml configuration:
1343
+
1344
+ - \\"assets\\" fields are experimental and may change or break at any time.
1345
+
1346
+ ",
1347
+ }
1348
+ `);
1349
+ });
1350
+ });
1351
+
1352
+ describe("--inspect", () => {
1353
+ it("should warn if --inspect is used", async () => {
1354
+ fs.writeFileSync("index.js", `export default {};`);
1355
+ await runWrangler("dev index.js --inspect");
1356
+ expect(std).toMatchInlineSnapshot(`
1357
+ Object {
1358
+ "debug": "",
1359
+ "err": "",
1360
+ "out": "",
1361
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1362
+
1363
+ ",
1364
+ }
1365
+ `);
1366
+ });
1367
+
1368
+ it("should default to true, without a warning", async () => {
1369
+ fs.writeFileSync("index.js", `export default {};`);
1370
+ await runWrangler("dev index.js");
1371
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1372
+ expect(std).toMatchInlineSnapshot(`
1373
+ Object {
1374
+ "debug": "",
1375
+ "err": "",
1376
+ "out": "",
1377
+ "warn": "",
1378
+ }
1379
+ `);
1380
+ });
1381
+
1382
+ it("should pass true, with a warning", async () => {
1383
+ fs.writeFileSync("index.js", `export default {};`);
1384
+ await runWrangler("dev index.js --inspect");
1385
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1386
+ expect(std).toMatchInlineSnapshot(`
1387
+ Object {
1388
+ "debug": "",
1389
+ "err": "",
1390
+ "out": "",
1391
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1392
+
1393
+ ",
1394
+ }
1395
+ `);
1396
+ });
1397
+
1398
+ it("should pass false, without a warning", async () => {
1399
+ fs.writeFileSync("index.js", `export default {};`);
1400
+ await runWrangler("dev index.js --inspect false");
1401
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(false);
1402
+ expect(std).toMatchInlineSnapshot(`
1403
+ Object {
1404
+ "debug": "",
1405
+ "err": "",
1406
+ "out": "",
1407
+ "warn": "",
1408
+ }
1409
+ `);
1410
+ });
1411
+ });
1412
+
1413
+ describe("--log-level", () => {
1414
+ it("should not output warnings with log-level 'none'", async () => {
1415
+ fs.writeFileSync("index.js", `export default {};`);
1416
+ await runWrangler("dev index.js --inspect --log-level none");
1417
+ expect(std).toMatchInlineSnapshot(`
1418
+ Object {
1419
+ "debug": "",
1420
+ "err": "",
1421
+ "out": "",
1422
+ "warn": "",
1423
+ }
1424
+ `);
1425
+ });
1426
+
1427
+ it("should output warnings with log-level 'warn'", async () => {
1428
+ fs.writeFileSync("index.js", `export default {};`);
1429
+ await runWrangler("dev index.js --inspect --log-level warn");
1430
+ expect(std).toMatchInlineSnapshot(`
1431
+ Object {
1432
+ "debug": "",
1433
+ "err": "",
1434
+ "out": "",
1435
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1436
+
1437
+ ",
1438
+ }
1439
+ `);
1440
+ });
1441
+
1442
+ it("should not output Errors with log-level error", async () => {
1443
+ fs.writeFileSync("index.js", `export default {};`);
1444
+ await runWrangler("dev index.js --inspect --log-level debug");
1445
+ expect(std.debug.length > 1).toBe(true);
1446
+ });
1447
+ });
1448
+
1449
+ describe("service bindings", () => {
1450
+ it("should warn when using service bindings", async () => {
1451
+ writeWranglerToml({
1452
+ services: [
1453
+ { binding: "WorkerA", service: "A" },
1454
+ { binding: "WorkerB", service: "B", environment: "staging" },
1455
+ ],
1456
+ });
1457
+ fs.writeFileSync("index.js", `export default {};`);
1458
+ await runWrangler("dev index.js");
1459
+ expect(std.out).toMatchInlineSnapshot(`
1460
+ "Your worker has access to the following bindings:
1461
+ - Services:
1462
+ - WorkerA: A
1463
+ - WorkerB: B - staging"
1464
+ `);
1465
+ expect(std.warn).toMatchInlineSnapshot(`
1466
+ "▲ [WARNING] Processing wrangler.toml configuration:
1467
+
1468
+ - \\"services\\" fields are experimental and may change or break at any time.
1469
+
1470
+
1471
+ ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
1472
+
1473
+ "
1474
+ `);
1475
+ expect(std.err).toMatchInlineSnapshot(`""`);
1476
+ });
1477
+ });
1478
+
1479
+ describe("print bindings", () => {
1480
+ it("should print bindings", async () => {
1481
+ writeWranglerToml({
1482
+ services: [
1483
+ { binding: "WorkerA", service: "A" },
1484
+ { binding: "WorkerB", service: "B", environment: "staging" },
1485
+ ],
1486
+ });
1487
+ fs.writeFileSync("index.js", `export default {};`);
1488
+ await runWrangler("dev index.js");
1489
+ expect(std).toMatchInlineSnapshot(`
1490
+ Object {
1491
+ "debug": "",
1492
+ "err": "",
1493
+ "out": "Your worker has access to the following bindings:
1494
+ - Services:
1495
+ - WorkerA: A
1496
+ - WorkerB: B - staging",
1497
+ "warn": "▲ [WARNING] Processing wrangler.toml configuration:
1498
+
1499
+ - \\"services\\" fields are experimental and may change or break at any time.
1500
+
1501
+
1502
+ ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
1503
+
1504
+ ",
1505
+ }
1506
+ `);
1507
+ });
1508
+
1509
+ it("should mask vars that were overriden in .dev.vars", async () => {
1510
+ writeWranglerToml({
1511
+ vars: {
1512
+ variable: 123,
1513
+ overriden: "original values",
1514
+ },
1515
+ });
1516
+ fs.writeFileSync(
1517
+ ".dev.vars",
1518
+ `
1519
+ SECRET = "A secret"
1520
+ overriden = "overriden value"
1521
+ `
1522
+ );
1523
+ fs.writeFileSync("index.js", `export default {};`);
1524
+ await runWrangler("dev index.js");
1525
+ expect(std).toMatchInlineSnapshot(`
1526
+ Object {
1527
+ "debug": "",
1528
+ "err": "",
1529
+ "out": "Using vars defined in .dev.vars
1530
+ Your worker has access to the following bindings:
1531
+ - Vars:
1532
+ - variable: 123
1533
+ - overriden: \\"(hidden)\\"
1534
+ - SECRET: \\"(hidden)\\"",
1535
+ "warn": "",
1536
+ }
1537
+ `);
1538
+ });
1539
+ });
749
1540
  });
750
1541
 
751
1542
  function mockGetZones(domain: string, zones: { id: string }[] = []) {
752
- const removeMock = setMockResponse(
753
- "/zones",
754
- "GET",
755
- (_urlPieces, _init, queryParams) => {
756
- expect([...queryParams.entries()]).toEqual([["name", domain]]);
757
- // Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
758
- removeMock();
759
- return zones;
760
- }
761
- );
1543
+ const removeMock = setMockResponse(
1544
+ "/zones",
1545
+ "GET",
1546
+ (_urlPieces, _init, queryParams) => {
1547
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
1548
+ // Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
1549
+ removeMock();
1550
+ return zones;
1551
+ }
1552
+ );
762
1553
  }