wrangler 0.0.0-e2a26a0 → 0.0.0-e3278fa

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