wrangler 0.0.0-e151223 → 0.0.0-e29a241

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