wrangler 0.0.0-ece06ea → 0.0.0-ed08df4

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