wrangler 0.0.0-e928f94 → 0.0.0-e9307365

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +51 -14
  3. package/bin/wrangler.js +159 -27
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +5416 -0
  6. package/package.json +172 -113
  7. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  8. package/src/__tests__/config-cache.test.ts +42 -0
  9. package/src/__tests__/configuration.test.ts +4022 -0
  10. package/src/__tests__/dev.test.tsx +1294 -0
  11. package/src/__tests__/generate.test.ts +93 -0
  12. package/src/__tests__/guess-worker-format.test.ts +85 -0
  13. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  14. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  15. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  16. package/src/__tests__/helpers/mock-bin.ts +36 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +263 -0
  18. package/src/__tests__/helpers/mock-console.ts +97 -0
  19. package/src/__tests__/helpers/mock-dialogs.ts +153 -0
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +74 -0
  22. package/src/__tests__/helpers/mock-kv.ts +33 -0
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
  24. package/src/__tests__/helpers/mock-process.ts +39 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  26. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  27. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  28. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  29. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  30. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  31. package/src/__tests__/https-options.test.ts +163 -0
  32. package/src/__tests__/index.test.ts +242 -150
  33. package/src/__tests__/init.test.ts +1992 -0
  34. package/src/__tests__/jest.setup.ts +150 -0
  35. package/src/__tests__/kv.test.ts +1671 -0
  36. package/src/__tests__/logger.test.ts +141 -0
  37. package/src/__tests__/metrics.test.ts +450 -0
  38. package/src/__tests__/package-manager.test.ts +353 -0
  39. package/src/__tests__/pages.test.ts +1261 -0
  40. package/src/__tests__/parse.test.ts +330 -0
  41. package/src/__tests__/publish.test.ts +6914 -0
  42. package/src/__tests__/pubsub.test.ts +367 -0
  43. package/src/__tests__/r2.test.ts +317 -0
  44. package/src/__tests__/route.test.ts +45 -0
  45. package/src/__tests__/secret.test.ts +492 -0
  46. package/src/__tests__/tail.test.ts +698 -0
  47. package/src/__tests__/test-old-node-version.js +31 -0
  48. package/src/__tests__/user.test.ts +144 -0
  49. package/src/__tests__/validate-dev-props.test.ts +56 -0
  50. package/src/__tests__/version.test.ts +35 -0
  51. package/src/__tests__/whoami.test.tsx +289 -0
  52. package/src/__tests__/worker-namespace.test.ts +327 -0
  53. package/src/abort.d.ts +3 -0
  54. package/src/api/dev.ts +86 -0
  55. package/src/api/index.ts +1 -0
  56. package/src/bundle-reporter.tsx +29 -0
  57. package/src/bundle.ts +472 -0
  58. package/src/cfetch/index.ts +121 -0
  59. package/src/cfetch/internal.ts +181 -0
  60. package/src/cli.ts +25 -8
  61. package/src/config/README.md +107 -0
  62. package/src/config/config.ts +263 -0
  63. package/src/config/diagnostics.ts +80 -0
  64. package/src/config/environment.ts +485 -0
  65. package/src/config/index.ts +251 -0
  66. package/src/config/validation-helpers.ts +584 -0
  67. package/src/config/validation.ts +1856 -0
  68. package/src/config-cache.ts +84 -0
  69. package/src/create-worker-preview.ts +257 -0
  70. package/src/create-worker-upload-form.ts +288 -0
  71. package/src/dev/dev-vars.ts +36 -0
  72. package/src/dev/dev.tsx +531 -0
  73. package/src/dev/local.tsx +385 -0
  74. package/src/dev/remote.tsx +413 -0
  75. package/src/dev/use-esbuild.ts +175 -0
  76. package/src/dev/validate-dev-props.ts +31 -0
  77. package/src/dev-registry.tsx +157 -0
  78. package/src/dev.tsx +604 -712
  79. package/src/dialogs.tsx +115 -65
  80. package/src/durable.ts +102 -0
  81. package/src/entry.ts +328 -0
  82. package/src/environment-variables.ts +35 -0
  83. package/src/errors.ts +11 -0
  84. package/src/faye-websocket.d.ts +6 -0
  85. package/src/generate.ts +131 -0
  86. package/src/git-client.ts +42 -0
  87. package/src/global-wrangler-config-path.ts +26 -0
  88. package/src/https-options.ts +127 -0
  89. package/src/index.tsx +2167 -1841
  90. package/src/init.ts +549 -0
  91. package/src/inspect.ts +750 -0
  92. package/src/intl-polyfill.d.ts +139 -0
  93. package/src/is-interactive.ts +12 -0
  94. package/src/kv.ts +430 -0
  95. package/src/logger.ts +73 -0
  96. package/src/metrics/index.ts +5 -0
  97. package/src/metrics/is-ci.ts +14 -0
  98. package/src/metrics/metrics-config.ts +239 -0
  99. package/src/metrics/metrics-dispatcher.ts +96 -0
  100. package/src/metrics/metrics-usage-headers.ts +24 -0
  101. package/src/metrics/send-event.ts +92 -0
  102. package/src/miniflare-cli/README.md +30 -0
  103. package/src/miniflare-cli/assets.ts +543 -0
  104. package/src/miniflare-cli/enum-keys.ts +17 -0
  105. package/src/miniflare-cli/index.ts +85 -0
  106. package/src/miniflare-cli/request-context.ts +40 -0
  107. package/src/module-collection.ts +247 -53
  108. package/src/open-in-browser.ts +17 -0
  109. package/src/package-manager.ts +219 -0
  110. package/src/pages/build.tsx +204 -0
  111. package/src/pages/constants.ts +8 -0
  112. package/src/pages/deployments.tsx +106 -0
  113. package/src/pages/dev.tsx +414 -0
  114. package/src/pages/functions/buildPlugin.ts +105 -0
  115. package/src/pages/functions/buildWorker.ts +151 -0
  116. package/src/pages/functions/filepath-routing.test.ts +234 -0
  117. package/src/pages/functions/filepath-routing.ts +189 -0
  118. package/src/pages/functions/identifiers.ts +78 -0
  119. package/src/pages/functions/routes.ts +151 -0
  120. package/src/pages/index.tsx +84 -0
  121. package/src/pages/projects.tsx +163 -0
  122. package/src/pages/publish.tsx +338 -0
  123. package/src/pages/types.ts +40 -0
  124. package/src/pages/upload.tsx +415 -0
  125. package/src/pages/utils.ts +12 -0
  126. package/src/parse.ts +286 -0
  127. package/src/paths.ts +26 -0
  128. package/src/preview.ts +31 -0
  129. package/src/proxy.ts +476 -0
  130. package/src/publish.ts +796 -335
  131. package/src/pubsub/index.ts +286 -0
  132. package/src/pubsub/pubsub-commands.tsx +623 -0
  133. package/src/r2.ts +118 -0
  134. package/src/selfsigned.d.ts +29 -0
  135. package/src/sites.tsx +357 -100
  136. package/src/tail/filters.ts +279 -0
  137. package/src/tail/index.ts +299 -0
  138. package/src/tail/printing.ts +67 -0
  139. package/src/update-check.ts +19 -0
  140. package/src/user/choose-account.tsx +69 -0
  141. package/src/user/env-vars.ts +46 -0
  142. package/src/user/generate-auth-url.ts +33 -0
  143. package/src/user/generate-random-state.ts +16 -0
  144. package/src/user/index.ts +3 -0
  145. package/src/user/user.tsx +1189 -0
  146. package/src/whoami.tsx +169 -0
  147. package/src/worker-namespace.ts +206 -0
  148. package/src/worker.ts +203 -0
  149. package/src/zones.ts +76 -0
  150. package/templates/checked-fetch.js +17 -0
  151. package/templates/first-party-worker-module-facade.ts +18 -0
  152. package/templates/format-dev-errors.ts +32 -0
  153. package/templates/gitignore +171 -0
  154. package/templates/new-worker-scheduled.js +17 -0
  155. package/templates/new-worker-scheduled.ts +32 -0
  156. package/templates/new-worker.js +15 -0
  157. package/templates/new-worker.ts +30 -0
  158. package/templates/no-op-worker.js +10 -0
  159. package/templates/pages-shim.ts +9 -0
  160. package/templates/pages-template-plugin.ts +155 -0
  161. package/templates/pages-template-worker.ts +161 -0
  162. package/templates/serve-static-assets.ts +63 -0
  163. package/templates/service-bindings-module-facade.js +51 -0
  164. package/templates/service-bindings-sw-facade.js +39 -0
  165. package/templates/tsconfig.json +105 -0
  166. package/wrangler-dist/cli.d.ts +327 -0
  167. package/wrangler-dist/cli.js +126729 -101995
  168. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  169. package/src/__tests__/mock-cfetch.js +0 -46
  170. package/src/api/form_data.ts +0 -141
  171. package/src/api/inspect.ts +0 -441
  172. package/src/api/preview.ts +0 -128
  173. package/src/api/worker.ts +0 -161
  174. package/src/cfetch.ts +0 -72
  175. package/src/config.ts +0 -122
  176. package/src/kv.tsx +0 -211
  177. package/src/pages.tsx +0 -843
  178. package/src/tail.tsx +0 -71
  179. package/src/user.tsx +0 -1029
  180. package/static-asset-facade.js +0 -47
  181. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  182. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  183. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  184. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  185. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  186. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  187. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  188. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  189. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  190. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  191. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  192. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  193. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  194. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  195. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  196. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  197. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  198. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  199. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  200. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  201. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  202. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  203. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  204. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  205. package/vendor/wrangler-mime/LICENSE +0 -21
  206. package/vendor/wrangler-mime/Mime.js +0 -97
  207. package/vendor/wrangler-mime/README.md +0 -187
  208. package/vendor/wrangler-mime/cli.js +0 -46
  209. package/vendor/wrangler-mime/index.js +0 -4
  210. package/vendor/wrangler-mime/lite.js +0 -4
  211. package/vendor/wrangler-mime/package.json +0 -52
  212. package/vendor/wrangler-mime/types/other.js +0 -1
  213. package/vendor/wrangler-mime/types/standard.js +0 -1
  214. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,4022 @@
1
+ import path from "node:path";
2
+ import { normalizeAndValidateConfig } from "../config/validation";
3
+ import { normalizeSlashes } from "./helpers/mock-console";
4
+ import type {
5
+ ConfigFields,
6
+ RawDevConfig,
7
+ RawConfig,
8
+ RawEnvironment,
9
+ } from "../config";
10
+
11
+ describe("normalizeAndValidateConfig()", () => {
12
+ it("should use defaults for empty configuration", () => {
13
+ const { config, diagnostics } = normalizeAndValidateConfig({}, undefined, {
14
+ env: undefined,
15
+ });
16
+
17
+ expect(config).toEqual({
18
+ account_id: undefined,
19
+ assets: undefined,
20
+ build: {
21
+ command: undefined,
22
+ cwd: undefined,
23
+ watch_dir: "./src",
24
+ },
25
+ compatibility_date: undefined,
26
+ compatibility_flags: [],
27
+ configPath: undefined,
28
+ dev: {
29
+ ip: "localhost",
30
+ local_protocol: "http",
31
+ port: undefined, // the default of 8787 is set at runtime
32
+ upstream_protocol: "https",
33
+ host: undefined,
34
+ },
35
+ durable_objects: {
36
+ bindings: [],
37
+ },
38
+ jsx_factory: "React.createElement",
39
+ jsx_fragment: "React.Fragment",
40
+ tsconfig: undefined,
41
+ kv_namespaces: [],
42
+ legacy_env: true,
43
+ logfwdr: {
44
+ bindings: [],
45
+ schema: undefined,
46
+ },
47
+ send_metrics: undefined,
48
+ main: undefined,
49
+ migrations: [],
50
+ name: undefined,
51
+ r2_buckets: [],
52
+ services: [],
53
+ route: undefined,
54
+ routes: undefined,
55
+ rules: [],
56
+ site: undefined,
57
+ text_blobs: undefined,
58
+ triggers: {
59
+ crons: [],
60
+ },
61
+ unsafe: {
62
+ bindings: [],
63
+ },
64
+ worker_namespaces: [],
65
+ usage_model: undefined,
66
+ vars: {},
67
+ define: {},
68
+ wasm_modules: undefined,
69
+ data_blobs: undefined,
70
+ workers_dev: undefined,
71
+ zone_id: undefined,
72
+ no_bundle: undefined,
73
+ minify: undefined,
74
+ node_compat: undefined,
75
+ first_party_worker: undefined,
76
+ });
77
+ expect(diagnostics.hasErrors()).toBe(false);
78
+ expect(diagnostics.hasWarnings()).toBe(false);
79
+ });
80
+
81
+ describe("top-level non-environment configuration", () => {
82
+ it("should override config defaults with provided values", () => {
83
+ const expectedConfig: Partial<ConfigFields<RawDevConfig>> = {
84
+ legacy_env: true,
85
+ send_metrics: false,
86
+ dev: {
87
+ ip: "255.255.255.255",
88
+ port: 9999,
89
+ local_protocol: "https",
90
+ upstream_protocol: "http",
91
+ },
92
+ };
93
+
94
+ const { config, diagnostics } = normalizeAndValidateConfig(
95
+ expectedConfig,
96
+ undefined,
97
+ { env: undefined }
98
+ );
99
+
100
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
101
+ expect(diagnostics.hasErrors()).toBe(false);
102
+ expect(diagnostics.hasWarnings()).toBe(false);
103
+ });
104
+
105
+ it("should error on invalid top level fields", () => {
106
+ const expectedConfig = {
107
+ legacy_env: "FOO",
108
+ send_metrics: "BAD",
109
+ dev: {
110
+ ip: 222,
111
+ port: "FOO",
112
+ local_protocol: "wss",
113
+ upstream_protocol: "ws",
114
+ },
115
+ };
116
+
117
+ const { config, diagnostics } = normalizeAndValidateConfig(
118
+ expectedConfig as unknown as RawConfig,
119
+ undefined,
120
+ { env: undefined }
121
+ );
122
+
123
+ expect(config).toEqual(
124
+ expect.objectContaining({ ...expectedConfig, main: undefined })
125
+ );
126
+ expect(diagnostics.hasWarnings()).toBe(false);
127
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
128
+ "Processing wrangler configuration:
129
+ - Expected \\"legacy_env\\" to be of type boolean but got \\"FOO\\".
130
+ - Expected \\"send_metrics\\" to be of type boolean but got \\"BAD\\".
131
+ - Expected \\"dev.ip\\" to be of type string but got 222.
132
+ - Expected \\"dev.port\\" to be of type number but got \\"FOO\\".
133
+ - Expected \\"dev.local_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"wss\\".
134
+ - Expected \\"dev.upstream_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"ws\\"."
135
+ `);
136
+ });
137
+
138
+ it("should warn on and remove unexpected top level fields", () => {
139
+ const expectedConfig = {
140
+ unexpected: {
141
+ subkey: "some-value",
142
+ },
143
+ };
144
+
145
+ const { config, diagnostics } = normalizeAndValidateConfig(
146
+ expectedConfig as unknown as RawConfig,
147
+ undefined,
148
+ { env: undefined }
149
+ );
150
+
151
+ expect("unexpected" in config).toBe(false);
152
+ expect(diagnostics.hasErrors()).toBe(false);
153
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
154
+ "Processing wrangler configuration:
155
+ - Unexpected fields found in top-level field: \\"unexpected\\""
156
+ `);
157
+ });
158
+
159
+ it("should report a deprecation warning if `miniflare` appears at the top level", () => {
160
+ const expectedConfig = {
161
+ miniflare: {
162
+ host: "localhost",
163
+ },
164
+ };
165
+
166
+ const { config, diagnostics } = normalizeAndValidateConfig(
167
+ expectedConfig as unknown as RawConfig,
168
+ undefined,
169
+ { env: undefined }
170
+ );
171
+
172
+ expect("miniflare" in config).toBe(false);
173
+ expect(diagnostics.hasErrors()).toBe(false);
174
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
175
+ "Processing wrangler configuration:
176
+ - 😶 Ignored: \\"miniflare\\":
177
+ Wrangler does not use configuration in the \`miniflare\` section. Unless you are using Miniflare directly you can remove this section."
178
+ `);
179
+ });
180
+
181
+ it("should normalise a blank route value to be undefined", () => {
182
+ const expectedConfig = {
183
+ route: "",
184
+ };
185
+
186
+ const { config, diagnostics } = normalizeAndValidateConfig(
187
+ expectedConfig as unknown as RawConfig,
188
+ undefined,
189
+ { env: undefined }
190
+ );
191
+
192
+ expect(config.route).toBeUndefined();
193
+ expect(diagnostics.hasErrors()).toBe(false);
194
+ expect(diagnostics.hasWarnings()).toBe(true);
195
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
196
+ "Processing wrangler configuration:
197
+ - The \\"route\\" field in your configuration is an empty string and will be ignored.
198
+ Please remove the \\"route\\" field from your configuration."
199
+ `);
200
+ });
201
+
202
+ it("should normalise a blank account_id value to be undefined", () => {
203
+ const expectedConfig = {
204
+ account_id: "",
205
+ };
206
+
207
+ const { config, diagnostics } = normalizeAndValidateConfig(
208
+ expectedConfig as unknown as RawConfig,
209
+ undefined,
210
+ { env: undefined }
211
+ );
212
+
213
+ expect(config.account_id).toBeUndefined();
214
+ expect(diagnostics.hasErrors()).toBe(false);
215
+ expect(diagnostics.hasWarnings()).toBe(true);
216
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
217
+ "Processing wrangler configuration:
218
+ - The \\"account_id\\" field in your configuration is an empty string and will be ignored.
219
+ Please remove the \\"account_id\\" field from your configuration."
220
+ `);
221
+ });
222
+
223
+ describe("[migrations]", () => {
224
+ it("should override `migrations` config defaults with provided values", () => {
225
+ const expectedConfig: RawConfig = {
226
+ migrations: [
227
+ {
228
+ tag: "TAG",
229
+ new_classes: ["CLASS_1", "CLASS_2"],
230
+ renamed_classes: [
231
+ {
232
+ from: "FROM_CLASS",
233
+ to: "TO_CLASS",
234
+ },
235
+ ],
236
+ deleted_classes: ["CLASS_3", "CLASS_4"],
237
+ },
238
+ ],
239
+ };
240
+
241
+ const { config, diagnostics } = normalizeAndValidateConfig(
242
+ expectedConfig,
243
+ undefined,
244
+ { env: undefined }
245
+ );
246
+
247
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
248
+ expect(diagnostics.hasErrors()).toBe(false);
249
+ expect(diagnostics.hasWarnings()).toBe(false);
250
+ });
251
+
252
+ it("should error on invalid `migrations` values", () => {
253
+ const expectedConfig = {
254
+ migrations: [
255
+ {
256
+ tag: 111,
257
+ new_classes: [222, 333],
258
+ renamed_classes: [
259
+ {
260
+ from: 444,
261
+ to: 555,
262
+ },
263
+ ],
264
+ deleted_classes: [666, 777],
265
+ },
266
+ ],
267
+ };
268
+
269
+ const { config, diagnostics } = normalizeAndValidateConfig(
270
+ expectedConfig as unknown as RawConfig,
271
+ undefined,
272
+ { env: undefined }
273
+ );
274
+
275
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
276
+ expect(diagnostics.hasWarnings()).toBe(false);
277
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
278
+ "Processing wrangler configuration:
279
+ - Expected \\"migrations[0].tag\\" to be of type string but got 111.
280
+ - Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
281
+ - Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
282
+ - Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
283
+ - Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
284
+ - Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
285
+ `);
286
+ });
287
+
288
+ it("should warn/error on unexpected fields on `migrations`", async () => {
289
+ const expectedConfig = {
290
+ migrations: [
291
+ {
292
+ tag: "TAG",
293
+ new_classes: ["CLASS_1", "CLASS_2"],
294
+ renamed_classes: [
295
+ {
296
+ from: "FROM_CLASS",
297
+ to: "TO_CLASS",
298
+ },
299
+ {
300
+ a: "something",
301
+ b: "someone",
302
+ },
303
+ ],
304
+ deleted_classes: ["CLASS_3", "CLASS_4"],
305
+ unrecognized_field: "FOO",
306
+ },
307
+ ],
308
+ };
309
+
310
+ const { config, diagnostics } = normalizeAndValidateConfig(
311
+ expectedConfig as unknown as RawConfig,
312
+ undefined,
313
+ { env: undefined }
314
+ );
315
+
316
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
317
+ expect(diagnostics.hasErrors()).toBe(true);
318
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
319
+ "Processing wrangler configuration:
320
+ - Unexpected fields found in migrations field: \\"unrecognized_field\\""
321
+ `);
322
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
323
+ "Processing wrangler configuration:
324
+ - Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":\\"FROM_CLASS\\",\\"to\\":\\"TO_CLASS\\"},{\\"a\\":\\"something\\",\\"b\\":\\"someone\\"}]."
325
+ `);
326
+ });
327
+ });
328
+
329
+ describe("[site]", () => {
330
+ it("should override `site` config defaults with provided values", () => {
331
+ const expectedConfig: RawConfig = {
332
+ site: {
333
+ bucket: "BUCKET",
334
+ "entry-point": "my-site",
335
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
336
+ include: ["INCLUDE_1", "INCLUDE_2"],
337
+ },
338
+ };
339
+
340
+ const { config, diagnostics } = normalizeAndValidateConfig(
341
+ expectedConfig,
342
+ undefined,
343
+ { env: undefined }
344
+ );
345
+
346
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
347
+ expect(diagnostics.hasErrors()).toBe(false);
348
+ expect(diagnostics.hasWarnings()).toBe(true);
349
+
350
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
351
+ .toMatchInlineSnapshot(`
352
+ "Processing wrangler configuration:
353
+ - Deprecation: \\"site.entry-point\\":
354
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
355
+ \`\`\`
356
+ main = \\"my-site/index.js\\"
357
+ \`\`\`"
358
+ `);
359
+ });
360
+
361
+ it("should error if `site` config is missing `bucket`", () => {
362
+ const expectedConfig: RawConfig = {
363
+ // @ts-expect-error we're intentionally passing an invalid configuration here
364
+ site: {
365
+ "entry-point": "workers-site",
366
+ include: ["INCLUDE_1", "INCLUDE_2"],
367
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
368
+ },
369
+ };
370
+
371
+ const { config, diagnostics } = normalizeAndValidateConfig(
372
+ expectedConfig,
373
+ undefined,
374
+ { env: undefined }
375
+ );
376
+
377
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
378
+ expect(diagnostics.hasWarnings()).toBe(true);
379
+ expect(diagnostics.hasErrors()).toBe(true);
380
+
381
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
382
+ .toMatchInlineSnapshot(`
383
+ "Processing wrangler configuration:
384
+ - Deprecation: \\"site.entry-point\\":
385
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
386
+ \`\`\`
387
+ main = \\"workers-site/index.js\\"
388
+ \`\`\`"
389
+ `);
390
+
391
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
392
+ "Processing wrangler configuration:
393
+ - \\"site.bucket\\" is a required field."
394
+ `);
395
+ });
396
+
397
+ it("should error on invalid `site` values", () => {
398
+ const expectedConfig = {
399
+ site: {
400
+ bucket: "BUCKET",
401
+ "entry-point": 111,
402
+ include: [222, 333],
403
+ exclude: [444, 555],
404
+ },
405
+ };
406
+
407
+ const { config, diagnostics } = normalizeAndValidateConfig(
408
+ expectedConfig as unknown as RawConfig,
409
+ undefined,
410
+ { env: undefined }
411
+ );
412
+
413
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
414
+ expect(diagnostics.hasWarnings()).toBe(true);
415
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
416
+ "Processing wrangler configuration:
417
+ - Expected \\"sites.include.[0]\\" to be of type string but got 222.
418
+ - Expected \\"sites.include.[1]\\" to be of type string but got 333.
419
+ - Expected \\"sites.exclude.[0]\\" to be of type string but got 444.
420
+ - Expected \\"sites.exclude.[1]\\" to be of type string but got 555.
421
+ - Expected \\"site.entry-point\\" to be of type string but got 111."
422
+ `);
423
+
424
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
425
+ .toMatchInlineSnapshot(`
426
+ "Processing wrangler configuration:
427
+ - Deprecation: \\"site.entry-point\\":
428
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
429
+ \`\`\`
430
+ main = \\"111/index.js\\"
431
+ \`\`\`"
432
+ `);
433
+ });
434
+
435
+ it("should log a deprecation warning if entry-point is defined", async () => {
436
+ const { config, diagnostics } = normalizeAndValidateConfig(
437
+ {
438
+ site: {
439
+ bucket: "some/path",
440
+ "entry-point": "some/other/script.js",
441
+ },
442
+ } as unknown as RawConfig,
443
+ undefined,
444
+ { env: undefined }
445
+ );
446
+
447
+ expect(config.site).toMatchInlineSnapshot(`
448
+ Object {
449
+ "bucket": "some/path",
450
+ "entry-point": "some/other/script.js",
451
+ "exclude": Array [],
452
+ "include": Array [],
453
+ }
454
+ `);
455
+ expect(diagnostics.hasWarnings()).toBe(true);
456
+ expect(diagnostics.hasErrors()).toBe(false);
457
+
458
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
459
+ .toMatchInlineSnapshot(`
460
+ "Processing wrangler configuration:
461
+ - Deprecation: \\"site.entry-point\\":
462
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
463
+ \`\`\`
464
+ main = \\"some/other/script.js\\"
465
+ \`\`\`"
466
+ `);
467
+ });
468
+ });
469
+
470
+ describe("[assets]", () => {
471
+ it("normalizes a string input to an object", () => {
472
+ const { config, diagnostics } = normalizeAndValidateConfig(
473
+ {
474
+ assets: "path/to/assets",
475
+ } as unknown as RawConfig,
476
+ undefined,
477
+ { env: undefined }
478
+ );
479
+
480
+ expect(config.assets).toMatchInlineSnapshot(`
481
+ Object {
482
+ "browser_TTL": undefined,
483
+ "bucket": "path/to/assets",
484
+ "exclude": Array [],
485
+ "include": Array [],
486
+ "serve_single_page_app": false,
487
+ }
488
+ `);
489
+ expect(diagnostics.hasWarnings()).toBe(true);
490
+ expect(diagnostics.hasErrors()).toBe(false);
491
+
492
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
493
+ "Processing wrangler configuration:
494
+ - \\"assets\\" fields are experimental and may change or break at any time."
495
+ `);
496
+ });
497
+
498
+ it("errors when input is not a string or object", () => {
499
+ const { config, diagnostics } = normalizeAndValidateConfig(
500
+ {
501
+ assets: 123,
502
+ } as unknown as RawConfig,
503
+ undefined,
504
+ { env: undefined }
505
+ );
506
+
507
+ expect(config.assets).toBeUndefined();
508
+ expect(diagnostics.hasWarnings()).toBe(true);
509
+ expect(diagnostics.hasErrors()).toBe(true);
510
+
511
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
512
+ "Processing wrangler configuration:
513
+ - \\"assets\\" fields are experimental and may change or break at any time."
514
+ `);
515
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
516
+ "Processing wrangler configuration:
517
+ - Expected the \`assets\` field to be a string or an object, but got number."
518
+ `);
519
+ });
520
+
521
+ it("should error if `assets` config is missing `bucket`", () => {
522
+ const expectedConfig: RawConfig = {
523
+ // @ts-expect-error we're intentionally passing an invalid configuration here
524
+ assets: {
525
+ include: ["INCLUDE_1", "INCLUDE_2"],
526
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
527
+ },
528
+ };
529
+
530
+ const { config, diagnostics } = normalizeAndValidateConfig(
531
+ expectedConfig,
532
+ undefined,
533
+ { env: undefined }
534
+ );
535
+
536
+ expect(config.assets).toEqual(
537
+ expect.objectContaining(expectedConfig.assets)
538
+ );
539
+ expect(diagnostics.hasWarnings()).toBe(true);
540
+ expect(diagnostics.hasErrors()).toBe(true);
541
+
542
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
543
+ "Processing wrangler configuration:
544
+ - \\"assets\\" fields are experimental and may change or break at any time."
545
+ `);
546
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
547
+ "Processing wrangler configuration:
548
+ - \\"assets.bucket\\" is a required field."
549
+ `);
550
+ });
551
+
552
+ it("should error on invalid `assets` values", () => {
553
+ const expectedConfig = {
554
+ assets: {
555
+ bucket: "BUCKET",
556
+ include: [222, 333],
557
+ exclude: [444, 555],
558
+ browser_TTL: "not valid",
559
+ serve_single_page_app: "INVALID",
560
+ },
561
+ };
562
+
563
+ const { config, diagnostics } = normalizeAndValidateConfig(
564
+ expectedConfig as unknown as RawConfig,
565
+ undefined,
566
+ { env: undefined }
567
+ );
568
+
569
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
570
+ expect(diagnostics.hasWarnings()).toBe(true);
571
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
572
+ "Processing wrangler configuration:
573
+ - \\"assets\\" fields are experimental and may change or break at any time."
574
+ `);
575
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
576
+ "Processing wrangler configuration:
577
+ - Expected \\"assets.include.[0]\\" to be of type string but got 222.
578
+ - Expected \\"assets.include.[1]\\" to be of type string but got 333.
579
+ - Expected \\"assets.exclude.[0]\\" to be of type string but got 444.
580
+ - Expected \\"assets.exclude.[1]\\" to be of type string but got 555.
581
+ - Expected \\"assets.browser_TTL\\" to be of type number but got \\"not valid\\".
582
+ - Expected \\"assets.serve_single_page_app\\" to be of type boolean but got \\"INVALID\\"."
583
+ `);
584
+ });
585
+ });
586
+
587
+ it("should map `wasm_module` paths from relative to the config path to relative to the cwd", () => {
588
+ const expectedConfig: RawConfig = {
589
+ wasm_modules: {
590
+ MODULE_1: "path/to/module_1.mjs",
591
+ MODULE_2: "module_2.mjs",
592
+ },
593
+ };
594
+
595
+ const { config, diagnostics } = normalizeAndValidateConfig(
596
+ expectedConfig,
597
+ "project/wrangler.toml",
598
+ { env: undefined }
599
+ );
600
+
601
+ expect(config).toEqual(
602
+ expect.objectContaining({
603
+ wasm_modules: {
604
+ MODULE_1: path.normalize("project/path/to/module_1.mjs"),
605
+ MODULE_2: path.normalize("project/module_2.mjs"),
606
+ },
607
+ })
608
+ );
609
+ expect(diagnostics.hasErrors()).toBe(false);
610
+ expect(diagnostics.hasWarnings()).toBe(false);
611
+ });
612
+
613
+ it("should warn on unexpected fields on `triggers`", async () => {
614
+ const expectedConfig: RawConfig = {
615
+ triggers: {
616
+ crons: ["1 * * * *"],
617
+ // @ts-expect-error we're purposely adding a field
618
+ // that doesn't belong here
619
+ someOtherfield: 123,
620
+ },
621
+ };
622
+
623
+ const { config, diagnostics } = normalizeAndValidateConfig(
624
+ expectedConfig,
625
+ "project/wrangler.toml",
626
+ { env: undefined }
627
+ );
628
+
629
+ expect(config).toEqual(
630
+ expect.objectContaining({
631
+ triggers: {
632
+ crons: ["1 * * * *"],
633
+ someOtherfield: 123,
634
+ },
635
+ })
636
+ );
637
+ expect(diagnostics.hasErrors()).toBe(false);
638
+ expect(diagnostics.hasWarnings()).toBe(true);
639
+
640
+ expect(normalizePath(diagnostics.renderWarnings()))
641
+ .toMatchInlineSnapshot(`
642
+ "Processing project/wrangler.toml configuration:
643
+ - Unexpected fields found in triggers field: \\"someOtherfield\\""
644
+ `);
645
+ });
646
+
647
+ it("should error on invalid `wasm_modules` paths", () => {
648
+ const expectedConfig = {
649
+ wasm_modules: {
650
+ MODULE_1: 111,
651
+ MODULE_2: 222,
652
+ },
653
+ };
654
+
655
+ const { config, diagnostics } = normalizeAndValidateConfig(
656
+ expectedConfig as unknown as RawConfig,
657
+ "project/wrangler.toml",
658
+ { env: undefined }
659
+ );
660
+
661
+ expect(config).toEqual(
662
+ expect.objectContaining({
663
+ wasm_modules: {},
664
+ })
665
+ );
666
+ expect(diagnostics.hasWarnings()).toBe(false);
667
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
668
+ "Processing project/wrangler.toml configuration:
669
+ - Expected \\"wasm_modules['MODULE_1']\\" to be of type string but got 111.
670
+ - Expected \\"wasm_modules['MODULE_2']\\" to be of type string but got 222."
671
+ `);
672
+ });
673
+
674
+ it("should map `text_blobs` paths from relative to the config path to relative to the cwd", () => {
675
+ const expectedConfig: RawConfig = {
676
+ text_blobs: {
677
+ BLOB_1: "path/to/text1.txt",
678
+ BLOB_2: "text2.md",
679
+ },
680
+ };
681
+
682
+ const { config, diagnostics } = normalizeAndValidateConfig(
683
+ expectedConfig,
684
+ "project/wrangler.toml",
685
+ { env: undefined }
686
+ );
687
+
688
+ expect(config).toEqual(
689
+ expect.objectContaining({
690
+ text_blobs: {
691
+ BLOB_1: path.normalize("project/path/to/text1.txt"),
692
+ BLOB_2: path.normalize("project/text2.md"),
693
+ },
694
+ })
695
+ );
696
+ expect(diagnostics.hasErrors()).toBe(false);
697
+ expect(diagnostics.hasWarnings()).toBe(false);
698
+ });
699
+
700
+ it("should error on invalid `text_blob` paths", () => {
701
+ const expectedConfig = {
702
+ text_blobs: {
703
+ MODULE_1: 111,
704
+ MODULE_2: 222,
705
+ },
706
+ };
707
+
708
+ const { config, diagnostics } = normalizeAndValidateConfig(
709
+ expectedConfig as unknown as RawConfig,
710
+ "project/wrangler.toml",
711
+ { env: undefined }
712
+ );
713
+
714
+ expect(config).toEqual(
715
+ expect.objectContaining({
716
+ text_blobs: {},
717
+ })
718
+ );
719
+ expect(diagnostics.hasWarnings()).toBe(false);
720
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
721
+ "Processing project/wrangler.toml configuration:
722
+ - Expected \\"text_blobs['MODULE_1']\\" to be of type string but got 111.
723
+ - Expected \\"text_blobs['MODULE_2']\\" to be of type string but got 222."
724
+ `);
725
+ });
726
+
727
+ it("should map `data_blobs` paths from relative to the config path to relative to the cwd", () => {
728
+ const expectedConfig: RawConfig = {
729
+ data_blobs: {
730
+ BLOB_1: "path/to/data1.bin",
731
+ BLOB_2: "data2.bin",
732
+ },
733
+ };
734
+
735
+ const { config, diagnostics } = normalizeAndValidateConfig(
736
+ expectedConfig,
737
+ "project/wrangler.toml",
738
+ { env: undefined }
739
+ );
740
+
741
+ expect(config).toEqual(
742
+ expect.objectContaining({
743
+ data_blobs: {
744
+ BLOB_1: path.normalize("project/path/to/data1.bin"),
745
+ BLOB_2: path.normalize("project/data2.bin"),
746
+ },
747
+ })
748
+ );
749
+ expect(diagnostics.hasErrors()).toBe(false);
750
+ expect(diagnostics.hasWarnings()).toBe(false);
751
+ });
752
+
753
+ it("should error on invalid `data_blob` paths", () => {
754
+ const expectedConfig = {
755
+ data_blobs: {
756
+ MODULE_1: 111,
757
+ MODULE_2: 222,
758
+ },
759
+ };
760
+
761
+ const { config, diagnostics } = normalizeAndValidateConfig(
762
+ expectedConfig as unknown as RawConfig,
763
+ "project/wrangler.toml",
764
+ { env: undefined }
765
+ );
766
+
767
+ expect(config).toEqual(
768
+ expect.objectContaining({
769
+ data_blobs: {},
770
+ })
771
+ );
772
+ expect(diagnostics.hasWarnings()).toBe(false);
773
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
774
+ "Processing project/wrangler.toml configuration:
775
+ - Expected \\"data_blobs['MODULE_1']\\" to be of type string but got 111.
776
+ - Expected \\"data_blobs['MODULE_2']\\" to be of type string but got 222."
777
+ `);
778
+ });
779
+
780
+ it("should resolve tsconfig relative to wrangler.toml", async () => {
781
+ const expectedConfig: RawEnvironment = {
782
+ tsconfig: "path/to/some-tsconfig.json",
783
+ };
784
+
785
+ const { config, diagnostics } = normalizeAndValidateConfig(
786
+ expectedConfig,
787
+ "project/wrangler.toml",
788
+ { env: undefined }
789
+ );
790
+
791
+ expect(config).toEqual(
792
+ expect.objectContaining({
793
+ tsconfig: path.normalize("project/path/to/some-tsconfig.json"),
794
+ })
795
+ );
796
+
797
+ expect(diagnostics.hasErrors()).toBe(false);
798
+ expect(diagnostics.hasWarnings()).toBe(false);
799
+ });
800
+
801
+ describe("(deprecated)", () => {
802
+ it("should remove and warn about deprecated properties", () => {
803
+ const rawConfig: RawConfig = {
804
+ type: "webpack",
805
+ webpack_config: "CONFIG",
806
+ };
807
+
808
+ const { config, diagnostics } = normalizeAndValidateConfig(
809
+ rawConfig,
810
+ undefined,
811
+ { env: undefined }
812
+ );
813
+
814
+ // Note the `.not.` here...
815
+ expect(config).toEqual(
816
+ expect.not.objectContaining({
817
+ type: expect.anything(),
818
+ webpack_config: expect.anything(),
819
+ })
820
+ );
821
+ expect(diagnostics.hasErrors()).toBe(false);
822
+ expect(diagnostics.hasWarnings()).toBe(true);
823
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
824
+ "Processing wrangler configuration:
825
+ - 😶 Ignored: \\"type\\":
826
+ Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build.
827
+ - 😶 Ignored: \\"webpack_config\\":
828
+ Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build."
829
+ `);
830
+ });
831
+ });
832
+ });
833
+
834
+ describe("top-level environment configuration", () => {
835
+ it("should override config defaults with provided values", () => {
836
+ const main = "src/index.ts";
837
+ const resolvedMain = path.resolve(process.cwd(), main);
838
+
839
+ const expectedConfig: RawEnvironment = {
840
+ name: "mock-name",
841
+ account_id: "ACCOUNT_ID",
842
+ compatibility_date: "2022-01-01",
843
+ compatibility_flags: ["FLAG1", "FLAG2"],
844
+ workers_dev: false,
845
+ routes: [
846
+ "ROUTE_1",
847
+ "ROUTE_2",
848
+ { pattern: "ROUTE3", zone_id: "ZONE_ID_3" },
849
+ "ROUTE_4",
850
+ ],
851
+ jsx_factory: "JSX_FACTORY",
852
+ jsx_fragment: "JSX_FRAGMENT",
853
+ tsconfig: "path/to/tsconfig",
854
+ triggers: { crons: ["CRON_1", "CRON_2"] },
855
+ usage_model: "bundled",
856
+ main,
857
+ build: {
858
+ command: "COMMAND",
859
+ cwd: "CWD",
860
+ watch_dir: "WATCH_DIR",
861
+ },
862
+ define: {
863
+ DEF1: "DEFINE_1",
864
+ DEF2: "DEFINE_2",
865
+ },
866
+ vars: {
867
+ VAR1: "VALUE_1",
868
+ VAR2: "VALUE_2",
869
+ },
870
+ durable_objects: {
871
+ bindings: [
872
+ { name: "DO_BINDING_1", class_name: "CLASS1" },
873
+ {
874
+ name: "DO_BINDING_2",
875
+ class_name: "CLASS2",
876
+ script_name: "SCRIPT2",
877
+ },
878
+ {
879
+ name: "DO_BINDING_3",
880
+ class_name: "CLASS3",
881
+ script_name: "SCRIPT3",
882
+ environment: "ENV3",
883
+ },
884
+ ],
885
+ },
886
+ kv_namespaces: [
887
+ { binding: "KV_BINDING_1", id: "KV_ID_1" },
888
+ {
889
+ binding: "KV_BINDING_2",
890
+ id: "KV_ID_2",
891
+ preview_id: "KV_PREVIEW_1",
892
+ },
893
+ ],
894
+ r2_buckets: [
895
+ { binding: "R2_BINDING_1", bucket_name: "R2_BUCKET_1" },
896
+ {
897
+ binding: "R2_BINDING_2",
898
+ bucket_name: "R2_BUCKET_2",
899
+ preview_bucket_name: "R2_PREVIEW_2",
900
+ },
901
+ ],
902
+ services: [
903
+ {
904
+ binding: "SERVICE_BINDING_1",
905
+ service: "SERVICE_TYPE_1",
906
+ environment: "SERVICE_BINDING_ENVIRONMENT_1",
907
+ },
908
+ ],
909
+ unsafe: {
910
+ bindings: [
911
+ { name: "UNSAFE_BINDING_1", type: "UNSAFE_TYPE_1" },
912
+ {
913
+ name: "UNSAFE_BINDING_2",
914
+ type: "UNSAFE_TYPE_2",
915
+ extra: "UNSAFE_EXTRA_1",
916
+ },
917
+ ],
918
+ },
919
+ no_bundle: true,
920
+ minify: true,
921
+ node_compat: true,
922
+ first_party_worker: true,
923
+ };
924
+
925
+ const { config, diagnostics } = normalizeAndValidateConfig(
926
+ expectedConfig,
927
+ undefined,
928
+ { env: undefined }
929
+ );
930
+
931
+ expect(config).toEqual(
932
+ expect.objectContaining({ ...expectedConfig, main: resolvedMain })
933
+ );
934
+ expect(diagnostics.hasErrors()).toBe(false);
935
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
936
+ "Processing wrangler configuration:
937
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
938
+ - \\"services\\" fields are experimental and may change or break at any time.
939
+ - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS1), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
940
+
941
+ \`\`\`
942
+ [[migrations]]
943
+ tag = \\"v1\\" # Should be unique for each entry
944
+ new_classes = [\\"CLASS1\\"]
945
+ \`\`\`
946
+
947
+ Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
948
+ `);
949
+ });
950
+
951
+ it("should error on invalid environment values", () => {
952
+ const expectedConfig: RawEnvironment = {
953
+ name: 111,
954
+ account_id: 222,
955
+ compatibility_date: 333,
956
+ compatibility_flags: [444, 555],
957
+ workers_dev: "BAD",
958
+ routes: [
959
+ 666,
960
+ 777,
961
+ // this one's valid, but we add it here to make sure
962
+ // it doesn't get included in the error message
963
+ "example.com/*",
964
+ { pattern: 123, zone_id: "zone_id_1" },
965
+ { pattern: "route_2", zone_id: 123 },
966
+ { pattern: "route_2", zone_name: 123 },
967
+ { pattern: "route_3" },
968
+ { zone_id: "zone_id_4" },
969
+ { zone_name: "zone_name_4" },
970
+ { pattern: undefined },
971
+ { pattern: "route_5", zone_id: "zone_id_5", some_other_key: 123 },
972
+ { pattern: "route_5", zone_name: "zone_name_5", some_other_key: 123 },
973
+ // this one's valid too
974
+ { pattern: "route_6", zone_id: "zone_id_6" },
975
+ // as well as this one
976
+ { pattern: "route_6", zone_name: "zone_name_6" },
977
+ ],
978
+ route: 888,
979
+ jsx_factory: 999,
980
+ jsx_fragment: 1000,
981
+ tsconfig: true,
982
+ triggers: { crons: [1111, 1222] },
983
+ usage_model: "INVALID",
984
+ main: 1333,
985
+ build: {
986
+ command: 1444,
987
+ cwd: 1555,
988
+ watch_dir: 1666,
989
+ },
990
+ define: {
991
+ DEF1: 1777,
992
+ },
993
+ no_bundle: "INVALID",
994
+ minify: "INVALID",
995
+ node_compat: "INVALID",
996
+ first_party_worker: "INVALID",
997
+ } as unknown as RawEnvironment;
998
+
999
+ const { config, diagnostics } = normalizeAndValidateConfig(
1000
+ expectedConfig,
1001
+ undefined,
1002
+ { env: undefined }
1003
+ );
1004
+
1005
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1006
+ expect(diagnostics.hasWarnings()).toBe(false);
1007
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1008
+ "Processing wrangler configuration:
1009
+ - Expected \\"route\\" to be either a string, or an object with shape { pattern, custom_domain, zone_id | zone_name }, but got 888.
1010
+ - Expected \\"account_id\\" to be of type string but got 222.
1011
+ - Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, custom_domain, zone_id | zone_name }, but these weren't valid: [
1012
+ 666,
1013
+ 777,
1014
+ {
1015
+ \\"pattern\\": 123,
1016
+ \\"zone_id\\": \\"zone_id_1\\"
1017
+ },
1018
+ {
1019
+ \\"pattern\\": \\"route_2\\",
1020
+ \\"zone_id\\": 123
1021
+ },
1022
+ {
1023
+ \\"pattern\\": \\"route_2\\",
1024
+ \\"zone_name\\": 123
1025
+ },
1026
+ {
1027
+ \\"pattern\\": \\"route_3\\"
1028
+ },
1029
+ {
1030
+ \\"zone_id\\": \\"zone_id_4\\"
1031
+ },
1032
+ {
1033
+ \\"zone_name\\": \\"zone_name_4\\"
1034
+ },
1035
+ {},
1036
+ {
1037
+ \\"pattern\\": \\"route_5\\",
1038
+ \\"zone_id\\": \\"zone_id_5\\",
1039
+ \\"some_other_key\\": 123
1040
+ },
1041
+ {
1042
+ \\"pattern\\": \\"route_5\\",
1043
+ \\"zone_name\\": \\"zone_name_5\\",
1044
+ \\"some_other_key\\": 123
1045
+ }
1046
+ ].
1047
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
1048
+ - Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
1049
+ - Expected \\"build.command\\" to be of type string but got 1444.
1050
+ - Expected \\"build.cwd\\" to be of type string but got 1555.
1051
+ - Expected \\"build.watch_dir\\" to be of type string but got 1666.
1052
+ - Expected \\"compatibility_date\\" to be of type string but got 333.
1053
+ - Expected \\"compatibility_flags\\" to be of type string array but got [444,555].
1054
+ - Expected \\"jsx_factory\\" to be of type string but got 999.
1055
+ - Expected \\"jsx_fragment\\" to be of type string but got 1000.
1056
+ - Expected \\"tsconfig\\" to be of type string but got true.
1057
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got 111.
1058
+ - Expected \\"main\\" to be of type string but got 1333.
1059
+ - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\".
1060
+ - The field \\"define.DEF1\\" should be a string but got 1777.
1061
+ - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\".
1062
+ - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\".
1063
+ - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\".
1064
+ - Expected \\"first_party_worker\\" to be of type boolean but got \\"INVALID\\"."
1065
+ `);
1066
+ });
1067
+
1068
+ describe("name", () => {
1069
+ it("should error on invalid `name` value with spaces", () => {
1070
+ const expectedConfig: RawEnvironment = {
1071
+ name: "NCC 1701 D",
1072
+ } as unknown as RawEnvironment;
1073
+
1074
+ const { config, diagnostics } = normalizeAndValidateConfig(
1075
+ expectedConfig,
1076
+ undefined,
1077
+ { env: undefined }
1078
+ );
1079
+
1080
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1081
+ expect(diagnostics.hasWarnings()).toBe(false);
1082
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1083
+ "Processing wrangler configuration:
1084
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"NCC 1701 D\\"."
1085
+ `);
1086
+ });
1087
+
1088
+ it("should be valid `name` with underscores", () => {
1089
+ const expectedConfig: RawEnvironment = {
1090
+ name: "enterprise_nx_01",
1091
+ } as unknown as RawEnvironment;
1092
+
1093
+ const { config, diagnostics } = normalizeAndValidateConfig(
1094
+ expectedConfig,
1095
+ undefined,
1096
+ { env: undefined }
1097
+ );
1098
+
1099
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1100
+ expect(diagnostics.hasWarnings()).toBe(false);
1101
+ expect(diagnostics.hasErrors()).toBe(false);
1102
+ });
1103
+
1104
+ it("should error on invalid `name` value with special characters", () => {
1105
+ const expectedConfig: RawEnvironment = {
1106
+ name: "Thy'lek-Shran",
1107
+ } as unknown as RawEnvironment;
1108
+
1109
+ const { config, diagnostics } = normalizeAndValidateConfig(
1110
+ expectedConfig,
1111
+ undefined,
1112
+ { env: undefined }
1113
+ );
1114
+
1115
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1116
+ expect(diagnostics.hasWarnings()).toBe(false);
1117
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1118
+ "Processing wrangler configuration:
1119
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"Thy'lek-Shran\\"."
1120
+ `);
1121
+ });
1122
+
1123
+ it("should error on invalid `name` value with only special characters", () => {
1124
+ const expectedConfig: RawEnvironment = {
1125
+ name: "!@#$%^&*(()",
1126
+ } as unknown as RawEnvironment;
1127
+
1128
+ const { config, diagnostics } = normalizeAndValidateConfig(
1129
+ expectedConfig,
1130
+ undefined,
1131
+ { env: undefined }
1132
+ );
1133
+
1134
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1135
+ expect(diagnostics.hasWarnings()).toBe(false);
1136
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1137
+ "Processing wrangler configuration:
1138
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"!@#$%^&*(()\\"."
1139
+ `);
1140
+ });
1141
+ });
1142
+
1143
+ describe("[build]", () => {
1144
+ it("should override build.upload config defaults with provided values and warn about deprecations", () => {
1145
+ const expectedConfig: RawEnvironment = {
1146
+ build: {
1147
+ upload: {
1148
+ dir: "src",
1149
+ format: "modules",
1150
+ main: "index.ts",
1151
+ rules: [{ type: "Text", globs: ["GLOB"], fallthrough: true }],
1152
+ },
1153
+ },
1154
+ };
1155
+
1156
+ const { config, diagnostics } = normalizeAndValidateConfig(
1157
+ expectedConfig,
1158
+ path.resolve("project/wrangler.toml"),
1159
+ { env: undefined }
1160
+ );
1161
+
1162
+ expect(config.main).toEqual(path.resolve("project/src/index.ts"));
1163
+ expect(config.build.upload).toBeUndefined();
1164
+ expect(diagnostics.hasErrors()).toBe(false);
1165
+ expect(diagnostics.hasWarnings()).toBe(true);
1166
+ expect(normalizePath(diagnostics.renderWarnings()))
1167
+ .toMatchInlineSnapshot(`
1168
+ "Processing project/wrangler.toml configuration:
1169
+ - Deprecation: \\"build.upload.format\\":
1170
+ The format is inferred automatically from the code.
1171
+ - Deprecation: \\"build.upload.main\\":
1172
+ Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
1173
+ Then add the top level \`main\` field to your configuration file:
1174
+ \`\`\`
1175
+ main = \\"src/index.ts\\"
1176
+ \`\`\`
1177
+ - Deprecation: \\"build.upload.dir\\":
1178
+ Use the top level \\"main\\" field or a command-line argument to specify the entry-point for the Worker.
1179
+ - Deprecation: The \`build.upload.rules\` config field is no longer used, the rules should be specified via the \`rules\` config field. Delete the \`build.upload\` field from the configuration file, and add this:
1180
+ \`\`\`
1181
+ [[rules]]
1182
+ type = \\"Text\\"
1183
+ globs = [ \\"GLOB\\" ]
1184
+ fallthrough = true
1185
+ \`\`\`"
1186
+ `);
1187
+ });
1188
+
1189
+ it("should default custom build watch directories to src", () => {
1190
+ const expectedConfig: RawEnvironment = {
1191
+ build: {
1192
+ command: "execute some --build",
1193
+ },
1194
+ };
1195
+
1196
+ const { config, diagnostics } = normalizeAndValidateConfig(
1197
+ expectedConfig,
1198
+ undefined,
1199
+ { env: undefined }
1200
+ );
1201
+
1202
+ expect(config.build).toEqual(
1203
+ expect.objectContaining({
1204
+ command: "execute some --build",
1205
+ watch_dir: "./src",
1206
+ })
1207
+ );
1208
+
1209
+ expect(diagnostics.hasErrors()).toBe(false);
1210
+ expect(diagnostics.hasWarnings()).toBe(false);
1211
+ });
1212
+
1213
+ it("should resolve custom build watch directories relative to wrangler.toml", async () => {
1214
+ const expectedConfig: RawEnvironment = {
1215
+ build: {
1216
+ command: "execute some --build",
1217
+ watch_dir: "some/path",
1218
+ },
1219
+ };
1220
+
1221
+ const { config, diagnostics } = normalizeAndValidateConfig(
1222
+ expectedConfig,
1223
+ "project/wrangler.toml",
1224
+ { env: undefined }
1225
+ );
1226
+
1227
+ expect(config.build).toEqual(
1228
+ expect.objectContaining({
1229
+ command: "execute some --build",
1230
+ watch_dir: path.normalize("project/some/path"),
1231
+ })
1232
+ );
1233
+
1234
+ expect(diagnostics.hasErrors()).toBe(false);
1235
+ expect(diagnostics.hasWarnings()).toBe(false);
1236
+ });
1237
+
1238
+ it("should allow watch_dir to be an array of paths", () => {
1239
+ const expectedConfig: RawEnvironment = {
1240
+ build: {
1241
+ command: "execute some --build",
1242
+ watch_dir: ["some/path/a", "some/path/b", "some/path/c"],
1243
+ },
1244
+ };
1245
+
1246
+ const { config, diagnostics } = normalizeAndValidateConfig(
1247
+ expectedConfig,
1248
+ "project/wrangler.toml",
1249
+ { env: undefined }
1250
+ );
1251
+
1252
+ expect(config.build).toEqual(
1253
+ expect.objectContaining({
1254
+ command: "execute some --build",
1255
+ watch_dir: [
1256
+ path.normalize("project/some/path/a"),
1257
+ path.normalize("project/some/path/b"),
1258
+ path.normalize("project/some/path/c"),
1259
+ ],
1260
+ })
1261
+ );
1262
+
1263
+ expect(diagnostics.hasErrors()).toBe(false);
1264
+ expect(diagnostics.hasWarnings()).toBe(false);
1265
+ });
1266
+
1267
+ it("should error when the watch_dir array isn't an array of strings", () => {
1268
+ const expectedConfig: RawEnvironment = {
1269
+ build: {
1270
+ command: "execute some --build",
1271
+ watch_dir: [
1272
+ "some/path/a",
1273
+ "some/path/b",
1274
+ // @ts-expect-error intentionally bad "paths"
1275
+ 123,
1276
+ "some/path/c",
1277
+ // @ts-expect-error intentionally bad "paths"
1278
+ false,
1279
+ ],
1280
+ },
1281
+ };
1282
+
1283
+ const { config, diagnostics } = normalizeAndValidateConfig(
1284
+ expectedConfig,
1285
+ "project/wrangler.toml",
1286
+ { env: undefined }
1287
+ );
1288
+
1289
+ expect(config.build).toEqual(
1290
+ expect.objectContaining({
1291
+ command: "execute some --build",
1292
+ watch_dir: [
1293
+ path.normalize("project/some/path/a"),
1294
+ path.normalize("project/some/path/b"),
1295
+ path.normalize("project/123"),
1296
+ path.normalize("project/some/path/c"),
1297
+ path.normalize("project/false"),
1298
+ ],
1299
+ })
1300
+ );
1301
+
1302
+ expect(diagnostics.hasWarnings()).toBe(false);
1303
+ expect(diagnostics.hasErrors()).toBe(true);
1304
+
1305
+ expect(normalizePath(diagnostics.renderErrors()))
1306
+ .toMatchInlineSnapshot(`
1307
+ "Processing project/wrangler.toml configuration:
1308
+ - Expected \\"build.watch_dir.[2]\\" to be of type string but got 123.
1309
+ - Expected \\"build.watch_dir.[4]\\" to be of type string but got false."
1310
+ `);
1311
+ });
1312
+ });
1313
+
1314
+ describe("[durable_objects]", () => {
1315
+ it("should error if durable_objects is an array", () => {
1316
+ const { config, diagnostics } = normalizeAndValidateConfig(
1317
+ { durable_objects: [] } as unknown as RawConfig,
1318
+ undefined,
1319
+ { env: undefined }
1320
+ );
1321
+
1322
+ expect(config).toEqual(
1323
+ expect.not.objectContaining({ durable_objects: expect.anything })
1324
+ );
1325
+ expect(diagnostics.hasWarnings()).toBe(false);
1326
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1327
+ "Processing wrangler configuration:
1328
+ - The field \\"durable_objects\\" should be an object but got []."
1329
+ `);
1330
+ });
1331
+
1332
+ it("should error if durable_objects is a string", () => {
1333
+ const { config, diagnostics } = normalizeAndValidateConfig(
1334
+ { durable_objects: "BAD" } as unknown as RawConfig,
1335
+ undefined,
1336
+ { env: undefined }
1337
+ );
1338
+
1339
+ expect(config).toEqual(
1340
+ expect.not.objectContaining({ durable_objects: expect.anything })
1341
+ );
1342
+ expect(diagnostics.hasWarnings()).toBe(false);
1343
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1344
+ "Processing wrangler configuration:
1345
+ - The field \\"durable_objects\\" should be an object but got \\"BAD\\"."
1346
+ `);
1347
+ });
1348
+
1349
+ it("should error if durable_objects is a number", () => {
1350
+ const { config, diagnostics } = normalizeAndValidateConfig(
1351
+ { durable_objects: 999 } as unknown as RawConfig,
1352
+ undefined,
1353
+ { env: undefined }
1354
+ );
1355
+
1356
+ expect(config).toEqual(
1357
+ expect.not.objectContaining({ durable_objects: expect.anything })
1358
+ );
1359
+ expect(diagnostics.hasWarnings()).toBe(false);
1360
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1361
+ "Processing wrangler configuration:
1362
+ - The field \\"durable_objects\\" should be an object but got 999."
1363
+ `);
1364
+ });
1365
+
1366
+ it("should error if durable_objects is null", () => {
1367
+ const { config, diagnostics } = normalizeAndValidateConfig(
1368
+ { durable_objects: null } as unknown as RawConfig,
1369
+ undefined,
1370
+ { env: undefined }
1371
+ );
1372
+
1373
+ expect(config).toEqual(
1374
+ expect.not.objectContaining({ durable_objects: expect.anything })
1375
+ );
1376
+ expect(diagnostics.hasWarnings()).toBe(false);
1377
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1378
+ "Processing wrangler configuration:
1379
+ - The field \\"durable_objects\\" should be an object but got null."
1380
+ `);
1381
+ });
1382
+
1383
+ it("should error if durable_objects.bindings is not defined", () => {
1384
+ const { config, diagnostics } = normalizeAndValidateConfig(
1385
+ { durable_objects: {} } as unknown as RawConfig,
1386
+ undefined,
1387
+ { env: undefined }
1388
+ );
1389
+
1390
+ expect(config).toEqual(
1391
+ expect.not.objectContaining({ durable_objects: expect.anything })
1392
+ );
1393
+ expect(diagnostics.hasWarnings()).toBe(false);
1394
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1395
+ "Processing wrangler configuration:
1396
+ - The field \\"durable_objects\\" is missing the required \\"bindings\\" property."
1397
+ `);
1398
+ });
1399
+
1400
+ it("should error if durable_objects.bindings is an object", () => {
1401
+ const { config, diagnostics } = normalizeAndValidateConfig(
1402
+ { durable_objects: { bindings: {} } } as unknown as RawConfig,
1403
+ undefined,
1404
+ { env: undefined }
1405
+ );
1406
+
1407
+ expect(config).toEqual(
1408
+ expect.not.objectContaining({
1409
+ durable_objects: { bindings: expect.anything },
1410
+ })
1411
+ );
1412
+ expect(diagnostics.hasWarnings()).toBe(false);
1413
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1414
+ "Processing wrangler configuration:
1415
+ - The field \\"durable_objects.bindings\\" should be an array but got {}."
1416
+ `);
1417
+ });
1418
+
1419
+ it("should error if durable_objects.bindings is a string", () => {
1420
+ const { config, diagnostics } = normalizeAndValidateConfig(
1421
+ { durable_objects: { bindings: "BAD" } } as unknown as RawConfig,
1422
+ undefined,
1423
+ { env: undefined }
1424
+ );
1425
+
1426
+ expect(config).toEqual(
1427
+ expect.not.objectContaining({
1428
+ durable_objects: { bindings: expect.anything },
1429
+ })
1430
+ );
1431
+ expect(diagnostics.hasWarnings()).toBe(false);
1432
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1433
+ "Processing wrangler configuration:
1434
+ - The field \\"durable_objects.bindings\\" should be an array but got \\"BAD\\"."
1435
+ `);
1436
+ });
1437
+
1438
+ it("should error if durable_objects.bindings is a number", () => {
1439
+ const { config, diagnostics } = normalizeAndValidateConfig(
1440
+ { durable_objects: { bindings: 999 } } as unknown as RawConfig,
1441
+ undefined,
1442
+ { env: undefined }
1443
+ );
1444
+
1445
+ expect(config).toEqual(
1446
+ expect.not.objectContaining({
1447
+ durable_objects: { bindings: expect.anything },
1448
+ })
1449
+ );
1450
+ expect(diagnostics.hasWarnings()).toBe(false);
1451
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1452
+ "Processing wrangler configuration:
1453
+ - The field \\"durable_objects.bindings\\" should be an array but got 999."
1454
+ `);
1455
+ });
1456
+
1457
+ it("should error if durable_objects.bindings is null", () => {
1458
+ const { config, diagnostics } = normalizeAndValidateConfig(
1459
+ { durable_objects: { bindings: null } } as unknown as RawConfig,
1460
+ undefined,
1461
+ { env: undefined }
1462
+ );
1463
+
1464
+ expect(config).toEqual(
1465
+ expect.not.objectContaining({
1466
+ durable_objects: { bindings: expect.anything },
1467
+ })
1468
+ );
1469
+ expect(diagnostics.hasWarnings()).toBe(false);
1470
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1471
+ "Processing wrangler configuration:
1472
+ - The field \\"durable_objects.bindings\\" should be an array but got null."
1473
+ `);
1474
+ });
1475
+
1476
+ it("should error if durable_objects.bindings are not valid", () => {
1477
+ const { config, diagnostics } = normalizeAndValidateConfig(
1478
+ {
1479
+ durable_objects: {
1480
+ bindings: [
1481
+ {},
1482
+ { name: "MISSING_CLASS" },
1483
+ { name: 1555, class_name: 1666 },
1484
+ {
1485
+ name: 1777,
1486
+ class_name: 1888,
1487
+ script_name: 1999,
1488
+ },
1489
+ {
1490
+ name: "SOMENAME",
1491
+ class_name: "SomeClass",
1492
+ environment: "staging",
1493
+ },
1494
+ {
1495
+ name: 1778,
1496
+ class_name: 1889,
1497
+ script_name: 1992,
1498
+ environment: 2111,
1499
+ },
1500
+ {
1501
+ name: 1772,
1502
+ class_name: 1883,
1503
+ environment: 2112,
1504
+ },
1505
+ ],
1506
+ },
1507
+ } as unknown as RawConfig,
1508
+ undefined,
1509
+ { env: undefined }
1510
+ );
1511
+
1512
+ expect(config).toEqual(
1513
+ expect.not.objectContaining({
1514
+ durable_objects: { bindings: expect.anything },
1515
+ })
1516
+ );
1517
+ expect(diagnostics.hasWarnings()).toBe(false);
1518
+
1519
+ expect(diagnostics.hasErrors()).toBe(true);
1520
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1521
+ "Processing wrangler configuration:
1522
+
1523
+ - \\"durable_objects.bindings[0]\\": {}
1524
+ - binding should have a string \\"name\\" field.
1525
+ - binding should have a string \\"class_name\\" field.
1526
+
1527
+ - \\"durable_objects.bindings[1]\\": {\\"name\\":\\"MISSING_CLASS\\"}
1528
+ - binding should have a string \\"class_name\\" field.
1529
+
1530
+ - \\"durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
1531
+ - binding should have a string \\"name\\" field.
1532
+ - binding should have a string \\"class_name\\" field.
1533
+
1534
+ - \\"durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
1535
+ - binding should have a string \\"name\\" field.
1536
+ - binding should have a string \\"class_name\\" field.
1537
+ - the field \\"script_name\\", when present, should be a string.
1538
+
1539
+ - \\"durable_objects.bindings[4]\\": {\\"name\\":\\"SOMENAME\\",\\"class_name\\":\\"SomeClass\\",\\"environment\\":\\"staging\\"}
1540
+ - binding should have a \\"script_name\\" field if \\"environment\\" is present.
1541
+
1542
+ - \\"durable_objects.bindings[5]\\": {\\"name\\":1778,\\"class_name\\":1889,\\"script_name\\":1992,\\"environment\\":2111}
1543
+ - binding should have a string \\"name\\" field.
1544
+ - binding should have a string \\"class_name\\" field.
1545
+ - the field \\"script_name\\", when present, should be a string.
1546
+ - the field \\"environment\\", when present, should be a string.
1547
+
1548
+ - \\"durable_objects.bindings[6]\\": {\\"name\\":1772,\\"class_name\\":1883,\\"environment\\":2112}
1549
+ - binding should have a string \\"name\\" field.
1550
+ - binding should have a string \\"class_name\\" field.
1551
+ - the field \\"environment\\", when present, should be a string.
1552
+ - binding should have a \\"script_name\\" field if \\"environment\\" is present."
1553
+ `);
1554
+ });
1555
+ });
1556
+
1557
+ describe("[kv_namespaces]", () => {
1558
+ it("should error if kv_namespaces is an object", () => {
1559
+ const { config, diagnostics } = normalizeAndValidateConfig(
1560
+ { kv_namespaces: {} } as unknown as RawConfig,
1561
+ undefined,
1562
+ { env: undefined }
1563
+ );
1564
+
1565
+ expect(config).toEqual(
1566
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1567
+ );
1568
+ expect(diagnostics.hasWarnings()).toBe(false);
1569
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1570
+ "Processing wrangler configuration:
1571
+ - The field \\"kv_namespaces\\" should be an array but got {}."
1572
+ `);
1573
+ });
1574
+
1575
+ it("should error if kv_namespaces is a string", () => {
1576
+ const { config, diagnostics } = normalizeAndValidateConfig(
1577
+ { kv_namespaces: "BAD" } as unknown as RawConfig,
1578
+ undefined,
1579
+ { env: undefined }
1580
+ );
1581
+
1582
+ expect(config).toEqual(
1583
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1584
+ );
1585
+ expect(diagnostics.hasWarnings()).toBe(false);
1586
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1587
+ "Processing wrangler configuration:
1588
+ - The field \\"kv_namespaces\\" should be an array but got \\"BAD\\"."
1589
+ `);
1590
+ });
1591
+
1592
+ it("should error if kv_namespaces is a number", () => {
1593
+ const { config, diagnostics } = normalizeAndValidateConfig(
1594
+ { kv_namespaces: 999 } as unknown as RawConfig,
1595
+ undefined,
1596
+ { env: undefined }
1597
+ );
1598
+
1599
+ expect(config).toEqual(
1600
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1601
+ );
1602
+ expect(diagnostics.hasWarnings()).toBe(false);
1603
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1604
+ "Processing wrangler configuration:
1605
+ - The field \\"kv_namespaces\\" should be an array but got 999."
1606
+ `);
1607
+ });
1608
+
1609
+ it("should error if kv_namespaces is null", () => {
1610
+ const { config, diagnostics } = normalizeAndValidateConfig(
1611
+ { kv_namespaces: null } as unknown as RawConfig,
1612
+ undefined,
1613
+ { env: undefined }
1614
+ );
1615
+
1616
+ expect(config).toEqual(
1617
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1618
+ );
1619
+ expect(diagnostics.hasWarnings()).toBe(false);
1620
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1621
+ "Processing wrangler configuration:
1622
+ - The field \\"kv_namespaces\\" should be an array but got null."
1623
+ `);
1624
+ });
1625
+
1626
+ it("should error if kv_namespaces.bindings are not valid", () => {
1627
+ const { config, diagnostics } = normalizeAndValidateConfig(
1628
+ {
1629
+ kv_namespaces: [
1630
+ {},
1631
+ { binding: "VALID" },
1632
+ { binding: 2000, id: 2111 },
1633
+ {
1634
+ binding: "KV_BINDING_2",
1635
+ id: "KV_ID_2",
1636
+ preview_id: 2222,
1637
+ },
1638
+ ],
1639
+ } as unknown as RawConfig,
1640
+ undefined,
1641
+ { env: undefined }
1642
+ );
1643
+
1644
+ expect(config).toEqual(
1645
+ expect.not.objectContaining({
1646
+ kv_namespaces: { bindings: expect.anything },
1647
+ })
1648
+ );
1649
+ expect(diagnostics.hasWarnings()).toBe(false);
1650
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1651
+ "Processing wrangler configuration:
1652
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
1653
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
1654
+ - \\"kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
1655
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1656
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1657
+ - \\"kv_namespaces[3]\\" bindings should, optionally, have a string \\"preview_id\\" field but got {\\"binding\\":\\"KV_BINDING_2\\",\\"id\\":\\"KV_ID_2\\",\\"preview_id\\":2222}."
1658
+ `);
1659
+ });
1660
+ });
1661
+
1662
+ describe("[r2_buckets]", () => {
1663
+ it("should error if r2_buckets is an object", () => {
1664
+ const { config, diagnostics } = normalizeAndValidateConfig(
1665
+ { r2_buckets: {} } as unknown as RawConfig,
1666
+ undefined,
1667
+ { env: undefined }
1668
+ );
1669
+
1670
+ expect(config).toEqual(
1671
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1672
+ );
1673
+ expect(diagnostics.hasWarnings()).toBe(false);
1674
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1675
+ "Processing wrangler configuration:
1676
+ - The field \\"r2_buckets\\" should be an array but got {}."
1677
+ `);
1678
+ });
1679
+
1680
+ it("should error if r2_buckets is a string", () => {
1681
+ const { config, diagnostics } = normalizeAndValidateConfig(
1682
+ { r2_buckets: "BAD" } as unknown as RawConfig,
1683
+ undefined,
1684
+ { env: undefined }
1685
+ );
1686
+
1687
+ expect(config).toEqual(
1688
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1689
+ );
1690
+ expect(diagnostics.hasWarnings()).toBe(false);
1691
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1692
+ "Processing wrangler configuration:
1693
+ - The field \\"r2_buckets\\" should be an array but got \\"BAD\\"."
1694
+ `);
1695
+ });
1696
+
1697
+ it("should error if r2_buckets is a number", () => {
1698
+ const { config, diagnostics } = normalizeAndValidateConfig(
1699
+ { r2_buckets: 999 } as unknown as RawConfig,
1700
+ undefined,
1701
+ { env: undefined }
1702
+ );
1703
+
1704
+ expect(config).toEqual(
1705
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1706
+ );
1707
+ expect(diagnostics.hasWarnings()).toBe(false);
1708
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1709
+ "Processing wrangler configuration:
1710
+ - The field \\"r2_buckets\\" should be an array but got 999."
1711
+ `);
1712
+ });
1713
+
1714
+ it("should error if r2_buckets is null", () => {
1715
+ const { config, diagnostics } = normalizeAndValidateConfig(
1716
+ { r2_buckets: null } as unknown as RawConfig,
1717
+ undefined,
1718
+ { env: undefined }
1719
+ );
1720
+
1721
+ expect(config).toEqual(
1722
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1723
+ );
1724
+ expect(diagnostics.hasWarnings()).toBe(false);
1725
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1726
+ "Processing wrangler configuration:
1727
+ - The field \\"r2_buckets\\" should be an array but got null."
1728
+ `);
1729
+ });
1730
+
1731
+ it("should error if r2_buckets.bindings are not valid", () => {
1732
+ const { config, diagnostics } = normalizeAndValidateConfig(
1733
+ {
1734
+ r2_buckets: [
1735
+ {},
1736
+ { binding: "R2_BINDING_1" },
1737
+ { binding: 2333, bucket_name: 2444 },
1738
+ {
1739
+ binding: "R2_BINDING_2",
1740
+ bucket_name: "R2_BUCKET_2",
1741
+ preview_bucket_name: 2555,
1742
+ },
1743
+ ],
1744
+ } as unknown as RawConfig,
1745
+ undefined,
1746
+ { env: undefined }
1747
+ );
1748
+
1749
+ expect(config).toEqual(
1750
+ expect.not.objectContaining({
1751
+ r2_buckets: { bindings: expect.anything },
1752
+ })
1753
+ );
1754
+ expect(diagnostics.hasWarnings()).toBe(false);
1755
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1756
+ "Processing wrangler configuration:
1757
+ - \\"r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
1758
+ - \\"r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
1759
+ - \\"r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
1760
+ - \\"r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1761
+ - \\"r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1762
+ - \\"r2_buckets[3]\\" bindings should, optionally, have a string \\"preview_bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_2\\",\\"bucket_name\\":\\"R2_BUCKET_2\\",\\"preview_bucket_name\\":2555}."
1763
+ `);
1764
+ });
1765
+ });
1766
+
1767
+ describe("[services]", () => {
1768
+ it("should error if services is an object", () => {
1769
+ const { config, diagnostics } = normalizeAndValidateConfig(
1770
+ { services: {} } as unknown as RawConfig,
1771
+ undefined,
1772
+ { env: undefined }
1773
+ );
1774
+
1775
+ expect(config).toEqual(
1776
+ expect.not.objectContaining({ services: expect.anything })
1777
+ );
1778
+ expect(diagnostics.hasWarnings()).toBe(true);
1779
+ expect(diagnostics.hasErrors()).toBe(true);
1780
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1781
+ "Processing wrangler configuration:
1782
+ - \\"services\\" fields are experimental and may change or break at any time."
1783
+ `);
1784
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1785
+ "Processing wrangler configuration:
1786
+ - The field \\"services\\" should be an array but got {}."
1787
+ `);
1788
+ });
1789
+
1790
+ it("should error if services is a string", () => {
1791
+ const { config, diagnostics } = normalizeAndValidateConfig(
1792
+ { services: "BAD" } as unknown as RawConfig,
1793
+ undefined,
1794
+ { env: undefined }
1795
+ );
1796
+
1797
+ expect(config).toEqual(
1798
+ expect.not.objectContaining({ services: expect.anything })
1799
+ );
1800
+ expect(diagnostics.hasWarnings()).toBe(true);
1801
+ expect(diagnostics.hasErrors()).toBe(true);
1802
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1803
+ "Processing wrangler configuration:
1804
+ - \\"services\\" fields are experimental and may change or break at any time."
1805
+ `);
1806
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1807
+ "Processing wrangler configuration:
1808
+ - The field \\"services\\" should be an array but got \\"BAD\\"."
1809
+ `);
1810
+ });
1811
+
1812
+ it("should error if services is a number", () => {
1813
+ const { config, diagnostics } = normalizeAndValidateConfig(
1814
+ { services: 999 } as unknown as RawConfig,
1815
+ undefined,
1816
+ { env: undefined }
1817
+ );
1818
+
1819
+ expect(config).toEqual(
1820
+ expect.not.objectContaining({ services: expect.anything })
1821
+ );
1822
+ expect(diagnostics.hasWarnings()).toBe(true);
1823
+ expect(diagnostics.hasErrors()).toBe(true);
1824
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1825
+ "Processing wrangler configuration:
1826
+ - \\"services\\" fields are experimental and may change or break at any time."
1827
+ `);
1828
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1829
+ "Processing wrangler configuration:
1830
+ - The field \\"services\\" should be an array but got 999."
1831
+ `);
1832
+ });
1833
+
1834
+ it("should error if services is null", () => {
1835
+ const { config, diagnostics } = normalizeAndValidateConfig(
1836
+ { services: null } as unknown as RawConfig,
1837
+ undefined,
1838
+ { env: undefined }
1839
+ );
1840
+
1841
+ expect(config).toEqual(
1842
+ expect.not.objectContaining({ services: expect.anything })
1843
+ );
1844
+ expect(diagnostics.hasWarnings()).toBe(true);
1845
+ expect(diagnostics.hasErrors()).toBe(true);
1846
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1847
+ "Processing wrangler configuration:
1848
+ - \\"services\\" fields are experimental and may change or break at any time."
1849
+ `);
1850
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1851
+ "Processing wrangler configuration:
1852
+ - The field \\"services\\" should be an array but got null."
1853
+ `);
1854
+ });
1855
+
1856
+ it("should error if services bindings are not valid", () => {
1857
+ const { config, diagnostics } = normalizeAndValidateConfig(
1858
+ {
1859
+ services: [
1860
+ {},
1861
+ { binding: "SERVICE_BINDING_1" },
1862
+ { binding: 123, service: 456 },
1863
+ { binding: 123, service: 456, environment: 789 },
1864
+ { binding: "SERVICE_BINDING_1", service: 456, environment: 789 },
1865
+ {
1866
+ binding: 123,
1867
+ service: "SERVICE_BINDING_SERVICE_1",
1868
+ environment: 789,
1869
+ },
1870
+ {
1871
+ binding: 123,
1872
+ service: 456,
1873
+ environment: "SERVICE_BINDING_ENVIRONMENT_1",
1874
+ },
1875
+ ],
1876
+ } as unknown as RawConfig,
1877
+ undefined,
1878
+ { env: undefined }
1879
+ );
1880
+
1881
+ expect(config).toEqual(
1882
+ expect.not.objectContaining({
1883
+ services: { bindings: expect.anything },
1884
+ })
1885
+ );
1886
+ expect(diagnostics.hasWarnings()).toBe(true);
1887
+ expect(diagnostics.hasErrors()).toBe(true);
1888
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1889
+ "Processing wrangler configuration:
1890
+ - \\"services\\" fields are experimental and may change or break at any time."
1891
+ `);
1892
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1893
+ "Processing wrangler configuration:
1894
+ - \\"services[0]\\" bindings should have a string \\"binding\\" field but got {}.
1895
+ - \\"services[0]\\" bindings should have a string \\"service\\" field but got {}.
1896
+ - \\"services[1]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\"}.
1897
+ - \\"services[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456}.
1898
+ - \\"services[2]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456}.
1899
+ - \\"services[3]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
1900
+ - \\"services[3]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
1901
+ - \\"services[3]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
1902
+ - \\"services[4]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\",\\"service\\":456,\\"environment\\":789}.
1903
+ - \\"services[4]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\",\\"service\\":456,\\"environment\\":789}.
1904
+ - \\"services[5]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":\\"SERVICE_BINDING_SERVICE_1\\",\\"environment\\":789}.
1905
+ - \\"services[5]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":123,\\"service\\":\\"SERVICE_BINDING_SERVICE_1\\",\\"environment\\":789}.
1906
+ - \\"services[6]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":\\"SERVICE_BINDING_ENVIRONMENT_1\\"}.
1907
+ - \\"services[6]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":\\"SERVICE_BINDING_ENVIRONMENT_1\\"}."
1908
+ `);
1909
+ });
1910
+ });
1911
+
1912
+ describe("[worker_namespaces]", () => {
1913
+ it("should log an experimental warning when worker_namespaces is used", () => {
1914
+ const { config, diagnostics } = normalizeAndValidateConfig(
1915
+ {
1916
+ worker_namespaces: [
1917
+ {
1918
+ binding: "BINDING_1",
1919
+ namespace: "NAMESPACE_1",
1920
+ },
1921
+ ],
1922
+ } as unknown as RawConfig,
1923
+ undefined,
1924
+ { env: undefined }
1925
+ );
1926
+ expect(config).toEqual(
1927
+ expect.not.objectContaining({ worker_namespaces: expect.anything })
1928
+ );
1929
+ expect(diagnostics.hasWarnings()).toBe(true);
1930
+ expect(diagnostics.hasErrors()).toBe(false);
1931
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1932
+ "Processing wrangler configuration:
1933
+ - \\"worker_namespaces\\" fields are experimental and may change or break at any time."
1934
+ `);
1935
+ });
1936
+
1937
+ it("should error if worker_namespaces is not an array", () => {
1938
+ const { config, diagnostics } = normalizeAndValidateConfig(
1939
+ {
1940
+ worker_namespaces: "just a string",
1941
+ } as unknown as RawConfig,
1942
+ undefined,
1943
+ { env: undefined }
1944
+ );
1945
+
1946
+ expect(config).toEqual(
1947
+ expect.not.objectContaining({ worker_namespaces: expect.anything })
1948
+ );
1949
+ expect(diagnostics.hasWarnings()).toBe(true);
1950
+ expect(diagnostics.hasErrors()).toBe(true);
1951
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1952
+ "Processing wrangler configuration:
1953
+ - \\"worker_namespaces\\" fields are experimental and may change or break at any time."
1954
+ `);
1955
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1956
+ "Processing wrangler configuration:
1957
+ - The field \\"worker_namespaces\\" should be an array but got \\"just a string\\"."
1958
+ `);
1959
+ });
1960
+
1961
+ it("should error on non valid worker_namespaces", () => {
1962
+ const { config, diagnostics } = normalizeAndValidateConfig(
1963
+ {
1964
+ worker_namespaces: [
1965
+ "a string",
1966
+ 123,
1967
+ {
1968
+ binding: 123,
1969
+ namespace: 456,
1970
+ },
1971
+ {
1972
+ binding: "WORKER_NAMESPACE_BINDING_1",
1973
+ namespace: 456,
1974
+ },
1975
+ // this one is valid
1976
+ {
1977
+ binding: "WORKER_NAMESPACE_BINDING_1",
1978
+ namespace: "WORKER_NAMESPACE_BINDING_NAMESPACE_1",
1979
+ },
1980
+ {
1981
+ binding: 123,
1982
+ namespace: "WORKER_NAMESPACE_BINDING_SERVICE_1",
1983
+ },
1984
+ {
1985
+ binding: 123,
1986
+ service: 456,
1987
+ },
1988
+ ],
1989
+ } as unknown as RawConfig,
1990
+ undefined,
1991
+ { env: undefined }
1992
+ );
1993
+ expect(config).toEqual(
1994
+ expect.not.objectContaining({
1995
+ worker_namespaces: expect.anything,
1996
+ })
1997
+ );
1998
+ expect(diagnostics.hasWarnings()).toBe(true);
1999
+ expect(diagnostics.hasErrors()).toBe(true);
2000
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2001
+ "Processing wrangler configuration:
2002
+ - \\"worker_namespaces\\" fields are experimental and may change or break at any time."
2003
+ `);
2004
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2005
+ "Processing wrangler configuration:
2006
+ - \\"worker_namespaces[0]\\" binding should be objects, but got \\"a string\\"
2007
+ - \\"worker_namespaces[1]\\" binding should be objects, but got 123
2008
+ - \\"worker_namespaces[2]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"namespace\\":456}.
2009
+ - \\"worker_namespaces[2]\\" should have a string \\"namespace\\" field but got {\\"binding\\":123,\\"namespace\\":456}.
2010
+ - \\"worker_namespaces[3]\\" should have a string \\"namespace\\" field but got {\\"binding\\":\\"WORKER_NAMESPACE_BINDING_1\\",\\"namespace\\":456}.
2011
+ - \\"worker_namespaces[5]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"namespace\\":\\"WORKER_NAMESPACE_BINDING_SERVICE_1\\"}.
2012
+ - \\"worker_namespaces[6]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456}.
2013
+ - \\"worker_namespaces[6]\\" should have a string \\"namespace\\" field but got {\\"binding\\":123,\\"service\\":456}."
2014
+ `);
2015
+ });
2016
+ });
2017
+
2018
+ describe("[unsafe.bindings]", () => {
2019
+ it("should error if unsafe is an array", () => {
2020
+ const { config, diagnostics } = normalizeAndValidateConfig(
2021
+ { unsafe: [] } as unknown as RawConfig,
2022
+ undefined,
2023
+ { env: undefined }
2024
+ );
2025
+
2026
+ expect(config).toEqual(
2027
+ expect.not.objectContaining({ unsafe: expect.anything })
2028
+ );
2029
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2030
+ "Processing wrangler configuration:
2031
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2032
+ `);
2033
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2034
+ "Processing wrangler configuration:
2035
+ - The field \\"unsafe\\" should be an object but got []."
2036
+ `);
2037
+ });
2038
+
2039
+ it("should error if unsafe is a string", () => {
2040
+ const { config, diagnostics } = normalizeAndValidateConfig(
2041
+ { unsafe: "BAD" } as unknown as RawConfig,
2042
+ undefined,
2043
+ { env: undefined }
2044
+ );
2045
+
2046
+ expect(config).toEqual(
2047
+ expect.not.objectContaining({ unsafe: expect.anything })
2048
+ );
2049
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2050
+ "Processing wrangler configuration:
2051
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2052
+ `);
2053
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2054
+ "Processing wrangler configuration:
2055
+ - The field \\"unsafe\\" should be an object but got \\"BAD\\"."
2056
+ `);
2057
+ });
2058
+
2059
+ it("should error if unsafe is a number", () => {
2060
+ const { config, diagnostics } = normalizeAndValidateConfig(
2061
+ { unsafe: 999 } as unknown as RawConfig,
2062
+ undefined,
2063
+ { env: undefined }
2064
+ );
2065
+
2066
+ expect(config).toEqual(
2067
+ expect.not.objectContaining({ unsafe: expect.anything })
2068
+ );
2069
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2070
+ "Processing wrangler configuration:
2071
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2072
+ `);
2073
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2074
+ "Processing wrangler configuration:
2075
+ - The field \\"unsafe\\" should be an object but got 999."
2076
+ `);
2077
+ });
2078
+
2079
+ it("should error if unsafe is null", () => {
2080
+ const { config, diagnostics } = normalizeAndValidateConfig(
2081
+ { unsafe: null } as unknown as RawConfig,
2082
+ undefined,
2083
+ { env: undefined }
2084
+ );
2085
+
2086
+ expect(config).toEqual(
2087
+ expect.not.objectContaining({ unsafe: expect.anything })
2088
+ );
2089
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2090
+ "Processing wrangler configuration:
2091
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2092
+ `);
2093
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2094
+ "Processing wrangler configuration:
2095
+ - The field \\"unsafe\\" should be an object but got null."
2096
+ `);
2097
+ });
2098
+
2099
+ it("should error if unsafe.bindings is not defined", () => {
2100
+ const { config, diagnostics } = normalizeAndValidateConfig(
2101
+ { unsafe: {} } as unknown as RawConfig,
2102
+ undefined,
2103
+ { env: undefined }
2104
+ );
2105
+
2106
+ expect(config).toEqual(
2107
+ expect.not.objectContaining({ unsafe: expect.anything })
2108
+ );
2109
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2110
+ "Processing wrangler configuration:
2111
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2112
+ `);
2113
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2114
+ "Processing wrangler configuration:
2115
+ - The field \\"unsafe\\" is missing the required \\"bindings\\" property."
2116
+ `);
2117
+ });
2118
+
2119
+ it("should error if unsafe.bindings is an object", () => {
2120
+ const { config, diagnostics } = normalizeAndValidateConfig(
2121
+ { unsafe: { bindings: {} } } as unknown as RawConfig,
2122
+ undefined,
2123
+ { env: undefined }
2124
+ );
2125
+
2126
+ expect(config).toEqual(
2127
+ expect.not.objectContaining({
2128
+ unsafe: { bindings: expect.anything },
2129
+ })
2130
+ );
2131
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2132
+ "Processing wrangler configuration:
2133
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2134
+ `);
2135
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2136
+ "Processing wrangler configuration:
2137
+ - The field \\"unsafe.bindings\\" should be an array but got {}."
2138
+ `);
2139
+ });
2140
+
2141
+ it("should error if unsafe.bindings is a string", () => {
2142
+ const { config, diagnostics } = normalizeAndValidateConfig(
2143
+ { unsafe: { bindings: "BAD" } } as unknown as RawConfig,
2144
+ undefined,
2145
+ { env: undefined }
2146
+ );
2147
+
2148
+ expect(config).toEqual(
2149
+ expect.not.objectContaining({
2150
+ unsafe: { bindings: expect.anything },
2151
+ })
2152
+ );
2153
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2154
+ "Processing wrangler configuration:
2155
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2156
+ `);
2157
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2158
+ "Processing wrangler configuration:
2159
+ - The field \\"unsafe.bindings\\" should be an array but got \\"BAD\\"."
2160
+ `);
2161
+ });
2162
+
2163
+ it("should error if unsafe.bindings is a number", () => {
2164
+ const { config, diagnostics } = normalizeAndValidateConfig(
2165
+ { unsafe: { bindings: 999 } } as unknown as RawConfig,
2166
+ undefined,
2167
+ { env: undefined }
2168
+ );
2169
+
2170
+ expect(config).toEqual(
2171
+ expect.not.objectContaining({
2172
+ unsafe: { bindings: expect.anything },
2173
+ })
2174
+ );
2175
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2176
+ "Processing wrangler configuration:
2177
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2178
+ `);
2179
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2180
+ "Processing wrangler configuration:
2181
+ - The field \\"unsafe.bindings\\" should be an array but got 999."
2182
+ `);
2183
+ });
2184
+
2185
+ it("should error if unsafe.bindings is null", () => {
2186
+ const { config, diagnostics } = normalizeAndValidateConfig(
2187
+ { unsafe: { bindings: null } } as unknown as RawConfig,
2188
+ undefined,
2189
+ { env: undefined }
2190
+ );
2191
+
2192
+ expect(config).toEqual(
2193
+ expect.not.objectContaining({
2194
+ unsafe: { bindings: expect.anything },
2195
+ })
2196
+ );
2197
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2198
+ "Processing wrangler configuration:
2199
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2200
+ `);
2201
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2202
+ "Processing wrangler configuration:
2203
+ - The field \\"unsafe.bindings\\" should be an array but got null."
2204
+ `);
2205
+ });
2206
+
2207
+ it("should error if durable_objects.bindings are not valid", () => {
2208
+ const { config, diagnostics } = normalizeAndValidateConfig(
2209
+ {
2210
+ unsafe: {
2211
+ bindings: [
2212
+ {},
2213
+ { name: "UNSAFE_BINDING_1" },
2214
+ { name: 2666, type: 2777 },
2215
+ {
2216
+ name: "UNSAFE_BINDING_2",
2217
+ type: "UNSAFE_TYPE_2",
2218
+ extra: 2888,
2219
+ },
2220
+ ],
2221
+ },
2222
+ } as unknown as RawConfig,
2223
+ undefined,
2224
+ { env: undefined }
2225
+ );
2226
+
2227
+ expect(config).toEqual(
2228
+ expect.not.objectContaining({
2229
+ durable_objects: { bindings: expect.anything },
2230
+ })
2231
+ );
2232
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2233
+ "Processing wrangler configuration:
2234
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2235
+ `);
2236
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2237
+ "Processing wrangler configuration:
2238
+
2239
+ - \\"unsafe.bindings[0]\\": {}
2240
+ - binding should have a string \\"name\\" field.
2241
+ - binding should have a string \\"type\\" field.
2242
+
2243
+ - \\"unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
2244
+ - binding should have a string \\"type\\" field.
2245
+
2246
+ - \\"unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
2247
+ - binding should have a string \\"name\\" field.
2248
+ - binding should have a string \\"type\\" field."
2249
+ `);
2250
+ });
2251
+ });
2252
+
2253
+ describe("(deprecated)", () => {
2254
+ it("should remove and warn about deprecated properties", () => {
2255
+ const rawConfig: RawConfig = {
2256
+ zone_id: "ZONE_ID",
2257
+ experimental_services: [
2258
+ {
2259
+ name: "mock-name",
2260
+ service: "SERVICE",
2261
+ environment: "ENV",
2262
+ },
2263
+ ],
2264
+ };
2265
+
2266
+ const { config, diagnostics } = normalizeAndValidateConfig(
2267
+ rawConfig,
2268
+ undefined,
2269
+ { env: undefined }
2270
+ );
2271
+
2272
+ expect("experimental_services" in config).toBe(false);
2273
+ // Zone is not removed yet, since `route` commands might use it
2274
+ expect(config.zone_id).toEqual("ZONE_ID");
2275
+ expect(diagnostics.hasErrors()).toBe(false);
2276
+ expect(diagnostics.hasWarnings()).toBe(true);
2277
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2278
+ "Processing wrangler configuration:
2279
+ - Deprecation: \\"zone_id\\":
2280
+ This is unnecessary since we can deduce this from routes directly.
2281
+ - Deprecation: \\"experimental_services\\":
2282
+ The \\"experimental_services\\" field is no longer supported. Simply rename the [experimental_services] field to [services]."
2283
+ `);
2284
+ });
2285
+ });
2286
+
2287
+ describe("route & routes fields", () => {
2288
+ it("should error if both route and routes are specified", () => {
2289
+ const rawConfig: RawConfig = {
2290
+ route: "route1",
2291
+ routes: ["route2", "route3"],
2292
+ };
2293
+
2294
+ const { diagnostics } = normalizeAndValidateConfig(
2295
+ rawConfig,
2296
+ undefined,
2297
+ { env: undefined }
2298
+ );
2299
+
2300
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2301
+ "Processing wrangler configuration:
2302
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
2303
+ `);
2304
+ });
2305
+ });
2306
+ });
2307
+
2308
+ describe("named environments", () => {
2309
+ it("should warn if we specify an environment but there are no named environments", () => {
2310
+ const rawConfig: RawConfig = {};
2311
+ const { diagnostics } = normalizeAndValidateConfig(rawConfig, undefined, {
2312
+ env: "DEV",
2313
+ });
2314
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2315
+ "Processing wrangler configuration:
2316
+ "
2317
+ `);
2318
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2319
+ "Processing wrangler configuration:
2320
+ - No environment found in configuration with name \\"DEV\\".
2321
+ Before using \`--env=DEV\` there should be an equivalent environment section in the configuration.
2322
+
2323
+ Consider adding an environment configuration section to the wrangler.toml file:
2324
+ \`\`\`
2325
+ [env.DEV]
2326
+ \`\`\`
2327
+ "
2328
+ `);
2329
+ });
2330
+
2331
+ it("should error if we specify an environment that does not match the named environments", () => {
2332
+ const rawConfig: RawConfig = { env: { ENV1: {} } };
2333
+ const { diagnostics } = normalizeAndValidateConfig(rawConfig, undefined, {
2334
+ env: "DEV",
2335
+ });
2336
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2337
+ "Processing wrangler configuration:
2338
+ - No environment found in configuration with name \\"DEV\\".
2339
+ Before using \`--env=DEV\` there should be an equivalent environment section in the configuration.
2340
+ The available configured environment names are: [\\"ENV1\\"]
2341
+
2342
+ Consider adding an environment configuration section to the wrangler.toml file:
2343
+ \`\`\`
2344
+ [env.DEV]
2345
+ \`\`\`
2346
+ "
2347
+ `);
2348
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2349
+ "Processing wrangler configuration:
2350
+ "
2351
+ `);
2352
+ });
2353
+
2354
+ it("should use top-level values for inheritable config fields", () => {
2355
+ const main = "src/index.ts";
2356
+ const resolvedMain = path.resolve(process.cwd(), main);
2357
+ const rawConfig: RawConfig = {
2358
+ name: "mock-name",
2359
+ account_id: "ACCOUNT_ID",
2360
+ compatibility_date: "2022-01-01",
2361
+ compatibility_flags: ["FLAG1", "FLAG2"],
2362
+ workers_dev: false,
2363
+ routes: ["ROUTE_1", "ROUTE_2"],
2364
+ jsx_factory: "JSX_FACTORY",
2365
+ jsx_fragment: "JSX_FRAGMENT",
2366
+ tsconfig: "path/to/tsconfig.json",
2367
+ triggers: { crons: ["CRON_1", "CRON_2"] },
2368
+ usage_model: "bundled",
2369
+ main,
2370
+ build: {
2371
+ command: "COMMAND",
2372
+ cwd: "CWD",
2373
+ watch_dir: "WATCH_DIR",
2374
+ },
2375
+ no_bundle: true,
2376
+ minify: true,
2377
+ node_compat: true,
2378
+ first_party_worker: true,
2379
+ };
2380
+
2381
+ const { config, diagnostics } = normalizeAndValidateConfig(
2382
+ { ...rawConfig, env: { dev: {} } },
2383
+ undefined,
2384
+ { env: "dev" }
2385
+ );
2386
+
2387
+ expect(config).toEqual(
2388
+ expect.objectContaining({
2389
+ ...rawConfig,
2390
+ main: resolvedMain,
2391
+ name: "mock-name-dev",
2392
+ })
2393
+ );
2394
+ expect(diagnostics.hasErrors()).toBe(false);
2395
+ expect(diagnostics.hasWarnings()).toBe(false);
2396
+ });
2397
+
2398
+ it("should override top-level values for inheritable config fields", () => {
2399
+ const main = "src/index.ts";
2400
+ const resolvedMain = path.resolve(process.cwd(), main);
2401
+ const rawEnv: RawEnvironment = {
2402
+ name: "mock-env-name",
2403
+ account_id: "ENV_ACCOUNT_ID",
2404
+ compatibility_date: "2022-02-02",
2405
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
2406
+ workers_dev: true,
2407
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
2408
+ jsx_factory: "ENV_JSX_FACTORY",
2409
+ jsx_fragment: "ENV_JSX_FRAGMENT",
2410
+ tsconfig: "ENV_path/to/tsconfig.json",
2411
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
2412
+ usage_model: "unbound",
2413
+ main,
2414
+ build: {
2415
+ command: "ENV_COMMAND",
2416
+ cwd: "ENV_CWD",
2417
+ watch_dir: "ENV_WATCH_DIR",
2418
+ },
2419
+ no_bundle: false,
2420
+ minify: false,
2421
+ node_compat: false,
2422
+ first_party_worker: false,
2423
+ };
2424
+ const rawConfig: RawConfig = {
2425
+ name: "mock-name",
2426
+ account_id: "ACCOUNT_ID",
2427
+ compatibility_date: "2022-01-01",
2428
+ compatibility_flags: ["FLAG1", "FLAG2"],
2429
+ workers_dev: false,
2430
+ routes: ["ROUTE_1", "ROUTE_2"],
2431
+ jsx_factory: "JSX_FACTORY",
2432
+ jsx_fragment: "JSX_FRAGMENT",
2433
+ tsconfig: "path/to/tsconfig.json",
2434
+ triggers: { crons: ["CRON_1", "CRON_2"] },
2435
+ usage_model: "bundled",
2436
+ main: "top-level.js",
2437
+ build: {
2438
+ command: "COMMAND",
2439
+ cwd: "CWD",
2440
+ watch_dir: "WATCH_DIR",
2441
+ },
2442
+ no_bundle: true,
2443
+ minify: true,
2444
+ node_compat: true,
2445
+ first_party_worker: true,
2446
+ env: {
2447
+ ENV1: rawEnv,
2448
+ },
2449
+ };
2450
+
2451
+ const { config, diagnostics } = normalizeAndValidateConfig(
2452
+ rawConfig,
2453
+ undefined,
2454
+ { env: "ENV1" }
2455
+ );
2456
+
2457
+ expect(config).toEqual(
2458
+ expect.objectContaining({ ...rawEnv, main: resolvedMain })
2459
+ );
2460
+ expect(diagnostics.hasErrors()).toBe(false);
2461
+ expect(diagnostics.hasWarnings()).toBe(false);
2462
+ });
2463
+
2464
+ describe("non-legacy", () => {
2465
+ it("should use top-level `name` field", () => {
2466
+ const rawConfig: RawConfig = {
2467
+ name: "mock-name",
2468
+ legacy_env: false,
2469
+ env: { DEV: {} },
2470
+ };
2471
+
2472
+ const { config, diagnostics } = normalizeAndValidateConfig(
2473
+ rawConfig,
2474
+ undefined,
2475
+ { env: "DEV" }
2476
+ );
2477
+
2478
+ expect(config.name).toEqual("mock-name");
2479
+ expect(diagnostics.hasErrors()).toBe(false);
2480
+ expect(diagnostics.hasWarnings()).toBe(true);
2481
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2482
+ "Processing wrangler configuration:
2483
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2484
+ `);
2485
+ });
2486
+
2487
+ it("should error if named environment contains a `name` field, even if there is no top-level name", () => {
2488
+ const rawConfig: RawConfig = {
2489
+ legacy_env: false,
2490
+ env: {
2491
+ DEV: {
2492
+ name: "mock-env-name",
2493
+ },
2494
+ },
2495
+ };
2496
+
2497
+ const { config, diagnostics } = normalizeAndValidateConfig(
2498
+ rawConfig,
2499
+ undefined,
2500
+ { env: "DEV" }
2501
+ );
2502
+
2503
+ expect(config.name).toBeUndefined();
2504
+ expect(diagnostics.hasWarnings()).toBe(true);
2505
+ expect(diagnostics.hasErrors()).toBe(true);
2506
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2507
+ "Processing wrangler configuration:
2508
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2509
+ `);
2510
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2511
+ "Processing wrangler configuration:
2512
+
2513
+ - \\"env.DEV\\" environment configuration
2514
+ - The \\"name\\" field is not allowed in named service environments.
2515
+ Please remove the field from this environment."
2516
+ `);
2517
+ });
2518
+
2519
+ it("should error if top-level config and a named environment both contain a `name` field", () => {
2520
+ const rawConfig: RawConfig = {
2521
+ name: "mock-name",
2522
+ legacy_env: false,
2523
+ env: {
2524
+ DEV: {
2525
+ name: "mock-env-name",
2526
+ },
2527
+ },
2528
+ };
2529
+
2530
+ const { config, diagnostics } = normalizeAndValidateConfig(
2531
+ rawConfig,
2532
+ undefined,
2533
+ { env: "DEV" }
2534
+ );
2535
+
2536
+ expect(config.name).toEqual("mock-name");
2537
+ expect(diagnostics.hasWarnings()).toBe(true);
2538
+ expect(diagnostics.hasErrors()).toBe(true);
2539
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2540
+ "Processing wrangler configuration:
2541
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2542
+ `);
2543
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2544
+ "Processing wrangler configuration:
2545
+
2546
+ - \\"env.DEV\\" environment configuration
2547
+ - The \\"name\\" field is not allowed in named service environments.
2548
+ Please remove the field from this environment."
2549
+ `);
2550
+ });
2551
+ });
2552
+
2553
+ it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
2554
+ const rawConfig: RawConfig = {
2555
+ legacy_env: false,
2556
+ env: {
2557
+ DEV: {
2558
+ account_id: "some_account_id",
2559
+ },
2560
+ },
2561
+ };
2562
+
2563
+ const { config, diagnostics } = normalizeAndValidateConfig(
2564
+ rawConfig,
2565
+ undefined,
2566
+ { env: "DEV" }
2567
+ );
2568
+
2569
+ expect(diagnostics.hasWarnings()).toBe(true);
2570
+ expect(config.account_id).toBeUndefined();
2571
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2572
+ "Processing wrangler configuration:
2573
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2574
+ `);
2575
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2576
+ "Processing wrangler configuration:
2577
+
2578
+ - \\"env.DEV\\" environment configuration
2579
+ - The \\"account_id\\" field is not allowed in named service environments.
2580
+ Please remove the field from this environment."
2581
+ `);
2582
+ });
2583
+
2584
+ it("should error if top-level config and a named environment both contain a `account_id` field", () => {
2585
+ const rawConfig: RawConfig = {
2586
+ account_id: "ACCOUNT_ID",
2587
+ legacy_env: false,
2588
+ env: {
2589
+ DEV: {
2590
+ account_id: "ENV_ACCOUNT_ID",
2591
+ },
2592
+ },
2593
+ };
2594
+
2595
+ const { config, diagnostics } = normalizeAndValidateConfig(
2596
+ rawConfig,
2597
+ undefined,
2598
+ { env: "DEV" }
2599
+ );
2600
+
2601
+ expect(config.account_id).toEqual("ACCOUNT_ID");
2602
+ expect(diagnostics.hasErrors()).toBe(true);
2603
+ expect(diagnostics.hasWarnings()).toBe(true);
2604
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2605
+ "Processing wrangler configuration:
2606
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2607
+ `);
2608
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2609
+ "Processing wrangler configuration:
2610
+
2611
+ - \\"env.DEV\\" environment configuration
2612
+ - The \\"account_id\\" field is not allowed in named service environments.
2613
+ Please remove the field from this environment."
2614
+ `);
2615
+ });
2616
+
2617
+ it("should warn for non-inherited fields that are missing in environments", () => {
2618
+ const define: RawConfig["define"] = {
2619
+ abc: "123",
2620
+ };
2621
+ const vars: RawConfig["vars"] = {
2622
+ FOO: "foo",
2623
+ };
2624
+ const durable_objects: RawConfig["durable_objects"] = {
2625
+ bindings: [],
2626
+ };
2627
+ const kv_namespaces: RawConfig["kv_namespaces"] = [];
2628
+ const r2_buckets: RawConfig["r2_buckets"] = [];
2629
+ const unsafe: RawConfig["unsafe"] = { bindings: [] };
2630
+ const rawConfig: RawConfig = {
2631
+ define,
2632
+ vars,
2633
+ durable_objects,
2634
+ kv_namespaces,
2635
+ r2_buckets,
2636
+ unsafe,
2637
+ env: {
2638
+ ENV1: {},
2639
+ },
2640
+ };
2641
+
2642
+ const { config, diagnostics } = normalizeAndValidateConfig(
2643
+ rawConfig,
2644
+ undefined,
2645
+ { env: "ENV1" }
2646
+ );
2647
+
2648
+ expect(config).toEqual(
2649
+ expect.not.objectContaining({
2650
+ define,
2651
+ vars,
2652
+ durable_objects,
2653
+ kv_namespaces,
2654
+ r2_buckets,
2655
+ unsafe,
2656
+ })
2657
+ );
2658
+ expect(diagnostics.hasErrors()).toBe(false);
2659
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2660
+ "Processing wrangler configuration:
2661
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
2662
+ - \\"env.ENV1\\" environment configuration
2663
+ - \\"vars\\" exists at the top level, but not on \\"env.ENV1\\".
2664
+ This is not what you probably want, since \\"vars\\" is not inherited by environments.
2665
+ Please add \\"vars\\" to \\"env.ENV1\\".
2666
+ - \\"define\\" exists at the top level, but not on \\"env.ENV1\\".
2667
+ This is not what you probably want, since \\"define\\" is not inherited by environments.
2668
+ Please add \\"define\\" to \\"env.ENV1\\".
2669
+ - \\"durable_objects\\" exists at the top level, but not on \\"env.ENV1\\".
2670
+ This is not what you probably want, since \\"durable_objects\\" is not inherited by environments.
2671
+ Please add \\"durable_objects\\" to \\"env.ENV1\\".
2672
+ - \\"kv_namespaces\\" exists at the top level, but not on \\"env.ENV1\\".
2673
+ This is not what you probably want, since \\"kv_namespaces\\" is not inherited by environments.
2674
+ Please add \\"kv_namespaces\\" to \\"env.ENV1\\".
2675
+ - \\"r2_buckets\\" exists at the top level, but not on \\"env.ENV1\\".
2676
+ This is not what you probably want, since \\"r2_buckets\\" is not inherited by environments.
2677
+ Please add \\"r2_buckets\\" to \\"env.ENV1\\".
2678
+ - \\"unsafe\\" exists at the top level, but not on \\"env.ENV1\\".
2679
+ This is not what you probably want, since \\"unsafe\\" is not inherited by environments.
2680
+ Please add \\"unsafe\\" to \\"env.ENV1\\"."
2681
+ `);
2682
+ });
2683
+
2684
+ it("should error on invalid environment values", () => {
2685
+ const expectedConfig: RawEnvironment = {
2686
+ name: 111,
2687
+ account_id: 222,
2688
+ compatibility_date: 333,
2689
+ compatibility_flags: [444, 555],
2690
+ workers_dev: "BAD",
2691
+ routes: [666, 777],
2692
+ route: 888,
2693
+ jsx_factory: 999,
2694
+ jsx_fragment: 1000,
2695
+ tsconfig: 123,
2696
+ triggers: { crons: [1111, 1222] },
2697
+ usage_model: "INVALID",
2698
+ main: 1333,
2699
+ build: {
2700
+ command: 1444,
2701
+ cwd: 1555,
2702
+ watch_dir: 1666,
2703
+ },
2704
+ no_bundle: "INVALID",
2705
+ minify: "INVALID",
2706
+ node_compat: "INVALID",
2707
+ first_party_worker: "INVALID",
2708
+ } as unknown as RawEnvironment;
2709
+
2710
+ const { config, diagnostics } = normalizeAndValidateConfig(
2711
+ { env: { ENV1: expectedConfig } },
2712
+ undefined,
2713
+ { env: "ENV1" }
2714
+ );
2715
+
2716
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
2717
+ expect(diagnostics.hasWarnings()).toBe(false);
2718
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2719
+ "Processing wrangler configuration:
2720
+
2721
+ - \\"env.ENV1\\" environment configuration
2722
+ - Expected \\"route\\" to be either a string, or an object with shape { pattern, custom_domain, zone_id | zone_name }, but got 888.
2723
+ - Expected \\"account_id\\" to be of type string but got 222.
2724
+ - Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, custom_domain, zone_id | zone_name }, but these weren't valid: [
2725
+ 666,
2726
+ 777
2727
+ ].
2728
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
2729
+ - Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
2730
+ - Expected \\"build.command\\" to be of type string but got 1444.
2731
+ - Expected \\"build.cwd\\" to be of type string but got 1555.
2732
+ - Expected \\"build.watch_dir\\" to be of type string but got 1666.
2733
+ - Expected \\"compatibility_date\\" to be of type string but got 333.
2734
+ - Expected \\"compatibility_flags\\" to be of type string array but got [444,555].
2735
+ - Expected \\"jsx_factory\\" to be of type string but got 999.
2736
+ - Expected \\"jsx_fragment\\" to be of type string but got 1000.
2737
+ - Expected \\"tsconfig\\" to be of type string but got 123.
2738
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got 111.
2739
+ - Expected \\"main\\" to be of type string but got 1333.
2740
+ - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\".
2741
+ - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\".
2742
+ - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\".
2743
+ - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\".
2744
+ - Expected \\"first_party_worker\\" to be of type boolean but got \\"INVALID\\"."
2745
+ `);
2746
+ });
2747
+
2748
+ describe("[define]", () => {
2749
+ it("should accept valid values for config.define", () => {
2750
+ const rawConfig: RawConfig = {
2751
+ define: {
2752
+ abc: "def",
2753
+ ghi: "123",
2754
+ },
2755
+ };
2756
+ const { config, diagnostics } = normalizeAndValidateConfig(
2757
+ rawConfig,
2758
+ undefined,
2759
+ { env: undefined }
2760
+ );
2761
+
2762
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2763
+ expect(diagnostics.hasWarnings()).toBe(false);
2764
+ expect(diagnostics.hasErrors()).toBe(false);
2765
+ });
2766
+
2767
+ it("should error if config.define is not an object", () => {
2768
+ const rawConfig: RawConfig = {
2769
+ // @ts-expect-error purposely using an invalid value
2770
+ define: 123,
2771
+ };
2772
+ const { config, diagnostics } = normalizeAndValidateConfig(
2773
+ rawConfig,
2774
+ undefined,
2775
+ { env: undefined }
2776
+ );
2777
+
2778
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2779
+ expect(diagnostics.hasWarnings()).toBe(false);
2780
+ expect(diagnostics.hasErrors()).toBe(true);
2781
+
2782
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2783
+ "Processing wrangler configuration:
2784
+ - The field \\"define\\" should be an object but got 123.
2785
+ "
2786
+ `);
2787
+ });
2788
+
2789
+ it("should error if the values on config.define are not strings", () => {
2790
+ const rawConfig: RawConfig = {
2791
+ define: {
2792
+ // @ts-expect-error purposely using an invalid value
2793
+ abc: 123,
2794
+ // This one's valid
2795
+ def: "xyz",
2796
+ // @ts-expect-error purposely using an invalid value
2797
+ ghi: true,
2798
+ // @ts-expect-error purposely using an invalid value
2799
+ jkl: {
2800
+ nested: "value",
2801
+ },
2802
+ },
2803
+ };
2804
+ const { config, diagnostics } = normalizeAndValidateConfig(
2805
+ rawConfig,
2806
+ undefined,
2807
+ { env: undefined }
2808
+ );
2809
+
2810
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2811
+ expect(diagnostics.hasWarnings()).toBe(false);
2812
+ expect(diagnostics.hasErrors()).toBe(true);
2813
+
2814
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2815
+ "Processing wrangler configuration:
2816
+ - The field \\"define.abc\\" should be a string but got 123.
2817
+ - The field \\"define.ghi\\" should be a string but got true.
2818
+ - The field \\"define.jkl\\" should be a string but got {\\"nested\\":\\"value\\"}."
2819
+ `);
2820
+ });
2821
+
2822
+ describe("named environments", () => {
2823
+ it("should accept valid values for config.define inside an environment", () => {
2824
+ const rawConfig: RawConfig = {
2825
+ define: {
2826
+ abc: "def",
2827
+ ghi: "123",
2828
+ },
2829
+ env: {
2830
+ ENV1: {
2831
+ define: {
2832
+ abc: "xyz",
2833
+ ghi: "456",
2834
+ },
2835
+ },
2836
+ },
2837
+ };
2838
+ const { config, diagnostics } = normalizeAndValidateConfig(
2839
+ rawConfig,
2840
+ undefined,
2841
+ { env: "ENV1" }
2842
+ );
2843
+
2844
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2845
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2846
+ expect(diagnostics.hasWarnings()).toBe(false);
2847
+ expect(diagnostics.hasErrors()).toBe(false);
2848
+ });
2849
+
2850
+ it("should error if config.define is not an object inside an environment", () => {
2851
+ const rawConfig: RawConfig = {
2852
+ define: {
2853
+ abc: "def",
2854
+ ghi: "123",
2855
+ },
2856
+ env: {
2857
+ ENV1: {
2858
+ // @ts-expect-error purposely using an invalid value
2859
+ define: 123,
2860
+ },
2861
+ },
2862
+ };
2863
+ const { config, diagnostics } = normalizeAndValidateConfig(
2864
+ rawConfig,
2865
+ undefined,
2866
+ { env: "ENV1" }
2867
+ );
2868
+
2869
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2870
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2871
+ expect(diagnostics.hasWarnings()).toBe(false);
2872
+ expect(diagnostics.hasErrors()).toBe(true);
2873
+
2874
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2875
+ "Processing wrangler configuration:
2876
+
2877
+ - \\"env.ENV1\\" environment configuration
2878
+ - The field \\"env.ENV1.define\\" should be an object but got 123.
2879
+ "
2880
+ `);
2881
+ });
2882
+
2883
+ it("should warn if if the shape of .define inside an environment doesn't match the shape of the top level .define", () => {
2884
+ const rawConfig: RawConfig = {
2885
+ define: {
2886
+ abc: "def",
2887
+ ghi: "123",
2888
+ },
2889
+ env: {
2890
+ ENV1: {
2891
+ define: {
2892
+ abc: "def",
2893
+ xyz: "123",
2894
+ },
2895
+ },
2896
+ },
2897
+ };
2898
+ const { config, diagnostics } = normalizeAndValidateConfig(
2899
+ rawConfig,
2900
+ undefined,
2901
+ { env: "ENV1" }
2902
+ );
2903
+
2904
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2905
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2906
+ expect(diagnostics.hasWarnings()).toBe(true);
2907
+ expect(diagnostics.hasErrors()).toBe(false);
2908
+
2909
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2910
+ "Processing wrangler configuration:
2911
+
2912
+ - \\"env.ENV1\\" environment configuration
2913
+ - \\"define.ghi\\" exists at the top level, but not on \\"env.ENV1.define\\".
2914
+ This is not what you probably want, since \\"define\\" configuration is not inherited by environments.
2915
+ Please add \\"define.ghi\\" to \\"env.ENV1\\".
2916
+ - \\"xyz\\" exists on \\"env.ENV1\\", but not on the top level.
2917
+ This is not what you probably want, since \\"define\\" configuration within environments can only override existing top level \\"define\\" configuration
2918
+ Please remove \\"env.ENV1.define.xyz\\", or add \\"define.xyz\\"."
2919
+ `);
2920
+ });
2921
+
2922
+ it("should error if the values on config.define in an environment are not strings", () => {
2923
+ const rawConfig: RawConfig = {
2924
+ define: {
2925
+ abc: "123",
2926
+ def: "xyz",
2927
+ ghi: "true",
2928
+ jkl: "some value",
2929
+ },
2930
+ env: {
2931
+ ENV1: {
2932
+ define: {
2933
+ // @ts-expect-error purposely using an invalid value
2934
+ abc: 123,
2935
+ // This one's valid
2936
+ def: "xyz",
2937
+ // @ts-expect-error purposely using an invalid value
2938
+ ghi: true,
2939
+ // @ts-expect-error purposely using an invalid value
2940
+ jkl: {
2941
+ nested: "value",
2942
+ },
2943
+ },
2944
+ },
2945
+ },
2946
+ };
2947
+ const { config, diagnostics } = normalizeAndValidateConfig(
2948
+ rawConfig,
2949
+ undefined,
2950
+ { env: "ENV1" }
2951
+ );
2952
+
2953
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2954
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2955
+ expect(diagnostics.hasWarnings()).toBe(false);
2956
+ expect(diagnostics.hasErrors()).toBe(true);
2957
+
2958
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2959
+ "Processing wrangler configuration:
2960
+
2961
+ - \\"env.ENV1\\" environment configuration
2962
+ - The field \\"env.ENV1.define.abc\\" should be a string but got 123.
2963
+ - The field \\"env.ENV1.define.ghi\\" should be a string but got true.
2964
+ - The field \\"env.ENV1.define.jkl\\" should be a string but got {\\"nested\\":\\"value\\"}."
2965
+ `);
2966
+ });
2967
+ });
2968
+ });
2969
+
2970
+ describe("[durable_objects]", () => {
2971
+ it("should error if durable_objects is an array", () => {
2972
+ const { config, diagnostics } = normalizeAndValidateConfig(
2973
+ { env: { ENV1: { durable_objects: [] } } } as unknown as RawConfig,
2974
+ undefined,
2975
+ { env: "ENV1" }
2976
+ );
2977
+
2978
+ expect(config).toEqual(
2979
+ expect.not.objectContaining({ durable_objects: expect.anything })
2980
+ );
2981
+ expect(diagnostics.hasWarnings()).toBe(false);
2982
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2983
+ "Processing wrangler configuration:
2984
+
2985
+ - \\"env.ENV1\\" environment configuration
2986
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got []."
2987
+ `);
2988
+ });
2989
+
2990
+ it("should error if durable_objects is a string", () => {
2991
+ const { config, diagnostics } = normalizeAndValidateConfig(
2992
+ { env: { ENV1: { durable_objects: "BAD" } } } as unknown as RawConfig,
2993
+ undefined,
2994
+ { env: "ENV1" }
2995
+ );
2996
+
2997
+ expect(config).toEqual(
2998
+ expect.not.objectContaining({ durable_objects: expect.anything })
2999
+ );
3000
+ expect(diagnostics.hasWarnings()).toBe(false);
3001
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3002
+ "Processing wrangler configuration:
3003
+
3004
+ - \\"env.ENV1\\" environment configuration
3005
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got \\"BAD\\"."
3006
+ `);
3007
+ });
3008
+
3009
+ it("should error if durable_objects is a number", () => {
3010
+ const { config, diagnostics } = normalizeAndValidateConfig(
3011
+ { env: { ENV1: { durable_objects: 999 } } } as unknown as RawConfig,
3012
+ undefined,
3013
+ { env: "ENV1" }
3014
+ );
3015
+
3016
+ expect(config).toEqual(
3017
+ expect.not.objectContaining({ durable_objects: expect.anything })
3018
+ );
3019
+ expect(diagnostics.hasWarnings()).toBe(false);
3020
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3021
+ "Processing wrangler configuration:
3022
+
3023
+ - \\"env.ENV1\\" environment configuration
3024
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got 999."
3025
+ `);
3026
+ });
3027
+
3028
+ it("should error if durable_objects is null", () => {
3029
+ const { config, diagnostics } = normalizeAndValidateConfig(
3030
+ { env: { ENV1: { durable_objects: null } } } as unknown as RawConfig,
3031
+ undefined,
3032
+ { env: "ENV1" }
3033
+ );
3034
+
3035
+ expect(config).toEqual(
3036
+ expect.not.objectContaining({ durable_objects: expect.anything })
3037
+ );
3038
+ expect(diagnostics.hasWarnings()).toBe(false);
3039
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3040
+ "Processing wrangler configuration:
3041
+
3042
+ - \\"env.ENV1\\" environment configuration
3043
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got null."
3044
+ `);
3045
+ });
3046
+
3047
+ it("should error if durable_objects.bindings is not defined", () => {
3048
+ const { config, diagnostics } = normalizeAndValidateConfig(
3049
+ { env: { ENV1: { durable_objects: {} } } } as unknown as RawConfig,
3050
+ undefined,
3051
+ { env: "ENV1" }
3052
+ );
3053
+
3054
+ expect(config).toEqual(
3055
+ expect.not.objectContaining({ durable_objects: expect.anything })
3056
+ );
3057
+ expect(diagnostics.hasWarnings()).toBe(false);
3058
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3059
+ "Processing wrangler configuration:
3060
+
3061
+ - \\"env.ENV1\\" environment configuration
3062
+ - The field \\"env.ENV1.durable_objects\\" is missing the required \\"bindings\\" property."
3063
+ `);
3064
+ });
3065
+
3066
+ it("should error if durable_objects.bindings is an object", () => {
3067
+ const { config, diagnostics } = normalizeAndValidateConfig(
3068
+ {
3069
+ env: { ENV1: { durable_objects: { bindings: {} } } },
3070
+ } as unknown as RawConfig,
3071
+ undefined,
3072
+ { env: "ENV1" }
3073
+ );
3074
+
3075
+ expect(config).toEqual(
3076
+ expect.not.objectContaining({
3077
+ durable_objects: { bindings: expect.anything },
3078
+ })
3079
+ );
3080
+ expect(diagnostics.hasWarnings()).toBe(false);
3081
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3082
+ "Processing wrangler configuration:
3083
+
3084
+ - \\"env.ENV1\\" environment configuration
3085
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got {}."
3086
+ `);
3087
+ });
3088
+
3089
+ it("should error if durable_objects.bindings is a string", () => {
3090
+ const { config, diagnostics } = normalizeAndValidateConfig(
3091
+ {
3092
+ env: { ENV1: { durable_objects: { bindings: "BAD" } } },
3093
+ } as unknown as RawConfig,
3094
+ undefined,
3095
+ { env: "ENV1" }
3096
+ );
3097
+
3098
+ expect(config).toEqual(
3099
+ expect.not.objectContaining({
3100
+ durable_objects: { bindings: expect.anything },
3101
+ })
3102
+ );
3103
+ expect(diagnostics.hasWarnings()).toBe(false);
3104
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3105
+ "Processing wrangler configuration:
3106
+
3107
+ - \\"env.ENV1\\" environment configuration
3108
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got \\"BAD\\"."
3109
+ `);
3110
+ });
3111
+
3112
+ it("should error if durable_objects.bindings is a number", () => {
3113
+ const { config, diagnostics } = normalizeAndValidateConfig(
3114
+ {
3115
+ env: { ENV1: { durable_objects: { bindings: 999 } } },
3116
+ } as unknown as RawConfig,
3117
+ undefined,
3118
+ { env: "ENV1" }
3119
+ );
3120
+
3121
+ expect(config).toEqual(
3122
+ expect.not.objectContaining({
3123
+ durable_objects: { bindings: expect.anything },
3124
+ })
3125
+ );
3126
+ expect(diagnostics.hasWarnings()).toBe(false);
3127
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3128
+ "Processing wrangler configuration:
3129
+
3130
+ - \\"env.ENV1\\" environment configuration
3131
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got 999."
3132
+ `);
3133
+ });
3134
+
3135
+ it("should error if durable_objects.bindings is null", () => {
3136
+ const { config, diagnostics } = normalizeAndValidateConfig(
3137
+ {
3138
+ env: { ENV1: { durable_objects: { bindings: null } } },
3139
+ } as unknown as RawConfig,
3140
+ undefined,
3141
+ { env: "ENV1" }
3142
+ );
3143
+
3144
+ expect(config).toEqual(
3145
+ expect.not.objectContaining({
3146
+ durable_objects: { bindings: expect.anything },
3147
+ })
3148
+ );
3149
+ expect(diagnostics.hasWarnings()).toBe(false);
3150
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3151
+ "Processing wrangler configuration:
3152
+
3153
+ - \\"env.ENV1\\" environment configuration
3154
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got null."
3155
+ `);
3156
+ });
3157
+
3158
+ it("should error if durable_objects.bindings are not valid", () => {
3159
+ const { config, diagnostics } = normalizeAndValidateConfig(
3160
+ {
3161
+ env: {
3162
+ ENV1: {
3163
+ durable_objects: {
3164
+ bindings: [
3165
+ {},
3166
+ { name: "VALID" },
3167
+ { name: 1555, class_name: 1666 },
3168
+ {
3169
+ name: 1777,
3170
+ class_name: 1888,
3171
+ script_name: 1999,
3172
+ },
3173
+ ],
3174
+ },
3175
+ },
3176
+ },
3177
+ } as unknown as RawConfig,
3178
+ undefined,
3179
+ { env: "ENV1" }
3180
+ );
3181
+
3182
+ expect(config).toEqual(
3183
+ expect.not.objectContaining({
3184
+ durable_objects: { bindings: expect.anything },
3185
+ })
3186
+ );
3187
+ expect(diagnostics.hasWarnings()).toBe(false);
3188
+ expect(diagnostics.hasErrors()).toBe(true);
3189
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3190
+ "Processing wrangler configuration:
3191
+
3192
+ - \\"env.ENV1\\" environment configuration
3193
+
3194
+ - \\"env.ENV1.durable_objects.bindings[0]\\": {}
3195
+ - binding should have a string \\"name\\" field.
3196
+ - binding should have a string \\"class_name\\" field.
3197
+
3198
+ - \\"env.ENV1.durable_objects.bindings[1]\\": {\\"name\\":\\"VALID\\"}
3199
+ - binding should have a string \\"class_name\\" field.
3200
+
3201
+ - \\"env.ENV1.durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
3202
+ - binding should have a string \\"name\\" field.
3203
+ - binding should have a string \\"class_name\\" field.
3204
+
3205
+ - \\"env.ENV1.durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
3206
+ - binding should have a string \\"name\\" field.
3207
+ - binding should have a string \\"class_name\\" field.
3208
+ - the field \\"script_name\\", when present, should be a string."
3209
+ `);
3210
+ });
3211
+ });
3212
+
3213
+ describe("[kv_namespaces]", () => {
3214
+ it("should error if kv_namespaces is an object", () => {
3215
+ const { config, diagnostics } = normalizeAndValidateConfig(
3216
+ { env: { ENV1: { kv_namespaces: {} } } } as unknown as RawConfig,
3217
+ undefined,
3218
+ { env: "ENV1" }
3219
+ );
3220
+
3221
+ expect(config).toEqual(
3222
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
3223
+ );
3224
+ expect(diagnostics.hasWarnings()).toBe(false);
3225
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3226
+ "Processing wrangler configuration:
3227
+
3228
+ - \\"env.ENV1\\" environment configuration
3229
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got {}."
3230
+ `);
3231
+ });
3232
+
3233
+ it("should error if kv_namespaces is a string", () => {
3234
+ const { config, diagnostics } = normalizeAndValidateConfig(
3235
+ { env: { ENV1: { kv_namespaces: "BAD" } } } as unknown as RawConfig,
3236
+ undefined,
3237
+ { env: "ENV1" }
3238
+ );
3239
+
3240
+ expect(config).toEqual(
3241
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
3242
+ );
3243
+ expect(diagnostics.hasWarnings()).toBe(false);
3244
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3245
+ "Processing wrangler configuration:
3246
+
3247
+ - \\"env.ENV1\\" environment configuration
3248
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got \\"BAD\\"."
3249
+ `);
3250
+ });
3251
+
3252
+ it("should error if kv_namespaces is a number", () => {
3253
+ const { config, diagnostics } = normalizeAndValidateConfig(
3254
+ { env: { ENV1: { kv_namespaces: 999 } } } as unknown as RawConfig,
3255
+ undefined,
3256
+ { env: "ENV1" }
3257
+ );
3258
+
3259
+ expect(config).toEqual(
3260
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
3261
+ );
3262
+ expect(diagnostics.hasWarnings()).toBe(false);
3263
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3264
+ "Processing wrangler configuration:
3265
+
3266
+ - \\"env.ENV1\\" environment configuration
3267
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got 999."
3268
+ `);
3269
+ });
3270
+
3271
+ it("should error if kv_namespaces is null", () => {
3272
+ const { config, diagnostics } = normalizeAndValidateConfig(
3273
+ { env: { ENV1: { kv_namespaces: null } } } as unknown as RawConfig,
3274
+ undefined,
3275
+ { env: "ENV1" }
3276
+ );
3277
+
3278
+ expect(config).toEqual(
3279
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
3280
+ );
3281
+ expect(diagnostics.hasWarnings()).toBe(false);
3282
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3283
+ "Processing wrangler configuration:
3284
+
3285
+ - \\"env.ENV1\\" environment configuration
3286
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got null."
3287
+ `);
3288
+ });
3289
+
3290
+ it("should error if kv_namespaces.bindings are not valid", () => {
3291
+ const { config, diagnostics } = normalizeAndValidateConfig(
3292
+ {
3293
+ env: {
3294
+ ENV1: {
3295
+ kv_namespaces: [
3296
+ {},
3297
+ { binding: "VALID" },
3298
+ { binding: 2000, id: 2111 },
3299
+ {
3300
+ binding: "KV_BINDING_2",
3301
+ id: "KV_ID_2",
3302
+ preview_id: 2222,
3303
+ },
3304
+ ],
3305
+ },
3306
+ },
3307
+ } as unknown as RawConfig,
3308
+ undefined,
3309
+ { env: "ENV1" }
3310
+ );
3311
+
3312
+ expect(config).toEqual(
3313
+ expect.not.objectContaining({
3314
+ kv_namespaces: { bindings: expect.anything },
3315
+ })
3316
+ );
3317
+ expect(diagnostics.hasWarnings()).toBe(false);
3318
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3319
+ "Processing wrangler configuration:
3320
+
3321
+ - \\"env.ENV1\\" environment configuration
3322
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
3323
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
3324
+ - \\"env.ENV1.kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
3325
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
3326
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
3327
+ - \\"env.ENV1.kv_namespaces[3]\\" bindings should, optionally, have a string \\"preview_id\\" field but got {\\"binding\\":\\"KV_BINDING_2\\",\\"id\\":\\"KV_ID_2\\",\\"preview_id\\":2222}."
3328
+ `);
3329
+ });
3330
+ });
3331
+
3332
+ describe("[r2_buckets]", () => {
3333
+ it("should error if r2_buckets is an object", () => {
3334
+ const { config, diagnostics } = normalizeAndValidateConfig(
3335
+ { env: { ENV1: { r2_buckets: {} } } } as unknown as RawConfig,
3336
+ undefined,
3337
+ { env: "ENV1" }
3338
+ );
3339
+
3340
+ expect(config).toEqual(
3341
+ expect.not.objectContaining({ r2_buckets: expect.anything })
3342
+ );
3343
+ expect(diagnostics.hasWarnings()).toBe(false);
3344
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3345
+ "Processing wrangler configuration:
3346
+
3347
+ - \\"env.ENV1\\" environment configuration
3348
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got {}."
3349
+ `);
3350
+ });
3351
+
3352
+ it("should error if r2_buckets is a string", () => {
3353
+ const { config, diagnostics } = normalizeAndValidateConfig(
3354
+ { env: { ENV1: { r2_buckets: "BAD" } } } as unknown as RawConfig,
3355
+ undefined,
3356
+ { env: "ENV1" }
3357
+ );
3358
+
3359
+ expect(config).toEqual(
3360
+ expect.not.objectContaining({ r2_buckets: expect.anything })
3361
+ );
3362
+ expect(diagnostics.hasWarnings()).toBe(false);
3363
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3364
+ "Processing wrangler configuration:
3365
+
3366
+ - \\"env.ENV1\\" environment configuration
3367
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got \\"BAD\\"."
3368
+ `);
3369
+ });
3370
+
3371
+ it("should error if r2_buckets is a number", () => {
3372
+ const { config, diagnostics } = normalizeAndValidateConfig(
3373
+ { env: { ENV1: { r2_buckets: 999 } } } as unknown as RawConfig,
3374
+ undefined,
3375
+ { env: "ENV1" }
3376
+ );
3377
+
3378
+ expect(config).toEqual(
3379
+ expect.not.objectContaining({ r2_buckets: expect.anything })
3380
+ );
3381
+ expect(diagnostics.hasWarnings()).toBe(false);
3382
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3383
+ "Processing wrangler configuration:
3384
+
3385
+ - \\"env.ENV1\\" environment configuration
3386
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got 999."
3387
+ `);
3388
+ });
3389
+
3390
+ it("should error if r2_buckets is null", () => {
3391
+ const { config, diagnostics } = normalizeAndValidateConfig(
3392
+ { env: { ENV1: { r2_buckets: null } } } as unknown as RawConfig,
3393
+ undefined,
3394
+ { env: "ENV1" }
3395
+ );
3396
+
3397
+ expect(config).toEqual(
3398
+ expect.not.objectContaining({ r2_buckets: expect.anything })
3399
+ );
3400
+ expect(diagnostics.hasWarnings()).toBe(false);
3401
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3402
+ "Processing wrangler configuration:
3403
+
3404
+ - \\"env.ENV1\\" environment configuration
3405
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got null."
3406
+ `);
3407
+ });
3408
+
3409
+ it("should error if r2_buckets.bindings are not valid", () => {
3410
+ const { config, diagnostics } = normalizeAndValidateConfig(
3411
+ {
3412
+ env: {
3413
+ ENV1: {
3414
+ r2_buckets: [
3415
+ {},
3416
+ { binding: "R2_BINDING_1" },
3417
+ { binding: 2333, bucket_name: 2444 },
3418
+ {
3419
+ binding: "R2_BINDING_2",
3420
+ bucket_name: "R2_BUCKET_2",
3421
+ preview_bucket_name: 2555,
3422
+ },
3423
+ ],
3424
+ },
3425
+ },
3426
+ } as unknown as RawConfig,
3427
+ undefined,
3428
+ { env: "ENV1" }
3429
+ );
3430
+
3431
+ expect(config).toEqual(
3432
+ expect.not.objectContaining({
3433
+ r2_buckets: { bindings: expect.anything },
3434
+ })
3435
+ );
3436
+ expect(diagnostics.hasWarnings()).toBe(false);
3437
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3438
+ "Processing wrangler configuration:
3439
+
3440
+ - \\"env.ENV1\\" environment configuration
3441
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
3442
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
3443
+ - \\"env.ENV1.r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
3444
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
3445
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
3446
+ - \\"env.ENV1.r2_buckets[3]\\" bindings should, optionally, have a string \\"preview_bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_2\\",\\"bucket_name\\":\\"R2_BUCKET_2\\",\\"preview_bucket_name\\":2555}."
3447
+ `);
3448
+ });
3449
+ });
3450
+
3451
+ describe("[unsafe.bindings]", () => {
3452
+ it("should error if unsafe is an array", () => {
3453
+ const { config, diagnostics } = normalizeAndValidateConfig(
3454
+ { env: { ENV1: { unsafe: [] } } } as unknown as RawConfig,
3455
+ undefined,
3456
+ { env: "ENV1" }
3457
+ );
3458
+
3459
+ expect(config).toEqual(
3460
+ expect.not.objectContaining({ unsafe: expect.anything })
3461
+ );
3462
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3463
+ "Processing wrangler configuration:
3464
+
3465
+ - \\"env.ENV1\\" environment configuration
3466
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3467
+ `);
3468
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3469
+ "Processing wrangler configuration:
3470
+
3471
+ - \\"env.ENV1\\" environment configuration
3472
+ - The field \\"env.ENV1.unsafe\\" should be an object but got []."
3473
+ `);
3474
+ });
3475
+
3476
+ it("should error if unsafe is a string", () => {
3477
+ const { config, diagnostics } = normalizeAndValidateConfig(
3478
+ { env: { ENV1: { unsafe: "BAD" } } } as unknown as RawConfig,
3479
+ undefined,
3480
+ { env: "ENV1" }
3481
+ );
3482
+
3483
+ expect(config).toEqual(
3484
+ expect.not.objectContaining({ unsafe: expect.anything })
3485
+ );
3486
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3487
+ "Processing wrangler configuration:
3488
+
3489
+ - \\"env.ENV1\\" environment configuration
3490
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3491
+ `);
3492
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3493
+ "Processing wrangler configuration:
3494
+
3495
+ - \\"env.ENV1\\" environment configuration
3496
+ - The field \\"env.ENV1.unsafe\\" should be an object but got \\"BAD\\"."
3497
+ `);
3498
+ });
3499
+
3500
+ it("should error if unsafe is a number", () => {
3501
+ const { config, diagnostics } = normalizeAndValidateConfig(
3502
+ { env: { ENV1: { unsafe: 999 } } } as unknown as RawConfig,
3503
+ undefined,
3504
+ { env: "ENV1" }
3505
+ );
3506
+
3507
+ expect(config).toEqual(
3508
+ expect.not.objectContaining({ unsafe: expect.anything })
3509
+ );
3510
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3511
+ "Processing wrangler configuration:
3512
+
3513
+ - \\"env.ENV1\\" environment configuration
3514
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3515
+ `);
3516
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3517
+ "Processing wrangler configuration:
3518
+
3519
+ - \\"env.ENV1\\" environment configuration
3520
+ - The field \\"env.ENV1.unsafe\\" should be an object but got 999."
3521
+ `);
3522
+ });
3523
+
3524
+ it("should error if unsafe is null", () => {
3525
+ const { config, diagnostics } = normalizeAndValidateConfig(
3526
+ { env: { ENV1: { unsafe: null } } } as unknown as RawConfig,
3527
+ undefined,
3528
+ { env: "ENV1" }
3529
+ );
3530
+
3531
+ expect(config).toEqual(
3532
+ expect.not.objectContaining({ unsafe: expect.anything })
3533
+ );
3534
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3535
+ "Processing wrangler configuration:
3536
+
3537
+ - \\"env.ENV1\\" environment configuration
3538
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3539
+ `);
3540
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3541
+ "Processing wrangler configuration:
3542
+
3543
+ - \\"env.ENV1\\" environment configuration
3544
+ - The field \\"env.ENV1.unsafe\\" should be an object but got null."
3545
+ `);
3546
+ });
3547
+
3548
+ it("should error if unsafe.bindings is not defined", () => {
3549
+ const { config, diagnostics } = normalizeAndValidateConfig(
3550
+ { env: { ENV1: { unsafe: {} } } } as unknown as RawConfig,
3551
+ undefined,
3552
+ { env: "ENV1" }
3553
+ );
3554
+
3555
+ expect(config).toEqual(
3556
+ expect.not.objectContaining({ unsafe: expect.anything })
3557
+ );
3558
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3559
+ "Processing wrangler configuration:
3560
+
3561
+ - \\"env.ENV1\\" environment configuration
3562
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3563
+ `);
3564
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3565
+ "Processing wrangler configuration:
3566
+
3567
+ - \\"env.ENV1\\" environment configuration
3568
+ - The field \\"env.ENV1.unsafe\\" is missing the required \\"bindings\\" property."
3569
+ `);
3570
+ });
3571
+
3572
+ it("should error if unsafe.bindings is an object", () => {
3573
+ const { config, diagnostics } = normalizeAndValidateConfig(
3574
+ {
3575
+ env: { ENV1: { unsafe: { bindings: {} } } },
3576
+ } as unknown as RawConfig,
3577
+ undefined,
3578
+ { env: "ENV1" }
3579
+ );
3580
+
3581
+ expect(config).toEqual(
3582
+ expect.not.objectContaining({
3583
+ unsafe: { bindings: expect.anything },
3584
+ })
3585
+ );
3586
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3587
+ "Processing wrangler configuration:
3588
+
3589
+ - \\"env.ENV1\\" environment configuration
3590
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3591
+ `);
3592
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3593
+ "Processing wrangler configuration:
3594
+
3595
+ - \\"env.ENV1\\" environment configuration
3596
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got {}."
3597
+ `);
3598
+ });
3599
+
3600
+ it("should error if unsafe.bindings is a string", () => {
3601
+ const { config, diagnostics } = normalizeAndValidateConfig(
3602
+ {
3603
+ env: { ENV1: { unsafe: { bindings: "BAD" } } },
3604
+ } as unknown as RawConfig,
3605
+ undefined,
3606
+ { env: "ENV1" }
3607
+ );
3608
+
3609
+ expect(config).toEqual(
3610
+ expect.not.objectContaining({
3611
+ unsafe: { bindings: expect.anything },
3612
+ })
3613
+ );
3614
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3615
+ "Processing wrangler configuration:
3616
+
3617
+ - \\"env.ENV1\\" environment configuration
3618
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3619
+ `);
3620
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3621
+ "Processing wrangler configuration:
3622
+
3623
+ - \\"env.ENV1\\" environment configuration
3624
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got \\"BAD\\"."
3625
+ `);
3626
+ });
3627
+
3628
+ it("should error if unsafe.bindings is a number", () => {
3629
+ const { config, diagnostics } = normalizeAndValidateConfig(
3630
+ {
3631
+ env: { ENV1: { unsafe: { bindings: 999 } } },
3632
+ } as unknown as RawConfig,
3633
+ undefined,
3634
+ { env: "ENV1" }
3635
+ );
3636
+
3637
+ expect(config).toEqual(
3638
+ expect.not.objectContaining({
3639
+ unsafe: { bindings: expect.anything },
3640
+ })
3641
+ );
3642
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3643
+ "Processing wrangler configuration:
3644
+
3645
+ - \\"env.ENV1\\" environment configuration
3646
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3647
+ `);
3648
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3649
+ "Processing wrangler configuration:
3650
+
3651
+ - \\"env.ENV1\\" environment configuration
3652
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got 999."
3653
+ `);
3654
+ });
3655
+
3656
+ it("should error if unsafe.bindings is null", () => {
3657
+ const { config, diagnostics } = normalizeAndValidateConfig(
3658
+ {
3659
+ env: { ENV1: { unsafe: { bindings: null } } },
3660
+ } as unknown as RawConfig,
3661
+ undefined,
3662
+ { env: "ENV1" }
3663
+ );
3664
+
3665
+ expect(config).toEqual(
3666
+ expect.not.objectContaining({
3667
+ unsafe: { bindings: expect.anything },
3668
+ })
3669
+ );
3670
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3671
+ "Processing wrangler configuration:
3672
+
3673
+ - \\"env.ENV1\\" environment configuration
3674
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3675
+ `);
3676
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3677
+ "Processing wrangler configuration:
3678
+
3679
+ - \\"env.ENV1\\" environment configuration
3680
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got null."
3681
+ `);
3682
+ });
3683
+
3684
+ it("should error if unsafe.bindings are not valid", () => {
3685
+ const { config, diagnostics } = normalizeAndValidateConfig(
3686
+ {
3687
+ env: {
3688
+ ENV1: {
3689
+ unsafe: {
3690
+ bindings: [
3691
+ {},
3692
+ { name: "UNSAFE_BINDING_1" },
3693
+ { name: 2666, type: 2777 },
3694
+ {
3695
+ name: "UNSAFE_BINDING_2",
3696
+ type: "UNSAFE_TYPE_2",
3697
+ extra: 2888,
3698
+ },
3699
+ ],
3700
+ },
3701
+ },
3702
+ },
3703
+ } as unknown as RawConfig,
3704
+ undefined,
3705
+ { env: "ENV1" }
3706
+ );
3707
+
3708
+ expect(config).toEqual(
3709
+ expect.not.objectContaining({
3710
+ durable_objects: { bindings: expect.anything },
3711
+ })
3712
+ );
3713
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3714
+ "Processing wrangler configuration:
3715
+
3716
+ - \\"env.ENV1\\" environment configuration
3717
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3718
+ `);
3719
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3720
+ "Processing wrangler configuration:
3721
+
3722
+ - \\"env.ENV1\\" environment configuration
3723
+
3724
+ - \\"env.ENV1.unsafe.bindings[0]\\": {}
3725
+ - binding should have a string \\"name\\" field.
3726
+ - binding should have a string \\"type\\" field.
3727
+
3728
+ - \\"env.ENV1.unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
3729
+ - binding should have a string \\"type\\" field.
3730
+
3731
+ - \\"env.ENV1.unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
3732
+ - binding should have a string \\"name\\" field.
3733
+ - binding should have a string \\"type\\" field."
3734
+ `);
3735
+ });
3736
+ });
3737
+
3738
+ describe("(deprecated)", () => {
3739
+ it("should remove and warn about deprecated properties", () => {
3740
+ const environment: RawEnvironment = {
3741
+ zone_id: "ZONE_ID",
3742
+ "kv-namespaces": "BAD_KV_NAMESPACE",
3743
+ experimental_services: [
3744
+ {
3745
+ name: "mock-name",
3746
+ service: "SERVICE",
3747
+ environment: "ENV",
3748
+ },
3749
+ ],
3750
+ };
3751
+
3752
+ const { config, diagnostics } = normalizeAndValidateConfig(
3753
+ {
3754
+ env: {
3755
+ ENV1: environment,
3756
+ },
3757
+ },
3758
+ undefined,
3759
+ { env: "ENV1" }
3760
+ );
3761
+
3762
+ expect("experimental_services" in config).toBe(false);
3763
+ // Zone is not removed yet, since `route` commands might use it
3764
+ expect(config.zone_id).toEqual("ZONE_ID");
3765
+ expect(diagnostics.hasErrors()).toBe(false);
3766
+ expect(diagnostics.hasWarnings()).toBe(true);
3767
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3768
+ "Processing wrangler configuration:
3769
+
3770
+ - \\"env.ENV1\\" environment configuration
3771
+ - Deprecation: \\"kv-namespaces\\":
3772
+ The \\"kv-namespaces\\" field is no longer supported, please rename to \\"kv_namespaces\\"
3773
+ - Deprecation: \\"zone_id\\":
3774
+ This is unnecessary since we can deduce this from routes directly.
3775
+ - Deprecation: \\"experimental_services\\":
3776
+ The \\"experimental_services\\" field is no longer supported. Simply rename the [experimental_services] field to [services]."
3777
+ `);
3778
+ });
3779
+ });
3780
+
3781
+ describe("route & routes fields", () => {
3782
+ it("should error if both route and routes are specified in the same environment", () => {
3783
+ const environment: RawEnvironment = {
3784
+ name: "mock-env-name",
3785
+ account_id: "ENV_ACCOUNT_ID",
3786
+ compatibility_date: "2022-02-02",
3787
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3788
+ workers_dev: true,
3789
+ route: "ENV_ROUTE_1",
3790
+ routes: ["ENV_ROUTE_2", "ENV_ROUTE_3"],
3791
+ jsx_factory: "ENV_JSX_FACTORY",
3792
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3793
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3794
+ usage_model: "unbound",
3795
+ };
3796
+ const expectedConfig: RawConfig = {
3797
+ name: "mock-name",
3798
+ account_id: "ACCOUNT_ID",
3799
+ compatibility_date: "2022-01-01",
3800
+ compatibility_flags: ["FLAG1", "FLAG2"],
3801
+ workers_dev: false,
3802
+ routes: ["ROUTE_1", "ROUTE_2"],
3803
+ jsx_factory: "JSX_FACTORY",
3804
+ jsx_fragment: "JSX_FRAGMENT",
3805
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3806
+ usage_model: "bundled",
3807
+ env: {
3808
+ ENV1: environment,
3809
+ },
3810
+ };
3811
+
3812
+ const { config, diagnostics } = normalizeAndValidateConfig(
3813
+ expectedConfig,
3814
+ undefined,
3815
+ { env: "ENV1" }
3816
+ );
3817
+
3818
+ expect(config).toEqual(expect.objectContaining(environment));
3819
+ expect(diagnostics.hasErrors()).toBe(true);
3820
+ expect(diagnostics.hasWarnings()).toBe(false);
3821
+
3822
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3823
+ "Processing wrangler configuration:
3824
+
3825
+ - \\"env.ENV1\\" environment configuration
3826
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
3827
+ `);
3828
+ });
3829
+
3830
+ it("should error if both route and routes are specified in the top-level environment", () => {
3831
+ const environment: RawEnvironment = {
3832
+ name: "mock-env-name",
3833
+ account_id: "ENV_ACCOUNT_ID",
3834
+ compatibility_date: "2022-02-02",
3835
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3836
+ workers_dev: true,
3837
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
3838
+ jsx_factory: "ENV_JSX_FACTORY",
3839
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3840
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3841
+ usage_model: "unbound",
3842
+ };
3843
+ const expectedConfig: RawConfig = {
3844
+ name: "mock-name",
3845
+ account_id: "ACCOUNT_ID",
3846
+ compatibility_date: "2022-01-01",
3847
+ compatibility_flags: ["FLAG1", "FLAG2"],
3848
+ workers_dev: false,
3849
+ route: "ROUTE_1",
3850
+ routes: ["ROUTE_2", "ROUTE_3"],
3851
+ jsx_factory: "JSX_FACTORY",
3852
+ jsx_fragment: "JSX_FRAGMENT",
3853
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3854
+ usage_model: "bundled",
3855
+ env: {
3856
+ ENV1: environment,
3857
+ },
3858
+ };
3859
+
3860
+ const { config, diagnostics } = normalizeAndValidateConfig(
3861
+ expectedConfig,
3862
+ undefined,
3863
+ { env: "ENV1" }
3864
+ );
3865
+
3866
+ expect(config).toEqual(expect.objectContaining(environment));
3867
+ expect(diagnostics.hasErrors()).toBe(true);
3868
+ expect(diagnostics.hasWarnings()).toBe(false);
3869
+
3870
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3871
+ "Processing wrangler configuration:
3872
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
3873
+ `);
3874
+ });
3875
+
3876
+ it("should not error if <env>.route and <top-level>.routes are specified", () => {
3877
+ const environment: RawEnvironment = {
3878
+ name: "mock-env-name",
3879
+ account_id: "ENV_ACCOUNT_ID",
3880
+ compatibility_date: "2022-02-02",
3881
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3882
+ workers_dev: true,
3883
+ route: "ENV_ROUTE_1",
3884
+ jsx_factory: "ENV_JSX_FACTORY",
3885
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3886
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3887
+ usage_model: "unbound",
3888
+ };
3889
+ const expectedConfig: RawConfig = {
3890
+ name: "mock-name",
3891
+ account_id: "ACCOUNT_ID",
3892
+ compatibility_date: "2022-01-01",
3893
+ compatibility_flags: ["FLAG1", "FLAG2"],
3894
+ workers_dev: false,
3895
+ routes: ["ROUTE_1", "ROUTE_2"],
3896
+ jsx_factory: "JSX_FACTORY",
3897
+ jsx_fragment: "JSX_FRAGMENT",
3898
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3899
+ usage_model: "bundled",
3900
+ env: {
3901
+ ENV1: environment,
3902
+ },
3903
+ };
3904
+
3905
+ const { config, diagnostics } = normalizeAndValidateConfig(
3906
+ expectedConfig,
3907
+ undefined,
3908
+ { env: "ENV1" }
3909
+ );
3910
+
3911
+ expect(config).toEqual(expect.objectContaining(environment));
3912
+ expect(diagnostics.hasErrors()).toBe(false);
3913
+ expect(diagnostics.hasWarnings()).toBe(false);
3914
+ });
3915
+
3916
+ it("should not error if <env>.routes and <top-level>.route are specified", () => {
3917
+ const environment: RawEnvironment = {
3918
+ name: "mock-env-name",
3919
+ account_id: "ENV_ACCOUNT_ID",
3920
+ compatibility_date: "2022-02-02",
3921
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3922
+ workers_dev: true,
3923
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
3924
+ jsx_factory: "ENV_JSX_FACTORY",
3925
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3926
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3927
+ usage_model: "unbound",
3928
+ };
3929
+ const expectedConfig: RawConfig = {
3930
+ name: "mock-name",
3931
+ account_id: "ACCOUNT_ID",
3932
+ compatibility_date: "2022-01-01",
3933
+ compatibility_flags: ["FLAG1", "FLAG2"],
3934
+ workers_dev: false,
3935
+ route: "ROUTE_1",
3936
+ jsx_factory: "JSX_FACTORY",
3937
+ jsx_fragment: "JSX_FRAGMENT",
3938
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3939
+ usage_model: "bundled",
3940
+ env: {
3941
+ ENV1: environment,
3942
+ },
3943
+ };
3944
+
3945
+ const { config, diagnostics } = normalizeAndValidateConfig(
3946
+ expectedConfig,
3947
+ undefined,
3948
+ { env: "ENV1" }
3949
+ );
3950
+
3951
+ expect(config).toEqual(expect.objectContaining(environment));
3952
+ expect(diagnostics.hasErrors()).toBe(false);
3953
+ expect(diagnostics.hasWarnings()).toBe(false);
3954
+ });
3955
+
3956
+ it("should not error if <env1>.route and <env2>.routes are specified", () => {
3957
+ const environment1: RawEnvironment = {
3958
+ name: "mock-env-name",
3959
+ account_id: "ENV_ACCOUNT_ID",
3960
+ compatibility_date: "2022-02-02",
3961
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3962
+ workers_dev: true,
3963
+ routes: ["ENV1_ROUTE_1", "ENV2_ROUTE_2"],
3964
+ jsx_factory: "ENV_JSX_FACTORY",
3965
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3966
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3967
+ usage_model: "unbound",
3968
+ };
3969
+ const environment2: RawEnvironment = {
3970
+ name: "mock-env-name",
3971
+ account_id: "ENV_ACCOUNT_ID",
3972
+ compatibility_date: "2022-02-02",
3973
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3974
+ workers_dev: true,
3975
+ route: "ENV2_ROUTE_1",
3976
+ jsx_factory: "ENV_JSX_FACTORY",
3977
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3978
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3979
+ usage_model: "unbound",
3980
+ };
3981
+ const expectedConfig: RawConfig = {
3982
+ name: "mock-name",
3983
+ account_id: "ACCOUNT_ID",
3984
+ compatibility_date: "2022-01-01",
3985
+ compatibility_flags: ["FLAG1", "FLAG2"],
3986
+ workers_dev: false,
3987
+ route: "ROUTE_1",
3988
+ jsx_factory: "JSX_FACTORY",
3989
+ jsx_fragment: "JSX_FRAGMENT",
3990
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3991
+ usage_model: "bundled",
3992
+ env: {
3993
+ ENV1: environment1,
3994
+ ENV2: environment2,
3995
+ },
3996
+ };
3997
+
3998
+ const result1 = normalizeAndValidateConfig(expectedConfig, undefined, {
3999
+ env: "ENV1",
4000
+ });
4001
+
4002
+ expect(result1.config).toEqual(expect.objectContaining(environment1));
4003
+ expect(result1.diagnostics.hasErrors()).toBe(false);
4004
+ expect(result1.diagnostics.hasWarnings()).toBe(false);
4005
+
4006
+ const result2 = normalizeAndValidateConfig(expectedConfig, undefined, {
4007
+ env: "ENV2",
4008
+ });
4009
+
4010
+ expect(result2.config).toEqual(expect.objectContaining(environment2));
4011
+ expect(result2.diagnostics.hasErrors()).toBe(false);
4012
+ expect(result2.diagnostics.hasWarnings()).toBe(false);
4013
+ });
4014
+ });
4015
+ });
4016
+ });
4017
+
4018
+ function normalizePath(text: string): string {
4019
+ return text
4020
+ .replace("project\\wrangler.toml", "project/wrangler.toml")
4021
+ .replace("src\\index.ts", "src/index.ts");
4022
+ }