wrangler 0.0.0-ece06ea → 0.0.0-ecfbb0c

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