wrangler 0.0.0-e2f9bb2 → 0.0.0-e305903a

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