wrangler 0.0.0-e2b7352 → 0.0.0-e305903a

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