wrangler 0.0.0-e64812e → 0.0.0-e7a0cd5b

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