wrangler 0.0.0-e64812e → 0.0.0-e8e684c7

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