wrangler 0.0.0-ea8f8d7 → 0.0.0-eab9542

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