wrangler 0.0.0-ddde8d4 → 0.0.0-e043479

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 (46) hide show
  1. package/package.json +7 -5
  2. package/src/__tests__/configuration.test.ts +2186 -0
  3. package/src/__tests__/dev.test.tsx +218 -75
  4. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  5. package/src/__tests__/helpers/mock-account-id.ts +18 -4
  6. package/src/__tests__/helpers/mock-console.ts +17 -9
  7. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  8. package/src/__tests__/helpers/write-wrangler-toml.ts +2 -5
  9. package/src/__tests__/index.test.ts +125 -49
  10. package/src/__tests__/jest.setup.ts +16 -4
  11. package/src/__tests__/kv.test.ts +399 -132
  12. package/src/__tests__/parse.test.ts +322 -0
  13. package/src/__tests__/publish.test.ts +791 -382
  14. package/src/__tests__/r2.test.ts +16 -24
  15. package/src/__tests__/secret.test.ts +299 -84
  16. package/src/__tests__/tail.test.ts +309 -176
  17. package/src/api/form_data.ts +43 -7
  18. package/src/api/preview.ts +36 -17
  19. package/src/api/worker.ts +18 -6
  20. package/src/bundle.ts +21 -15
  21. package/src/cfetch/index.ts +7 -5
  22. package/src/cfetch/internal.ts +24 -7
  23. package/src/config/README.md +107 -0
  24. package/src/config/config.ts +270 -0
  25. package/src/config/diagnostics.ts +80 -0
  26. package/src/config/environment.ts +305 -0
  27. package/src/config/index.ts +52 -0
  28. package/src/config/validation-helpers.ts +370 -0
  29. package/src/config/validation.ts +1019 -0
  30. package/src/dev.tsx +123 -49
  31. package/src/entry.ts +57 -0
  32. package/src/guess-worker-format.ts +1 -1
  33. package/src/index.tsx +631 -869
  34. package/src/kv.tsx +101 -44
  35. package/src/module-collection.ts +133 -47
  36. package/src/package-manager.ts +21 -15
  37. package/src/parse.ts +183 -0
  38. package/src/proxy.ts +18 -10
  39. package/src/publish.ts +70 -65
  40. package/src/sites.tsx +5 -11
  41. package/src/tail/index.ts +17 -7
  42. package/src/user.tsx +19 -5
  43. package/templates/tsconfig.json +105 -0
  44. package/wrangler-dist/cli.js +6177 -2424
  45. package/wrangler-dist/cli.js.map +3 -3
  46. package/src/config.ts +0 -671
@@ -0,0 +1,2186 @@
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({}, undefined);
8
+
9
+ expect(config).toEqual({
10
+ account_id: undefined,
11
+ build: {
12
+ command: undefined,
13
+ cwd: undefined,
14
+ watch_dir: undefined,
15
+ },
16
+ compatibility_date: undefined,
17
+ compatibility_flags: [],
18
+ configPath: undefined,
19
+ dev: {
20
+ ip: "127.0.0.1",
21
+ local_protocol: "http",
22
+ port: 8787,
23
+ upstream_protocol: "https",
24
+ host: undefined,
25
+ },
26
+ durable_objects: {
27
+ bindings: [],
28
+ },
29
+ env: {},
30
+ jsx_factory: "React.createElement",
31
+ jsx_fragment: "React.Fragment",
32
+ kv_namespaces: [],
33
+ legacy_env: true,
34
+ main: undefined,
35
+ migrations: [],
36
+ name: undefined,
37
+ r2_buckets: [],
38
+ route: undefined,
39
+ routes: undefined,
40
+ rules: [],
41
+ site: undefined,
42
+ text_blobs: undefined,
43
+ triggers: {
44
+ crons: [],
45
+ },
46
+ unsafe: {
47
+ bindings: [],
48
+ },
49
+ usage_model: undefined,
50
+ vars: {},
51
+ wasm_modules: undefined,
52
+ workers_dev: true,
53
+ zone_id: undefined,
54
+ });
55
+ expect(diagnostics.hasErrors()).toBe(false);
56
+ expect(diagnostics.hasWarnings()).toBe(false);
57
+ });
58
+
59
+ describe("top-level non-environment configuration", () => {
60
+ it("should override config defaults with provided values", () => {
61
+ const main = "src/index.ts";
62
+ const expectedConfig: RawConfig = {
63
+ main,
64
+ legacy_env: true,
65
+ dev: {
66
+ ip: "255.255.255.255",
67
+ port: 9999,
68
+ local_protocol: "HTTPS",
69
+ upstream_protocol: "HTTP",
70
+ },
71
+ build: {
72
+ command: "COMMAND",
73
+ cwd: "CWD",
74
+ watch_dir: "WATCH_DIR",
75
+ },
76
+ };
77
+
78
+ const { config, diagnostics } = normalizeAndValidateConfig(
79
+ expectedConfig,
80
+ undefined
81
+ );
82
+
83
+ const resolvedMain = path.resolve(process.cwd(), main);
84
+ expect(config).toEqual(
85
+ expect.objectContaining({ ...expectedConfig, main: resolvedMain })
86
+ );
87
+ expect(diagnostics.hasErrors()).toBe(false);
88
+ expect(diagnostics.hasWarnings()).toBe(false);
89
+ });
90
+
91
+ it("should error on invalid top level fields", () => {
92
+ const expectedConfig = {
93
+ main: 111,
94
+ legacy_env: "FOO",
95
+ dev: {
96
+ ip: 222,
97
+ port: "FOO",
98
+ local_protocol: 333,
99
+ upstream_protocol: 444,
100
+ },
101
+ build: {
102
+ command: 555,
103
+ cwd: 666,
104
+ watch_dir: 777,
105
+ },
106
+ };
107
+
108
+ const { config, diagnostics } = normalizeAndValidateConfig(
109
+ expectedConfig as unknown as RawConfig,
110
+ undefined
111
+ );
112
+
113
+ expect(config).toEqual(
114
+ expect.objectContaining({ ...expectedConfig, main: undefined })
115
+ );
116
+ expect(diagnostics.hasWarnings()).toBe(false);
117
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
118
+ "Processing wrangler configuration:
119
+ - ERROR: \\"build.command\\" should be of type string but got 555.
120
+ - ERROR: \\"build.cwd\\" should be of type string but got 666.
121
+ - ERROR: \\"build.watch_dir\\" should be of type string but got 777.
122
+ - ERROR: \\"legacy_env\\" should be of type boolean but got \\"FOO\\".
123
+ - ERROR: \\"main\\" should be a string but got 111
124
+ - ERROR: \\"dev.ip\\" should be of type string but got 222.
125
+ - ERROR: \\"dev.port\\" should be of type number but got \\"FOO\\".
126
+ - ERROR: \\"dev.local_protocol\\" should be of type string but got 333.
127
+ - ERROR: \\"dev.upstream_protocol\\" should be of type string but got 444."
128
+ `);
129
+ });
130
+
131
+ it("should warn on and remove unexpected top level fields", () => {
132
+ const expectedConfig = {
133
+ miniflare: {
134
+ host: "127.0.0.1",
135
+ },
136
+ };
137
+
138
+ const { config, diagnostics } = normalizeAndValidateConfig(
139
+ expectedConfig as unknown as RawConfig,
140
+ undefined
141
+ );
142
+
143
+ expect("miniflare" in config).toBe(false);
144
+ expect(diagnostics.hasErrors()).toBe(false);
145
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
146
+ "Processing wrangler configuration:
147
+ - Unexpected fields found in top-level field: \\"miniflare\\""
148
+ `);
149
+ });
150
+
151
+ it("should override build.upload config defaults with provided values and warn about deprecations", () => {
152
+ const expectedConfig: RawConfig = {
153
+ build: {
154
+ upload: {
155
+ dir: "src",
156
+ format: "modules",
157
+ main: "index.ts",
158
+ rules: [{ type: "Text", globs: ["GLOB"], fallthrough: true }],
159
+ },
160
+ },
161
+ };
162
+
163
+ const { config, diagnostics } = normalizeAndValidateConfig(
164
+ expectedConfig,
165
+ path.resolve("project/wrangler.toml")
166
+ );
167
+
168
+ expect(config.main).toEqual(path.resolve("project/src/index.ts"));
169
+ expect(config.build.upload).toBeUndefined();
170
+ expect(diagnostics.hasErrors()).toBe(false);
171
+ expect(diagnostics.hasWarnings()).toBe(true);
172
+ expect(normalizePath(diagnostics.renderWarnings()))
173
+ .toMatchInlineSnapshot(`
174
+ "Processing project/wrangler.toml configuration:
175
+ - DEPRECATION: \\"build.upload.format\\":
176
+ The format is inferred automatically from the code.
177
+ - DEPRECATION: \\"build.upload.main\\":
178
+ Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
179
+ Then add the top level \`main\` field to your configuration file:
180
+ \`\`\`
181
+ main = \\"src/index.ts\\"
182
+ \`\`\`
183
+ - DEPRECATION: \\"build.upload.dir\\":
184
+ Use the top level \\"main\\" field or a command-line argument to specify the entry-point for the Worker.
185
+ - 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:
186
+ \`\`\`
187
+ [[rules]]
188
+ type = \\"Text\\"
189
+ globs = [ \\"GLOB\\" ]
190
+ fallthrough = true
191
+ \`\`\`"
192
+ `);
193
+ });
194
+
195
+ it("should override `migrations` config defaults with provided values", () => {
196
+ const expectedConfig: RawConfig = {
197
+ migrations: [
198
+ {
199
+ tag: "TAG",
200
+ new_classes: ["CLASS_1", "CLASS_2"],
201
+ renamed_classes: [
202
+ {
203
+ from: "FROM_CLASS",
204
+ to: "TO_CLASS",
205
+ },
206
+ ],
207
+ deleted_classes: ["CLASS_3", "CLASS_4"],
208
+ },
209
+ ],
210
+ };
211
+
212
+ const { config, diagnostics } = normalizeAndValidateConfig(
213
+ expectedConfig,
214
+ undefined
215
+ );
216
+
217
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
218
+ expect(diagnostics.hasErrors()).toBe(false);
219
+ expect(diagnostics.hasWarnings()).toBe(false);
220
+ });
221
+
222
+ it("should error on invalid `migrations` values", () => {
223
+ const expectedConfig = {
224
+ migrations: [
225
+ {
226
+ tag: 111,
227
+ new_classes: [222, 333],
228
+ renamed_classes: [
229
+ {
230
+ from: 444,
231
+ to: 555,
232
+ },
233
+ ],
234
+ deleted_classes: [666, 777],
235
+ },
236
+ ],
237
+ };
238
+
239
+ const { config, diagnostics } = normalizeAndValidateConfig(
240
+ expectedConfig as unknown as RawConfig,
241
+ undefined
242
+ );
243
+
244
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
245
+ expect(diagnostics.hasWarnings()).toBe(false);
246
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
247
+ "Processing wrangler configuration:
248
+ - ERROR: \\"migrations[0].tag\\" should be of type string but got 111.
249
+ - ERROR: \\"migrations[0].new_classes.[0]\\" should be of type string but got 222.
250
+ - ERROR: \\"migrations[0].new_classes.[1]\\" should be of type string but got 333.
251
+ - \\"migrations[0].renamed_classes\\" should be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
252
+ - ERROR: \\"migrations[0].deleted_classes.[0]\\" should be of type string but got 666.
253
+ - ERROR: \\"migrations[0].deleted_classes.[1]\\" should be of type string but got 777."
254
+ `);
255
+ });
256
+
257
+ it("should override `site` config defaults with provided values", () => {
258
+ const expectedConfig: RawConfig = {
259
+ site: {
260
+ bucket: "BUCKET",
261
+ "entry-point": "ENTRY_POINT",
262
+ include: ["INCLUDE_1", "INCLUDE_2"],
263
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
264
+ },
265
+ };
266
+
267
+ const { config, diagnostics } = normalizeAndValidateConfig(
268
+ expectedConfig,
269
+ undefined
270
+ );
271
+
272
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
273
+ expect(diagnostics.hasErrors()).toBe(false);
274
+ expect(diagnostics.hasWarnings()).toBe(false);
275
+ });
276
+
277
+ it("should error if `site` config is missing `bucket`", () => {
278
+ const expectedConfig: RawConfig = {
279
+ site: {
280
+ "entry-point": "ENTRY_POINT",
281
+ include: ["INCLUDE_1", "INCLUDE_2"],
282
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
283
+ },
284
+ } as RawConfig;
285
+
286
+ const { config, diagnostics } = normalizeAndValidateConfig(
287
+ expectedConfig,
288
+ undefined
289
+ );
290
+
291
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
292
+ expect(diagnostics.hasErrors()).toBe(true);
293
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
294
+ "Processing wrangler configuration:
295
+ - ERROR: \\"site.bucket\\" is a required field."
296
+ `);
297
+ expect(diagnostics.hasWarnings()).toBe(false);
298
+ });
299
+
300
+ it("should error on invalid `site` values", () => {
301
+ const expectedConfig = {
302
+ site: {
303
+ bucket: "BUCKET",
304
+ "entry-point": 111,
305
+ include: [222, 333],
306
+ exclude: [444, 555],
307
+ },
308
+ };
309
+
310
+ const { config, diagnostics } = normalizeAndValidateConfig(
311
+ expectedConfig as unknown as RawConfig,
312
+ undefined
313
+ );
314
+
315
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
316
+ expect(diagnostics.hasWarnings()).toBe(false);
317
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
318
+ "Processing wrangler configuration:
319
+ - ERROR: \\"site.entry_point\\" should be of type string but got 111.
320
+ - ERROR: \\"sites.include.[0]\\" should be of type string but got 222.
321
+ - ERROR: \\"sites.include.[1]\\" should be of type string but got 333.
322
+ - ERROR: \\"sites.exclude.[0]\\" should be of type string but got 444.
323
+ - ERROR: \\"sites.exclude.[1]\\" should be of type string but got 555."
324
+ `);
325
+ });
326
+
327
+ it("should map `wasm_module` paths from relative to the config path to relative to the cwd", () => {
328
+ const expectedConfig: RawConfig = {
329
+ wasm_modules: {
330
+ MODULE_1: "path/to/module_1.mjs",
331
+ MODULE_2: "module_2.mjs",
332
+ },
333
+ };
334
+
335
+ const { config, diagnostics } = normalizeAndValidateConfig(
336
+ expectedConfig,
337
+ "project/wrangler.toml"
338
+ );
339
+
340
+ expect(config).toEqual(
341
+ expect.objectContaining({
342
+ wasm_modules: {
343
+ MODULE_1: path.normalize("project/path/to/module_1.mjs"),
344
+ MODULE_2: path.normalize("project/module_2.mjs"),
345
+ },
346
+ })
347
+ );
348
+ expect(diagnostics.hasErrors()).toBe(false);
349
+ expect(diagnostics.hasWarnings()).toBe(false);
350
+ });
351
+
352
+ it("should error on invalid `wasm_module` paths", () => {
353
+ const expectedConfig = {
354
+ wasm_modules: {
355
+ MODULE_1: 111,
356
+ MODULE_2: 222,
357
+ },
358
+ };
359
+
360
+ const { config, diagnostics } = normalizeAndValidateConfig(
361
+ expectedConfig as unknown as RawConfig,
362
+ "project/wrangler.toml"
363
+ );
364
+
365
+ expect(config).toEqual(
366
+ expect.objectContaining({
367
+ wasm_modules: {},
368
+ })
369
+ );
370
+ expect(diagnostics.hasWarnings()).toBe(false);
371
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
372
+ "Processing project/wrangler.toml configuration:
373
+ - Expected \\"wasm_modules['MODULE_1']\\" field to be a string but got 111.
374
+ - Expected \\"wasm_modules['MODULE_2']\\" field to be a string but got 222."
375
+ `);
376
+ });
377
+
378
+ it("should map `text_blobs` paths from relative to the config path to relative to the cwd", () => {
379
+ const expectedConfig: RawConfig = {
380
+ text_blobs: {
381
+ BLOB_1: "path/to/text1.txt",
382
+ BLOB_2: "text2.md",
383
+ },
384
+ };
385
+
386
+ const { config, diagnostics } = normalizeAndValidateConfig(
387
+ expectedConfig,
388
+ "project/wrangler.toml"
389
+ );
390
+
391
+ expect(config).toEqual(
392
+ expect.objectContaining({
393
+ text_blobs: {
394
+ BLOB_1: path.normalize("project/path/to/text1.txt"),
395
+ BLOB_2: path.normalize("project/text2.md"),
396
+ },
397
+ })
398
+ );
399
+ expect(diagnostics.hasErrors()).toBe(false);
400
+ expect(diagnostics.hasWarnings()).toBe(false);
401
+ });
402
+
403
+ it("should error on invalid `text_blob` paths", () => {
404
+ const expectedConfig = {
405
+ text_blobs: {
406
+ MODULE_1: 111,
407
+ MODULE_2: 222,
408
+ },
409
+ };
410
+
411
+ const { config, diagnostics } = normalizeAndValidateConfig(
412
+ expectedConfig as unknown as RawConfig,
413
+ "project/wrangler.toml"
414
+ );
415
+
416
+ expect(config).toEqual(
417
+ expect.objectContaining({
418
+ text_blobs: {},
419
+ })
420
+ );
421
+ expect(diagnostics.hasWarnings()).toBe(false);
422
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
423
+ "Processing project/wrangler.toml configuration:
424
+ - Expected \\"text_blobs['MODULE_1']\\" field to be a string but got 111.
425
+ - Expected \\"text_blobs['MODULE_2']\\" field to be a string but got 222."
426
+ `);
427
+ });
428
+
429
+ describe("(deprecated)", () => {
430
+ it("should remove and warn about deprecated properties", () => {
431
+ const rawConfig: RawConfig = {
432
+ type: "webpack",
433
+ webpack_config: "CONFIG",
434
+ };
435
+
436
+ const { config, diagnostics } = normalizeAndValidateConfig(
437
+ rawConfig,
438
+ undefined
439
+ );
440
+
441
+ // Note the `.not.` here...
442
+ expect(config).toEqual(
443
+ expect.not.objectContaining({
444
+ type: expect.anything(),
445
+ webpack_config: expect.anything(),
446
+ })
447
+ );
448
+ expect(diagnostics.hasErrors()).toBe(false);
449
+ expect(diagnostics.hasWarnings()).toBe(true);
450
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
451
+ "Processing wrangler configuration:
452
+ - DEPRECATION: \\"type\\":
453
+ 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.
454
+ - DEPRECATION: \\"webpack_config\\":
455
+ 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."
456
+ `);
457
+ });
458
+ });
459
+ });
460
+
461
+ describe("top-level environment configuration", () => {
462
+ it("should override config defaults with provided values", () => {
463
+ const expectedConfig: RawConfig = {
464
+ name: "NAME",
465
+ account_id: "ACCOUNT_ID",
466
+ compatibility_date: "2022-01-01",
467
+ compatibility_flags: ["FLAG1", "FLAG2"],
468
+ workers_dev: false,
469
+ routes: ["ROUTE_1", "ROUTE_2"],
470
+ route: "ROUTE_3",
471
+ jsx_factory: "JSX_FACTORY",
472
+ jsx_fragment: "JSX_FRAGMENT",
473
+ triggers: { crons: ["CRON_1", "CRON_2"] },
474
+ usage_model: "bundled",
475
+ vars: {
476
+ VAR1: "VALUE_1",
477
+ VAR2: "VALUE_2",
478
+ },
479
+ durable_objects: {
480
+ bindings: [
481
+ { name: "DO_BINDING_1", class_name: "CLASS1" },
482
+ {
483
+ name: "DO_BINDING_2",
484
+ class_name: "CLASS2",
485
+ script_name: "SCRIPT2",
486
+ },
487
+ ],
488
+ },
489
+ kv_namespaces: [
490
+ { binding: "KV_BINDING_1", id: "KV_ID_1" },
491
+ {
492
+ binding: "KV_BINDING_2",
493
+ id: "KV_ID_2",
494
+ preview_id: "KV_PREVIEW_1",
495
+ },
496
+ ],
497
+ r2_buckets: [
498
+ { binding: "R2_BINDING_1", bucket_name: "R2_BUCKET_1" },
499
+ {
500
+ binding: "R2_BINDING_2",
501
+ bucket_name: "R2_BUCKET_2",
502
+ preview_bucket_name: "R2_PREVIEW_2",
503
+ },
504
+ ],
505
+ unsafe: {
506
+ bindings: [
507
+ { name: "UNSAFE_BINDING_1", type: "UNSAFE_TYPE_1" },
508
+ {
509
+ name: "UNSAFE_BINDING_2",
510
+ type: "UNSAFE_TYPE_2",
511
+ extra: "UNSAFE_EXTRA_1",
512
+ },
513
+ ],
514
+ },
515
+ };
516
+
517
+ const { config, diagnostics } = normalizeAndValidateConfig(
518
+ expectedConfig,
519
+ undefined
520
+ );
521
+
522
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
523
+ expect(diagnostics.hasErrors()).toBe(false);
524
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
525
+ "Processing wrangler configuration:
526
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
527
+ `);
528
+ });
529
+
530
+ it("should error on invalid environment values", () => {
531
+ const expectedConfig: RawConfig = {
532
+ name: 111,
533
+ account_id: 222,
534
+ compatibility_date: 333,
535
+ compatibility_flags: [444, 555],
536
+ workers_dev: "BAD",
537
+ routes: [666, 777],
538
+ route: 888,
539
+ jsx_factory: 999,
540
+ jsx_fragment: 1000,
541
+ triggers: { crons: [1111, 1222] },
542
+ usage_model: "INVALID",
543
+ } as unknown as RawConfig;
544
+
545
+ const { config, diagnostics } = normalizeAndValidateConfig(
546
+ expectedConfig,
547
+ undefined
548
+ );
549
+
550
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
551
+ expect(diagnostics.hasWarnings()).toBe(false);
552
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
553
+ "Processing wrangler configuration:
554
+ - Expected \\"route\\" field to be a string but got 888.
555
+ - Expected \\"routes\\" field to be an array of strings but got [666,777].
556
+ - Expected \\"workers_dev\\" field to be a boolean but got \\"BAD\\".
557
+ - Expected \\"account_id\\" field to be a string but got 222.
558
+ - Expected \\"compatibility_date\\" field to be a string but got 333.
559
+ - Expected \\"compatibility_flags\\" field to be an array of strings but got [444,555].
560
+ - Expected \\"jsx_factory\\" field to be a string but got 999.
561
+ - Expected \\"jsx_fragment\\" field to be a string but got 1000.
562
+ - Expected \\"name\\" field to be a string but got 111.
563
+ - Expected \\"usage_model\\" field to be on of bundled,unbound but got \\"INVALID\\"."
564
+ `);
565
+ });
566
+
567
+ describe("durable_objects field", () => {
568
+ it("should error if durable_objects is an array", () => {
569
+ const { config, diagnostics } = normalizeAndValidateConfig(
570
+ { durable_objects: [] } as unknown as RawConfig,
571
+ undefined
572
+ );
573
+
574
+ expect(config).toEqual(
575
+ expect.not.objectContaining({ durable_objects: expect.anything })
576
+ );
577
+ expect(diagnostics.hasWarnings()).toBe(false);
578
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
579
+ "Processing wrangler configuration:
580
+ - The field \\"durable_objects\\" should be an object but got []."
581
+ `);
582
+ });
583
+
584
+ it("should error if durable_objects is a string", () => {
585
+ const { config, diagnostics } = normalizeAndValidateConfig(
586
+ { durable_objects: "BAD" } as unknown as RawConfig,
587
+ undefined
588
+ );
589
+
590
+ expect(config).toEqual(
591
+ expect.not.objectContaining({ durable_objects: expect.anything })
592
+ );
593
+ expect(diagnostics.hasWarnings()).toBe(false);
594
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
595
+ "Processing wrangler configuration:
596
+ - The field \\"durable_objects\\" should be an object but got \\"BAD\\"."
597
+ `);
598
+ });
599
+
600
+ it("should error if durable_objects is a number", () => {
601
+ const { config, diagnostics } = normalizeAndValidateConfig(
602
+ { durable_objects: 999 } as unknown as RawConfig,
603
+ undefined
604
+ );
605
+
606
+ expect(config).toEqual(
607
+ expect.not.objectContaining({ durable_objects: expect.anything })
608
+ );
609
+ expect(diagnostics.hasWarnings()).toBe(false);
610
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
611
+ "Processing wrangler configuration:
612
+ - The field \\"durable_objects\\" should be an object but got 999."
613
+ `);
614
+ });
615
+
616
+ it("should error if durable_objects is null", () => {
617
+ const { config, diagnostics } = normalizeAndValidateConfig(
618
+ { durable_objects: null } as unknown as RawConfig,
619
+ undefined
620
+ );
621
+
622
+ expect(config).toEqual(
623
+ expect.not.objectContaining({ durable_objects: expect.anything })
624
+ );
625
+ expect(diagnostics.hasWarnings()).toBe(false);
626
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
627
+ "Processing wrangler configuration:
628
+ - The field \\"durable_objects\\" should be an object but got null."
629
+ `);
630
+ });
631
+
632
+ it("should error if durable_objects.bindings is not defined", () => {
633
+ const { config, diagnostics } = normalizeAndValidateConfig(
634
+ { durable_objects: {} } as unknown as RawConfig,
635
+ undefined
636
+ );
637
+
638
+ expect(config).toEqual(
639
+ expect.not.objectContaining({ durable_objects: expect.anything })
640
+ );
641
+ expect(diagnostics.hasWarnings()).toBe(false);
642
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
643
+ "Processing wrangler configuration:
644
+ - The field \\"durable_objects\\" is missing the required \\"bindings\\" property."
645
+ `);
646
+ });
647
+
648
+ it("should error if durable_objects.bindings is an object", () => {
649
+ const { config, diagnostics } = normalizeAndValidateConfig(
650
+ { durable_objects: { bindings: {} } } as unknown as RawConfig,
651
+ undefined
652
+ );
653
+
654
+ expect(config).toEqual(
655
+ expect.not.objectContaining({
656
+ durable_objects: { bindings: expect.anything },
657
+ })
658
+ );
659
+ expect(diagnostics.hasWarnings()).toBe(false);
660
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
661
+ "Processing wrangler configuration:
662
+ - The field \\"durable_objects.bindings\\" should be an array but got {}."
663
+ `);
664
+ });
665
+
666
+ it("should error if durable_objects.bindings is a string", () => {
667
+ const { config, diagnostics } = normalizeAndValidateConfig(
668
+ { durable_objects: { bindings: "BAD" } } as unknown as RawConfig,
669
+ undefined
670
+ );
671
+
672
+ expect(config).toEqual(
673
+ expect.not.objectContaining({
674
+ durable_objects: { bindings: expect.anything },
675
+ })
676
+ );
677
+ expect(diagnostics.hasWarnings()).toBe(false);
678
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
679
+ "Processing wrangler configuration:
680
+ - The field \\"durable_objects.bindings\\" should be an array but got \\"BAD\\"."
681
+ `);
682
+ });
683
+
684
+ it("should error if durable_objects.bindings is a number", () => {
685
+ const { config, diagnostics } = normalizeAndValidateConfig(
686
+ { durable_objects: { bindings: 999 } } as unknown as RawConfig,
687
+ undefined
688
+ );
689
+
690
+ expect(config).toEqual(
691
+ expect.not.objectContaining({
692
+ durable_objects: { bindings: expect.anything },
693
+ })
694
+ );
695
+ expect(diagnostics.hasWarnings()).toBe(false);
696
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
697
+ "Processing wrangler configuration:
698
+ - The field \\"durable_objects.bindings\\" should be an array but got 999."
699
+ `);
700
+ });
701
+
702
+ it("should error if durable_objects.bindings is null", () => {
703
+ const { config, diagnostics } = normalizeAndValidateConfig(
704
+ { durable_objects: { bindings: null } } as unknown as RawConfig,
705
+ undefined
706
+ );
707
+
708
+ expect(config).toEqual(
709
+ expect.not.objectContaining({
710
+ durable_objects: { bindings: expect.anything },
711
+ })
712
+ );
713
+ expect(diagnostics.hasWarnings()).toBe(false);
714
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
715
+ "Processing wrangler configuration:
716
+ - The field \\"durable_objects.bindings\\" should be an array but got null."
717
+ `);
718
+ });
719
+
720
+ it("should error if durable_objects.bindings are not valid", () => {
721
+ const { config, diagnostics } = normalizeAndValidateConfig(
722
+ {
723
+ durable_objects: {
724
+ bindings: [
725
+ {},
726
+ { name: "VALID" },
727
+ { name: 1555, class_name: 1666 },
728
+ {
729
+ name: 1777,
730
+ class_name: 1888,
731
+ script_name: 1999,
732
+ },
733
+ ],
734
+ },
735
+ } as unknown as RawConfig,
736
+ undefined
737
+ );
738
+
739
+ expect(config).toEqual(
740
+ expect.not.objectContaining({
741
+ durable_objects: { bindings: expect.anything },
742
+ })
743
+ );
744
+ expect(diagnostics.hasWarnings()).toBe(false);
745
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
746
+ "Processing wrangler configuration:
747
+
748
+ - \\"durable_objects.bindings[0]\\": {}
749
+ - ERROR: bindings should have a string \\"name\\" field.
750
+ - ERROR: should have a string \\"class_name\\" field.
751
+
752
+ - \\"durable_objects.bindings[1]\\": {\\"name\\":\\"VALID\\"}
753
+ - ERROR: should have a string \\"class_name\\" field.
754
+
755
+ - \\"durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
756
+ - ERROR: bindings should have a string \\"name\\" field.
757
+ - ERROR: should have a string \\"class_name\\" field.
758
+
759
+ - \\"durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
760
+ - ERROR: bindings should have a string \\"name\\" field.
761
+ - ERROR: should have a string \\"class_name\\" field.
762
+ - ERROR: should, optionally, have a string \\"script_name\\" field."
763
+ `);
764
+ });
765
+ });
766
+
767
+ describe("kv_namespaces field", () => {
768
+ it("should error if kv_namespaces is an object", () => {
769
+ const { config, diagnostics } = normalizeAndValidateConfig(
770
+ { kv_namespaces: {} } as unknown as RawConfig,
771
+ undefined
772
+ );
773
+
774
+ expect(config).toEqual(
775
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
776
+ );
777
+ expect(diagnostics.hasWarnings()).toBe(false);
778
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
779
+ "Processing wrangler configuration:
780
+ - The field \\"kv_namespaces\\" should be an array but got {}."
781
+ `);
782
+ });
783
+
784
+ it("should error if kv_namespaces is a string", () => {
785
+ const { config, diagnostics } = normalizeAndValidateConfig(
786
+ { kv_namespaces: "BAD" } as unknown as RawConfig,
787
+ undefined
788
+ );
789
+
790
+ expect(config).toEqual(
791
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
792
+ );
793
+ expect(diagnostics.hasWarnings()).toBe(false);
794
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
795
+ "Processing wrangler configuration:
796
+ - The field \\"kv_namespaces\\" should be an array but got \\"BAD\\"."
797
+ `);
798
+ });
799
+
800
+ it("should error if kv_namespaces is a number", () => {
801
+ const { config, diagnostics } = normalizeAndValidateConfig(
802
+ { kv_namespaces: 999 } as unknown as RawConfig,
803
+ undefined
804
+ );
805
+
806
+ expect(config).toEqual(
807
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
808
+ );
809
+ expect(diagnostics.hasWarnings()).toBe(false);
810
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
811
+ "Processing wrangler configuration:
812
+ - The field \\"kv_namespaces\\" should be an array but got 999."
813
+ `);
814
+ });
815
+
816
+ it("should error if kv_namespaces is null", () => {
817
+ const { config, diagnostics } = normalizeAndValidateConfig(
818
+ { kv_namespaces: null } as unknown as RawConfig,
819
+ undefined
820
+ );
821
+
822
+ expect(config).toEqual(
823
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
824
+ );
825
+ expect(diagnostics.hasWarnings()).toBe(false);
826
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
827
+ "Processing wrangler configuration:
828
+ - The field \\"kv_namespaces\\" should be an array but got null."
829
+ `);
830
+ });
831
+
832
+ it("should error if kv_namespaces.bindings are not valid", () => {
833
+ const { config, diagnostics } = normalizeAndValidateConfig(
834
+ {
835
+ kv_namespaces: [
836
+ {},
837
+ { binding: "VALID" },
838
+ { binding: 2000, id: 2111 },
839
+ {
840
+ binding: "KV_BINDING_2",
841
+ id: "KV_ID_2",
842
+ preview_id: 2222,
843
+ },
844
+ ],
845
+ } as unknown as RawConfig,
846
+ undefined
847
+ );
848
+
849
+ expect(config).toEqual(
850
+ expect.not.objectContaining({
851
+ kv_namespaces: { bindings: expect.anything },
852
+ })
853
+ );
854
+ expect(diagnostics.hasWarnings()).toBe(false);
855
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
856
+ "Processing wrangler configuration:
857
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
858
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
859
+ - \\"kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
860
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
861
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
862
+ - \\"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}."
863
+ `);
864
+ });
865
+ });
866
+
867
+ describe("r2_buckets field", () => {
868
+ it("should error if r2_buckets is an object", () => {
869
+ const { config, diagnostics } = normalizeAndValidateConfig(
870
+ { r2_buckets: {} } as unknown as RawConfig,
871
+ undefined
872
+ );
873
+
874
+ expect(config).toEqual(
875
+ expect.not.objectContaining({ r2_buckets: expect.anything })
876
+ );
877
+ expect(diagnostics.hasWarnings()).toBe(false);
878
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
879
+ "Processing wrangler configuration:
880
+ - The field \\"r2_buckets\\" should be an array but got {}."
881
+ `);
882
+ });
883
+
884
+ it("should error if r2_buckets is a string", () => {
885
+ const { config, diagnostics } = normalizeAndValidateConfig(
886
+ { r2_buckets: "BAD" } as unknown as RawConfig,
887
+ undefined
888
+ );
889
+
890
+ expect(config).toEqual(
891
+ expect.not.objectContaining({ r2_buckets: expect.anything })
892
+ );
893
+ expect(diagnostics.hasWarnings()).toBe(false);
894
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
895
+ "Processing wrangler configuration:
896
+ - The field \\"r2_buckets\\" should be an array but got \\"BAD\\"."
897
+ `);
898
+ });
899
+
900
+ it("should error if r2_buckets is a number", () => {
901
+ const { config, diagnostics } = normalizeAndValidateConfig(
902
+ { r2_buckets: 999 } as unknown as RawConfig,
903
+ undefined
904
+ );
905
+
906
+ expect(config).toEqual(
907
+ expect.not.objectContaining({ r2_buckets: expect.anything })
908
+ );
909
+ expect(diagnostics.hasWarnings()).toBe(false);
910
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
911
+ "Processing wrangler configuration:
912
+ - The field \\"r2_buckets\\" should be an array but got 999."
913
+ `);
914
+ });
915
+
916
+ it("should error if r2_buckets is null", () => {
917
+ const { config, diagnostics } = normalizeAndValidateConfig(
918
+ { r2_buckets: null } as unknown as RawConfig,
919
+ undefined
920
+ );
921
+
922
+ expect(config).toEqual(
923
+ expect.not.objectContaining({ r2_buckets: expect.anything })
924
+ );
925
+ expect(diagnostics.hasWarnings()).toBe(false);
926
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
927
+ "Processing wrangler configuration:
928
+ - The field \\"r2_buckets\\" should be an array but got null."
929
+ `);
930
+ });
931
+
932
+ it("should error if r2_buckets.bindings are not valid", () => {
933
+ const { config, diagnostics } = normalizeAndValidateConfig(
934
+ {
935
+ r2_buckets: [
936
+ {},
937
+ { binding: "R2_BINDING_1" },
938
+ { binding: 2333, bucket_name: 2444 },
939
+ {
940
+ binding: "R2_BINDING_2",
941
+ bucket_name: "R2_BUCKET_2",
942
+ preview_bucket_name: 2555,
943
+ },
944
+ ],
945
+ } as unknown as RawConfig,
946
+ undefined
947
+ );
948
+
949
+ expect(config).toEqual(
950
+ expect.not.objectContaining({
951
+ r2_buckets: { bindings: expect.anything },
952
+ })
953
+ );
954
+ expect(diagnostics.hasWarnings()).toBe(false);
955
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
956
+ "Processing wrangler configuration:
957
+ - \\"r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
958
+ - \\"r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
959
+ - \\"r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
960
+ - \\"r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
961
+ - \\"r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
962
+ - \\"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}."
963
+ `);
964
+ });
965
+ });
966
+
967
+ describe("unsafe field", () => {
968
+ it("should error if unsafe is an array", () => {
969
+ const { config, diagnostics } = normalizeAndValidateConfig(
970
+ { unsafe: [] } as unknown as RawConfig,
971
+ undefined
972
+ );
973
+
974
+ expect(config).toEqual(
975
+ expect.not.objectContaining({ unsafe: expect.anything })
976
+ );
977
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
978
+ "Processing wrangler configuration:
979
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
980
+ `);
981
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
982
+ "Processing wrangler configuration:
983
+ - The field \\"unsafe\\" should be an object but got []."
984
+ `);
985
+ });
986
+
987
+ it("should error if unsafe is a string", () => {
988
+ const { config, diagnostics } = normalizeAndValidateConfig(
989
+ { unsafe: "BAD" } as unknown as RawConfig,
990
+ undefined
991
+ );
992
+
993
+ expect(config).toEqual(
994
+ expect.not.objectContaining({ unsafe: expect.anything })
995
+ );
996
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
997
+ "Processing wrangler configuration:
998
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
999
+ `);
1000
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1001
+ "Processing wrangler configuration:
1002
+ - The field \\"unsafe\\" should be an object but got \\"BAD\\"."
1003
+ `);
1004
+ });
1005
+
1006
+ it("should error if unsafe is a number", () => {
1007
+ const { config, diagnostics } = normalizeAndValidateConfig(
1008
+ { unsafe: 999 } as unknown as RawConfig,
1009
+ undefined
1010
+ );
1011
+
1012
+ expect(config).toEqual(
1013
+ expect.not.objectContaining({ unsafe: expect.anything })
1014
+ );
1015
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1016
+ "Processing wrangler configuration:
1017
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1018
+ `);
1019
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1020
+ "Processing wrangler configuration:
1021
+ - The field \\"unsafe\\" should be an object but got 999."
1022
+ `);
1023
+ });
1024
+
1025
+ it("should error if unsafe is null", () => {
1026
+ const { config, diagnostics } = normalizeAndValidateConfig(
1027
+ { unsafe: null } as unknown as RawConfig,
1028
+ undefined
1029
+ );
1030
+
1031
+ expect(config).toEqual(
1032
+ expect.not.objectContaining({ unsafe: expect.anything })
1033
+ );
1034
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1035
+ "Processing wrangler configuration:
1036
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1037
+ `);
1038
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1039
+ "Processing wrangler configuration:
1040
+ - The field \\"unsafe\\" should be an object but got null."
1041
+ `);
1042
+ });
1043
+
1044
+ it("should error if unsafe.bindings is not defined", () => {
1045
+ const { config, diagnostics } = normalizeAndValidateConfig(
1046
+ { unsafe: {} } as unknown as RawConfig,
1047
+ undefined
1048
+ );
1049
+
1050
+ expect(config).toEqual(
1051
+ expect.not.objectContaining({ unsafe: expect.anything })
1052
+ );
1053
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1054
+ "Processing wrangler configuration:
1055
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1056
+ `);
1057
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1058
+ "Processing wrangler configuration:
1059
+ - The field \\"unsafe\\" is missing the required \\"bindings\\" property."
1060
+ `);
1061
+ });
1062
+
1063
+ it("should error if unsafe.bindings is an object", () => {
1064
+ const { config, diagnostics } = normalizeAndValidateConfig(
1065
+ { unsafe: { bindings: {} } } as unknown as RawConfig,
1066
+ undefined
1067
+ );
1068
+
1069
+ expect(config).toEqual(
1070
+ expect.not.objectContaining({
1071
+ unsafe: { bindings: expect.anything },
1072
+ })
1073
+ );
1074
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1075
+ "Processing wrangler configuration:
1076
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1077
+ `);
1078
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1079
+ "Processing wrangler configuration:
1080
+ - The field \\"unsafe.bindings\\" should be an array but got {}."
1081
+ `);
1082
+ });
1083
+
1084
+ it("should error if unsafe.bindings is a string", () => {
1085
+ const { config, diagnostics } = normalizeAndValidateConfig(
1086
+ { unsafe: { bindings: "BAD" } } as unknown as RawConfig,
1087
+ undefined
1088
+ );
1089
+
1090
+ expect(config).toEqual(
1091
+ expect.not.objectContaining({
1092
+ unsafe: { bindings: expect.anything },
1093
+ })
1094
+ );
1095
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1096
+ "Processing wrangler configuration:
1097
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1098
+ `);
1099
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1100
+ "Processing wrangler configuration:
1101
+ - The field \\"unsafe.bindings\\" should be an array but got \\"BAD\\"."
1102
+ `);
1103
+ });
1104
+
1105
+ it("should error if unsafe.bindings is a number", () => {
1106
+ const { config, diagnostics } = normalizeAndValidateConfig(
1107
+ { unsafe: { bindings: 999 } } as unknown as RawConfig,
1108
+ undefined
1109
+ );
1110
+
1111
+ expect(config).toEqual(
1112
+ expect.not.objectContaining({
1113
+ unsafe: { bindings: expect.anything },
1114
+ })
1115
+ );
1116
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1117
+ "Processing wrangler configuration:
1118
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1119
+ `);
1120
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1121
+ "Processing wrangler configuration:
1122
+ - The field \\"unsafe.bindings\\" should be an array but got 999."
1123
+ `);
1124
+ });
1125
+
1126
+ it("should error if unsafe.bindings is null", () => {
1127
+ const { config, diagnostics } = normalizeAndValidateConfig(
1128
+ { unsafe: { bindings: null } } as unknown as RawConfig,
1129
+ undefined
1130
+ );
1131
+
1132
+ expect(config).toEqual(
1133
+ expect.not.objectContaining({
1134
+ unsafe: { bindings: expect.anything },
1135
+ })
1136
+ );
1137
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1138
+ "Processing wrangler configuration:
1139
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1140
+ `);
1141
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1142
+ "Processing wrangler configuration:
1143
+ - The field \\"unsafe.bindings\\" should be an array but got null."
1144
+ `);
1145
+ });
1146
+
1147
+ it("should error if durable_objects.bindings are not valid", () => {
1148
+ const { config, diagnostics } = normalizeAndValidateConfig(
1149
+ {
1150
+ unsafe: {
1151
+ bindings: [
1152
+ {},
1153
+ { name: "UNSAFE_BINDING_1" },
1154
+ { name: 2666, type: 2777 },
1155
+ {
1156
+ name: "UNSAFE_BINDING_2",
1157
+ type: "UNSAFE_TYPE_2",
1158
+ extra: 2888,
1159
+ },
1160
+ ],
1161
+ },
1162
+ } as unknown as RawConfig,
1163
+ undefined
1164
+ );
1165
+
1166
+ expect(config).toEqual(
1167
+ expect.not.objectContaining({
1168
+ durable_objects: { bindings: expect.anything },
1169
+ })
1170
+ );
1171
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1172
+ "Processing wrangler configuration:
1173
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1174
+ `);
1175
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1176
+ "Processing wrangler configuration:
1177
+
1178
+ - \\"unsafe.bindings[0]\\": {}
1179
+ - ERROR: should have a string \\"name\\" field.
1180
+ - ERROR: should have a string \\"type\\" field.
1181
+
1182
+ - \\"unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
1183
+ - ERROR: should have a string \\"type\\" field.
1184
+
1185
+ - \\"unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
1186
+ - ERROR: should have a string \\"name\\" field.
1187
+ - ERROR: should have a string \\"type\\" field."
1188
+ `);
1189
+ });
1190
+ });
1191
+
1192
+ describe("(deprecated)", () => {
1193
+ it("should remove and warn about deprecated properties", () => {
1194
+ const rawConfig: RawConfig = {
1195
+ zone_id: "ZONE_ID",
1196
+ experimental_services: [
1197
+ {
1198
+ name: "NAME",
1199
+ service: "SERVICE",
1200
+ environment: "ENV",
1201
+ },
1202
+ ],
1203
+ };
1204
+
1205
+ const { config, diagnostics } = normalizeAndValidateConfig(
1206
+ rawConfig,
1207
+ undefined
1208
+ );
1209
+
1210
+ expect("experimental_services" in config).toBe(false);
1211
+ // Zone is not removed yet, since `route` commands might use it
1212
+ expect(config.zone_id).toEqual("ZONE_ID");
1213
+ expect(diagnostics.hasErrors()).toBe(false);
1214
+ expect(diagnostics.hasWarnings()).toBe(true);
1215
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1216
+ "Processing wrangler configuration:
1217
+ - DEPRECATION: \\"zone_id\\":
1218
+ This is unnecessary since we can deduce this from routes directly.
1219
+ - DEPRECATION: \\"experimental_services\\":
1220
+ The \\"experimental_services\\" field is no longer supported. Instead, use [[unsafe.bindings]] to enable experimental features. Add this to your wrangler.toml:
1221
+ \`\`\`
1222
+ [[unsafe.bindings]]
1223
+ name = \\"NAME\\"
1224
+ type = \\"service\\"
1225
+ service = \\"SERVICE\\"
1226
+ environment = \\"ENV\\"
1227
+ \`\`\`"
1228
+ `);
1229
+ });
1230
+ });
1231
+ });
1232
+
1233
+ describe("named environments", () => {
1234
+ it("should use top-level values for inheritable config fields", () => {
1235
+ const expectedConfig: RawConfig = {
1236
+ name: "NAME",
1237
+ account_id: "ACCOUNT_ID",
1238
+ compatibility_date: "2022-01-01",
1239
+ compatibility_flags: ["FLAG1", "FLAG2"],
1240
+ workers_dev: false,
1241
+ routes: ["ROUTE_1", "ROUTE_2"],
1242
+ route: "ROUTE_3",
1243
+ jsx_factory: "JSX_FACTORY",
1244
+ jsx_fragment: "JSX_FRAGMENT",
1245
+ triggers: { crons: ["CRON_1", "CRON_2"] },
1246
+ usage_model: "bundled",
1247
+ };
1248
+
1249
+ const { config, diagnostics } = normalizeAndValidateConfig(
1250
+ { ...expectedConfig, env: { DEV: {} } },
1251
+ undefined
1252
+ );
1253
+
1254
+ expect(config.env.DEV).toEqual(expect.objectContaining(expectedConfig));
1255
+ expect(diagnostics.hasErrors()).toBe(false);
1256
+ expect(diagnostics.hasWarnings()).toBe(false);
1257
+ });
1258
+
1259
+ it("should override top-level values for inheritable config fields", () => {
1260
+ const environment: RawEnvironment = {
1261
+ name: "ENV_NAME",
1262
+ account_id: "ENV_ACCOUNT_ID",
1263
+ compatibility_date: "2022-02-02",
1264
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
1265
+ workers_dev: true,
1266
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
1267
+ route: "ENV_ROUTE_3",
1268
+ jsx_factory: "ENV_JSX_FACTORY",
1269
+ jsx_fragment: "ENV_JSX_FRAGMENT",
1270
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
1271
+ usage_model: "unbound",
1272
+ };
1273
+ const expectedConfig: RawConfig = {
1274
+ name: "NAME",
1275
+ account_id: "ACCOUNT_ID",
1276
+ compatibility_date: "2022-01-01",
1277
+ compatibility_flags: ["FLAG1", "FLAG2"],
1278
+ workers_dev: false,
1279
+ routes: ["ROUTE_1", "ROUTE_2"],
1280
+ route: "ROUTE_3",
1281
+ jsx_factory: "JSX_FACTORY",
1282
+ jsx_fragment: "JSX_FRAGMENT",
1283
+ triggers: { crons: ["CRON_1", "CRON_2"] },
1284
+ usage_model: "bundled",
1285
+ env: {
1286
+ ENV1: environment,
1287
+ },
1288
+ };
1289
+
1290
+ const { config, diagnostics } = normalizeAndValidateConfig(
1291
+ expectedConfig,
1292
+ undefined
1293
+ );
1294
+
1295
+ expect(config.env.ENV1).toEqual(expect.objectContaining(environment));
1296
+ expect(diagnostics.hasErrors()).toBe(false);
1297
+ expect(diagnostics.hasWarnings()).toBe(false);
1298
+ });
1299
+
1300
+ it("should warn for non-inherited fields that are missing in environments", () => {
1301
+ const vars: RawConfig["vars"] = {
1302
+ FOO: "foo",
1303
+ };
1304
+ const durable_objects: RawConfig["durable_objects"] = {
1305
+ bindings: [],
1306
+ };
1307
+ const kv_namespaces: RawConfig["kv_namespaces"] = [];
1308
+ const r2_buckets: RawConfig["r2_buckets"] = [];
1309
+ const unsafe: RawConfig["unsafe"] = { bindings: [] };
1310
+ const rawConfig: RawConfig = {
1311
+ vars,
1312
+ durable_objects,
1313
+ kv_namespaces,
1314
+ r2_buckets,
1315
+ unsafe,
1316
+ env: {
1317
+ ENV1: {},
1318
+ },
1319
+ };
1320
+
1321
+ const { config, diagnostics } = normalizeAndValidateConfig(
1322
+ rawConfig,
1323
+ undefined
1324
+ );
1325
+
1326
+ expect(config.env.ENV1).toEqual(
1327
+ expect.not.objectContaining({
1328
+ vars,
1329
+ durable_objects,
1330
+ kv_namespaces,
1331
+ r2_buckets,
1332
+ unsafe,
1333
+ })
1334
+ );
1335
+ expect(diagnostics.hasErrors()).toBe(false);
1336
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1337
+ "Processing wrangler configuration:
1338
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
1339
+ - \\"env.ENV1\\" environment configuration
1340
+ - \\"vars\\" exists at the top level, but not on \\"env.ENV1\\".
1341
+ This is not what you probably want, since \\"vars\\" is not inherited by environments.
1342
+ Please add \\"vars\\" to \\"env.ENV1\\".
1343
+ - \\"durable_objects\\" exists at the top level, but not on \\"env.ENV1\\".
1344
+ This is not what you probably want, since \\"durable_objects\\" is not inherited by environments.
1345
+ Please add \\"durable_objects\\" to \\"env.ENV1\\".
1346
+ - \\"kv_namespaces\\" exists at the top level, but not on \\"env.ENV1\\".
1347
+ This is not what you probably want, since \\"kv_namespaces\\" is not inherited by environments.
1348
+ Please add \\"kv_namespaces\\" to \\"env.ENV1\\".
1349
+ - \\"r2_buckets\\" exists at the top level, but not on \\"env.ENV1\\".
1350
+ This is not what you probably want, since \\"r2_buckets\\" is not inherited by environments.
1351
+ Please add \\"r2_buckets\\" to \\"env.ENV1\\".
1352
+ - \\"unsafe\\" exists at the top level, but not on \\"env.ENV1\\".
1353
+ This is not what you probably want, since \\"unsafe\\" is not inherited by environments.
1354
+ Please add \\"unsafe\\" to \\"env.ENV1\\"."
1355
+ `);
1356
+ });
1357
+
1358
+ it("should error on invalid environment values", () => {
1359
+ const expectedConfig: RawEnvironment = {
1360
+ name: 111,
1361
+ account_id: 222,
1362
+ compatibility_date: 333,
1363
+ compatibility_flags: [444, 555],
1364
+ workers_dev: "BAD",
1365
+ routes: [666, 777],
1366
+ route: 888,
1367
+ jsx_factory: 999,
1368
+ jsx_fragment: 1000,
1369
+ triggers: { crons: [1111, 1222] },
1370
+ usage_model: "INVALID",
1371
+ } as unknown as RawEnvironment;
1372
+
1373
+ const { config, diagnostics } = normalizeAndValidateConfig(
1374
+ { env: { ENV1: expectedConfig } },
1375
+ undefined
1376
+ );
1377
+
1378
+ expect(config.env.ENV1).toEqual(expect.objectContaining(expectedConfig));
1379
+ expect(diagnostics.hasWarnings()).toBe(false);
1380
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1381
+ "Processing wrangler configuration:
1382
+
1383
+ - \\"env.ENV1\\" environment configuration
1384
+ - Expected \\"route\\" field to be a string but got 888.
1385
+ - Expected \\"routes\\" field to be an array of strings but got [666,777].
1386
+ - Expected \\"workers_dev\\" field to be a boolean but got \\"BAD\\".
1387
+ - Expected \\"account_id\\" field to be a string but got 222.
1388
+ - Expected \\"compatibility_date\\" field to be a string but got 333.
1389
+ - Expected \\"compatibility_flags\\" field to be an array of strings but got [444,555].
1390
+ - Expected \\"jsx_factory\\" field to be a string but got 999.
1391
+ - Expected \\"jsx_fragment\\" field to be a string but got 1000.
1392
+ - Expected \\"name\\" field to be a string but got 111.
1393
+ - Expected \\"usage_model\\" field to be on of bundled,unbound but got \\"INVALID\\"."
1394
+ `);
1395
+ });
1396
+
1397
+ describe("durable_objects field", () => {
1398
+ it("should error if durable_objects is an array", () => {
1399
+ const { config, diagnostics } = normalizeAndValidateConfig(
1400
+ { env: { ENV1: { durable_objects: [] } } } as unknown as RawConfig,
1401
+ undefined
1402
+ );
1403
+
1404
+ expect(config.env.ENV1).toEqual(
1405
+ expect.not.objectContaining({ durable_objects: expect.anything })
1406
+ );
1407
+ expect(diagnostics.hasWarnings()).toBe(false);
1408
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1409
+ "Processing wrangler configuration:
1410
+
1411
+ - \\"env.ENV1\\" environment configuration
1412
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got []."
1413
+ `);
1414
+ });
1415
+
1416
+ it("should error if durable_objects is a string", () => {
1417
+ const { config, diagnostics } = normalizeAndValidateConfig(
1418
+ { env: { ENV1: { durable_objects: "BAD" } } } as unknown as RawConfig,
1419
+ undefined
1420
+ );
1421
+
1422
+ expect(config.env.ENV1).toEqual(
1423
+ expect.not.objectContaining({ durable_objects: expect.anything })
1424
+ );
1425
+ expect(diagnostics.hasWarnings()).toBe(false);
1426
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1427
+ "Processing wrangler configuration:
1428
+
1429
+ - \\"env.ENV1\\" environment configuration
1430
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got \\"BAD\\"."
1431
+ `);
1432
+ });
1433
+
1434
+ it("should error if durable_objects is a number", () => {
1435
+ const { config, diagnostics } = normalizeAndValidateConfig(
1436
+ { env: { ENV1: { durable_objects: 999 } } } as unknown as RawConfig,
1437
+ undefined
1438
+ );
1439
+
1440
+ expect(config.env.ENV1).toEqual(
1441
+ expect.not.objectContaining({ durable_objects: expect.anything })
1442
+ );
1443
+ expect(diagnostics.hasWarnings()).toBe(false);
1444
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1445
+ "Processing wrangler configuration:
1446
+
1447
+ - \\"env.ENV1\\" environment configuration
1448
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got 999."
1449
+ `);
1450
+ });
1451
+
1452
+ it("should error if durable_objects is null", () => {
1453
+ const { config, diagnostics } = normalizeAndValidateConfig(
1454
+ { env: { ENV1: { durable_objects: null } } } as unknown as RawConfig,
1455
+ undefined
1456
+ );
1457
+
1458
+ expect(config.env.ENV1).toEqual(
1459
+ expect.not.objectContaining({ durable_objects: expect.anything })
1460
+ );
1461
+ expect(diagnostics.hasWarnings()).toBe(false);
1462
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1463
+ "Processing wrangler configuration:
1464
+
1465
+ - \\"env.ENV1\\" environment configuration
1466
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got null."
1467
+ `);
1468
+ });
1469
+
1470
+ it("should error if durable_objects.bindings is not defined", () => {
1471
+ const { config, diagnostics } = normalizeAndValidateConfig(
1472
+ { env: { ENV1: { durable_objects: {} } } } as unknown as RawConfig,
1473
+ undefined
1474
+ );
1475
+
1476
+ expect(config.env.ENV1).toEqual(
1477
+ expect.not.objectContaining({ durable_objects: expect.anything })
1478
+ );
1479
+ expect(diagnostics.hasWarnings()).toBe(false);
1480
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1481
+ "Processing wrangler configuration:
1482
+
1483
+ - \\"env.ENV1\\" environment configuration
1484
+ - The field \\"env.ENV1.durable_objects\\" is missing the required \\"bindings\\" property."
1485
+ `);
1486
+ });
1487
+
1488
+ it("should error if durable_objects.bindings is an object", () => {
1489
+ const { config, diagnostics } = normalizeAndValidateConfig(
1490
+ {
1491
+ env: { ENV1: { durable_objects: { bindings: {} } } },
1492
+ } as unknown as RawConfig,
1493
+ undefined
1494
+ );
1495
+
1496
+ expect(config.env.ENV1).toEqual(
1497
+ expect.not.objectContaining({
1498
+ durable_objects: { bindings: expect.anything },
1499
+ })
1500
+ );
1501
+ expect(diagnostics.hasWarnings()).toBe(false);
1502
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1503
+ "Processing wrangler configuration:
1504
+
1505
+ - \\"env.ENV1\\" environment configuration
1506
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got {}."
1507
+ `);
1508
+ });
1509
+
1510
+ it("should error if durable_objects.bindings is a string", () => {
1511
+ const { config, diagnostics } = normalizeAndValidateConfig(
1512
+ {
1513
+ env: { ENV1: { durable_objects: { bindings: "BAD" } } },
1514
+ } as unknown as RawConfig,
1515
+ undefined
1516
+ );
1517
+
1518
+ expect(config.env.ENV1).toEqual(
1519
+ expect.not.objectContaining({
1520
+ durable_objects: { bindings: expect.anything },
1521
+ })
1522
+ );
1523
+ expect(diagnostics.hasWarnings()).toBe(false);
1524
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1525
+ "Processing wrangler configuration:
1526
+
1527
+ - \\"env.ENV1\\" environment configuration
1528
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got \\"BAD\\"."
1529
+ `);
1530
+ });
1531
+
1532
+ it("should error if durable_objects.bindings is a number", () => {
1533
+ const { config, diagnostics } = normalizeAndValidateConfig(
1534
+ {
1535
+ env: { ENV1: { durable_objects: { bindings: 999 } } },
1536
+ } as unknown as RawConfig,
1537
+ undefined
1538
+ );
1539
+
1540
+ expect(config.env.ENV1).toEqual(
1541
+ expect.not.objectContaining({
1542
+ durable_objects: { bindings: expect.anything },
1543
+ })
1544
+ );
1545
+ expect(diagnostics.hasWarnings()).toBe(false);
1546
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1547
+ "Processing wrangler configuration:
1548
+
1549
+ - \\"env.ENV1\\" environment configuration
1550
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got 999."
1551
+ `);
1552
+ });
1553
+
1554
+ it("should error if durable_objects.bindings is null", () => {
1555
+ const { config, diagnostics } = normalizeAndValidateConfig(
1556
+ {
1557
+ env: { ENV1: { durable_objects: { bindings: null } } },
1558
+ } as unknown as RawConfig,
1559
+ undefined
1560
+ );
1561
+
1562
+ expect(config.env.ENV1).toEqual(
1563
+ expect.not.objectContaining({
1564
+ durable_objects: { bindings: expect.anything },
1565
+ })
1566
+ );
1567
+ expect(diagnostics.hasWarnings()).toBe(false);
1568
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1569
+ "Processing wrangler configuration:
1570
+
1571
+ - \\"env.ENV1\\" environment configuration
1572
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got null."
1573
+ `);
1574
+ });
1575
+
1576
+ it("should error if durable_objects.bindings are not valid", () => {
1577
+ const { config, diagnostics } = normalizeAndValidateConfig(
1578
+ {
1579
+ env: {
1580
+ ENV1: {
1581
+ durable_objects: {
1582
+ bindings: [
1583
+ {},
1584
+ { name: "VALID" },
1585
+ { name: 1555, class_name: 1666 },
1586
+ {
1587
+ name: 1777,
1588
+ class_name: 1888,
1589
+ script_name: 1999,
1590
+ },
1591
+ ],
1592
+ },
1593
+ },
1594
+ },
1595
+ } as unknown as RawConfig,
1596
+ undefined
1597
+ );
1598
+
1599
+ expect(config.env.ENV1).toEqual(
1600
+ expect.not.objectContaining({
1601
+ durable_objects: { bindings: expect.anything },
1602
+ })
1603
+ );
1604
+ expect(diagnostics.hasWarnings()).toBe(false);
1605
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1606
+ "Processing wrangler configuration:
1607
+
1608
+ - \\"env.ENV1\\" environment configuration
1609
+
1610
+ - \\"env.ENV1.durable_objects.bindings[0]\\": {}
1611
+ - ERROR: bindings should have a string \\"name\\" field.
1612
+ - ERROR: should have a string \\"class_name\\" field.
1613
+
1614
+ - \\"env.ENV1.durable_objects.bindings[1]\\": {\\"name\\":\\"VALID\\"}
1615
+ - ERROR: should have a string \\"class_name\\" field.
1616
+
1617
+ - \\"env.ENV1.durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
1618
+ - ERROR: bindings should have a string \\"name\\" field.
1619
+ - ERROR: should have a string \\"class_name\\" field.
1620
+
1621
+ - \\"env.ENV1.durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
1622
+ - ERROR: bindings should have a string \\"name\\" field.
1623
+ - ERROR: should have a string \\"class_name\\" field.
1624
+ - ERROR: should, optionally, have a string \\"script_name\\" field."
1625
+ `);
1626
+ });
1627
+ });
1628
+
1629
+ describe("kv_namespaces field", () => {
1630
+ it("should error if kv_namespaces is an object", () => {
1631
+ const { config, diagnostics } = normalizeAndValidateConfig(
1632
+ { env: { ENV1: { kv_namespaces: {} } } } as unknown as RawConfig,
1633
+ undefined
1634
+ );
1635
+
1636
+ expect(config.env.ENV1).toEqual(
1637
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1638
+ );
1639
+ expect(diagnostics.hasWarnings()).toBe(false);
1640
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1641
+ "Processing wrangler configuration:
1642
+
1643
+ - \\"env.ENV1\\" environment configuration
1644
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got {}."
1645
+ `);
1646
+ });
1647
+
1648
+ it("should error if kv_namespaces is a string", () => {
1649
+ const { config, diagnostics } = normalizeAndValidateConfig(
1650
+ { env: { ENV1: { kv_namespaces: "BAD" } } } as unknown as RawConfig,
1651
+ undefined
1652
+ );
1653
+
1654
+ expect(config.env.ENV1).toEqual(
1655
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1656
+ );
1657
+ expect(diagnostics.hasWarnings()).toBe(false);
1658
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1659
+ "Processing wrangler configuration:
1660
+
1661
+ - \\"env.ENV1\\" environment configuration
1662
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got \\"BAD\\"."
1663
+ `);
1664
+ });
1665
+
1666
+ it("should error if kv_namespaces is a number", () => {
1667
+ const { config, diagnostics } = normalizeAndValidateConfig(
1668
+ { env: { ENV1: { kv_namespaces: 999 } } } as unknown as RawConfig,
1669
+ undefined
1670
+ );
1671
+
1672
+ expect(config.env.ENV1).toEqual(
1673
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1674
+ );
1675
+ expect(diagnostics.hasWarnings()).toBe(false);
1676
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1677
+ "Processing wrangler configuration:
1678
+
1679
+ - \\"env.ENV1\\" environment configuration
1680
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got 999."
1681
+ `);
1682
+ });
1683
+
1684
+ it("should error if kv_namespaces is null", () => {
1685
+ const { config, diagnostics } = normalizeAndValidateConfig(
1686
+ { env: { ENV1: { kv_namespaces: null } } } as unknown as RawConfig,
1687
+ undefined
1688
+ );
1689
+
1690
+ expect(config.env.ENV1).toEqual(
1691
+ expect.not.objectContaining({ kv_namespaces: expect.anything })
1692
+ );
1693
+ expect(diagnostics.hasWarnings()).toBe(false);
1694
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1695
+ "Processing wrangler configuration:
1696
+
1697
+ - \\"env.ENV1\\" environment configuration
1698
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got null."
1699
+ `);
1700
+ });
1701
+
1702
+ it("should error if kv_namespaces.bindings are not valid", () => {
1703
+ const { config, diagnostics } = normalizeAndValidateConfig(
1704
+ {
1705
+ env: {
1706
+ ENV1: {
1707
+ kv_namespaces: [
1708
+ {},
1709
+ { binding: "VALID" },
1710
+ { binding: 2000, id: 2111 },
1711
+ {
1712
+ binding: "KV_BINDING_2",
1713
+ id: "KV_ID_2",
1714
+ preview_id: 2222,
1715
+ },
1716
+ ],
1717
+ },
1718
+ },
1719
+ } as unknown as RawConfig,
1720
+ undefined
1721
+ );
1722
+
1723
+ expect(config.env.ENV1).toEqual(
1724
+ expect.not.objectContaining({
1725
+ kv_namespaces: { bindings: expect.anything },
1726
+ })
1727
+ );
1728
+ expect(diagnostics.hasWarnings()).toBe(false);
1729
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1730
+ "Processing wrangler configuration:
1731
+
1732
+ - \\"env.ENV1\\" environment configuration
1733
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
1734
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
1735
+ - \\"env.ENV1.kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
1736
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1737
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1738
+ - \\"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}."
1739
+ `);
1740
+ });
1741
+ });
1742
+
1743
+ describe("r2_buckets field", () => {
1744
+ it("should error if r2_buckets is an object", () => {
1745
+ const { config, diagnostics } = normalizeAndValidateConfig(
1746
+ { env: { ENV1: { r2_buckets: {} } } } as unknown as RawConfig,
1747
+ undefined
1748
+ );
1749
+
1750
+ expect(config.env.ENV1).toEqual(
1751
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1752
+ );
1753
+ expect(diagnostics.hasWarnings()).toBe(false);
1754
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1755
+ "Processing wrangler configuration:
1756
+
1757
+ - \\"env.ENV1\\" environment configuration
1758
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got {}."
1759
+ `);
1760
+ });
1761
+
1762
+ it("should error if r2_buckets is a string", () => {
1763
+ const { config, diagnostics } = normalizeAndValidateConfig(
1764
+ { env: { ENV1: { r2_buckets: "BAD" } } } as unknown as RawConfig,
1765
+ undefined
1766
+ );
1767
+
1768
+ expect(config.env.ENV1).toEqual(
1769
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1770
+ );
1771
+ expect(diagnostics.hasWarnings()).toBe(false);
1772
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1773
+ "Processing wrangler configuration:
1774
+
1775
+ - \\"env.ENV1\\" environment configuration
1776
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got \\"BAD\\"."
1777
+ `);
1778
+ });
1779
+
1780
+ it("should error if r2_buckets is a number", () => {
1781
+ const { config, diagnostics } = normalizeAndValidateConfig(
1782
+ { env: { ENV1: { r2_buckets: 999 } } } as unknown as RawConfig,
1783
+ undefined
1784
+ );
1785
+
1786
+ expect(config.env.ENV1).toEqual(
1787
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1788
+ );
1789
+ expect(diagnostics.hasWarnings()).toBe(false);
1790
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1791
+ "Processing wrangler configuration:
1792
+
1793
+ - \\"env.ENV1\\" environment configuration
1794
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got 999."
1795
+ `);
1796
+ });
1797
+
1798
+ it("should error if r2_buckets is null", () => {
1799
+ const { config, diagnostics } = normalizeAndValidateConfig(
1800
+ { env: { ENV1: { r2_buckets: null } } } as unknown as RawConfig,
1801
+ undefined
1802
+ );
1803
+
1804
+ expect(config.env.ENV1).toEqual(
1805
+ expect.not.objectContaining({ r2_buckets: expect.anything })
1806
+ );
1807
+ expect(diagnostics.hasWarnings()).toBe(false);
1808
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1809
+ "Processing wrangler configuration:
1810
+
1811
+ - \\"env.ENV1\\" environment configuration
1812
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got null."
1813
+ `);
1814
+ });
1815
+
1816
+ it("should error if r2_buckets.bindings are not valid", () => {
1817
+ const { config, diagnostics } = normalizeAndValidateConfig(
1818
+ {
1819
+ env: {
1820
+ ENV1: {
1821
+ r2_buckets: [
1822
+ {},
1823
+ { binding: "R2_BINDING_1" },
1824
+ { binding: 2333, bucket_name: 2444 },
1825
+ {
1826
+ binding: "R2_BINDING_2",
1827
+ bucket_name: "R2_BUCKET_2",
1828
+ preview_bucket_name: 2555,
1829
+ },
1830
+ ],
1831
+ },
1832
+ },
1833
+ } as unknown as RawConfig,
1834
+ undefined
1835
+ );
1836
+
1837
+ expect(config.env.ENV1).toEqual(
1838
+ expect.not.objectContaining({
1839
+ r2_buckets: { bindings: expect.anything },
1840
+ })
1841
+ );
1842
+ expect(diagnostics.hasWarnings()).toBe(false);
1843
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1844
+ "Processing wrangler configuration:
1845
+
1846
+ - \\"env.ENV1\\" environment configuration
1847
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
1848
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
1849
+ - \\"env.ENV1.r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
1850
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1851
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1852
+ - \\"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}."
1853
+ `);
1854
+ });
1855
+ });
1856
+
1857
+ describe("unsafe field", () => {
1858
+ it("should error if unsafe is an array", () => {
1859
+ const { config, diagnostics } = normalizeAndValidateConfig(
1860
+ { env: { ENV1: { unsafe: [] } } } as unknown as RawConfig,
1861
+ undefined
1862
+ );
1863
+
1864
+ expect(config.env.ENV1).toEqual(
1865
+ expect.not.objectContaining({ unsafe: expect.anything })
1866
+ );
1867
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1868
+ "Processing wrangler configuration:
1869
+
1870
+ - \\"env.ENV1\\" environment configuration
1871
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1872
+ `);
1873
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1874
+ "Processing wrangler configuration:
1875
+
1876
+ - \\"env.ENV1\\" environment configuration
1877
+ - The field \\"env.ENV1.unsafe\\" should be an object but got []."
1878
+ `);
1879
+ });
1880
+
1881
+ it("should error if unsafe is a string", () => {
1882
+ const { config, diagnostics } = normalizeAndValidateConfig(
1883
+ { env: { ENV1: { unsafe: "BAD" } } } as unknown as RawConfig,
1884
+ undefined
1885
+ );
1886
+
1887
+ expect(config.env.ENV1).toEqual(
1888
+ expect.not.objectContaining({ unsafe: expect.anything })
1889
+ );
1890
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1891
+ "Processing wrangler configuration:
1892
+
1893
+ - \\"env.ENV1\\" environment configuration
1894
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1895
+ `);
1896
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1897
+ "Processing wrangler configuration:
1898
+
1899
+ - \\"env.ENV1\\" environment configuration
1900
+ - The field \\"env.ENV1.unsafe\\" should be an object but got \\"BAD\\"."
1901
+ `);
1902
+ });
1903
+
1904
+ it("should error if unsafe is a number", () => {
1905
+ const { config, diagnostics } = normalizeAndValidateConfig(
1906
+ { env: { ENV1: { unsafe: 999 } } } as unknown as RawConfig,
1907
+ undefined
1908
+ );
1909
+
1910
+ expect(config.env.ENV1).toEqual(
1911
+ expect.not.objectContaining({ unsafe: expect.anything })
1912
+ );
1913
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1914
+ "Processing wrangler configuration:
1915
+
1916
+ - \\"env.ENV1\\" environment configuration
1917
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1918
+ `);
1919
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1920
+ "Processing wrangler configuration:
1921
+
1922
+ - \\"env.ENV1\\" environment configuration
1923
+ - The field \\"env.ENV1.unsafe\\" should be an object but got 999."
1924
+ `);
1925
+ });
1926
+
1927
+ it("should error if unsafe is null", () => {
1928
+ const { config, diagnostics } = normalizeAndValidateConfig(
1929
+ { env: { ENV1: { unsafe: null } } } as unknown as RawConfig,
1930
+ undefined
1931
+ );
1932
+
1933
+ expect(config.env.ENV1).toEqual(
1934
+ expect.not.objectContaining({ unsafe: expect.anything })
1935
+ );
1936
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1937
+ "Processing wrangler configuration:
1938
+
1939
+ - \\"env.ENV1\\" environment configuration
1940
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1941
+ `);
1942
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1943
+ "Processing wrangler configuration:
1944
+
1945
+ - \\"env.ENV1\\" environment configuration
1946
+ - The field \\"env.ENV1.unsafe\\" should be an object but got null."
1947
+ `);
1948
+ });
1949
+
1950
+ it("should error if unsafe.bindings is not defined", () => {
1951
+ const { config, diagnostics } = normalizeAndValidateConfig(
1952
+ { env: { ENV1: { unsafe: {} } } } as unknown as RawConfig,
1953
+ undefined
1954
+ );
1955
+
1956
+ expect(config.env.ENV1).toEqual(
1957
+ expect.not.objectContaining({ unsafe: expect.anything })
1958
+ );
1959
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1960
+ "Processing wrangler configuration:
1961
+
1962
+ - \\"env.ENV1\\" environment configuration
1963
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1964
+ `);
1965
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1966
+ "Processing wrangler configuration:
1967
+
1968
+ - \\"env.ENV1\\" environment configuration
1969
+ - The field \\"env.ENV1.unsafe\\" is missing the required \\"bindings\\" property."
1970
+ `);
1971
+ });
1972
+
1973
+ it("should error if unsafe.bindings is an object", () => {
1974
+ const { config, diagnostics } = normalizeAndValidateConfig(
1975
+ {
1976
+ env: { ENV1: { unsafe: { bindings: {} } } },
1977
+ } as unknown as RawConfig,
1978
+ undefined
1979
+ );
1980
+
1981
+ expect(config.env.ENV1).toEqual(
1982
+ expect.not.objectContaining({
1983
+ unsafe: { bindings: expect.anything },
1984
+ })
1985
+ );
1986
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1987
+ "Processing wrangler configuration:
1988
+
1989
+ - \\"env.ENV1\\" environment configuration
1990
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1991
+ `);
1992
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1993
+ "Processing wrangler configuration:
1994
+
1995
+ - \\"env.ENV1\\" environment configuration
1996
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got {}."
1997
+ `);
1998
+ });
1999
+
2000
+ it("should error if unsafe.bindings is a string", () => {
2001
+ const { config, diagnostics } = normalizeAndValidateConfig(
2002
+ {
2003
+ env: { ENV1: { unsafe: { bindings: "BAD" } } },
2004
+ } as unknown as RawConfig,
2005
+ undefined
2006
+ );
2007
+
2008
+ expect(config.env.ENV1).toEqual(
2009
+ expect.not.objectContaining({
2010
+ unsafe: { bindings: expect.anything },
2011
+ })
2012
+ );
2013
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2014
+ "Processing wrangler configuration:
2015
+
2016
+ - \\"env.ENV1\\" environment configuration
2017
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2018
+ `);
2019
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2020
+ "Processing wrangler configuration:
2021
+
2022
+ - \\"env.ENV1\\" environment configuration
2023
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got \\"BAD\\"."
2024
+ `);
2025
+ });
2026
+
2027
+ it("should error if unsafe.bindings is a number", () => {
2028
+ const { config, diagnostics } = normalizeAndValidateConfig(
2029
+ {
2030
+ env: { ENV1: { unsafe: { bindings: 999 } } },
2031
+ } as unknown as RawConfig,
2032
+ undefined
2033
+ );
2034
+
2035
+ expect(config.env.ENV1).toEqual(
2036
+ expect.not.objectContaining({
2037
+ unsafe: { bindings: expect.anything },
2038
+ })
2039
+ );
2040
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2041
+ "Processing wrangler configuration:
2042
+
2043
+ - \\"env.ENV1\\" environment configuration
2044
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2045
+ `);
2046
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2047
+ "Processing wrangler configuration:
2048
+
2049
+ - \\"env.ENV1\\" environment configuration
2050
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got 999."
2051
+ `);
2052
+ });
2053
+
2054
+ it("should error if unsafe.bindings is null", () => {
2055
+ const { config, diagnostics } = normalizeAndValidateConfig(
2056
+ {
2057
+ env: { ENV1: { unsafe: { bindings: null } } },
2058
+ } as unknown as RawConfig,
2059
+ undefined
2060
+ );
2061
+
2062
+ expect(config.env.ENV1).toEqual(
2063
+ expect.not.objectContaining({
2064
+ unsafe: { bindings: expect.anything },
2065
+ })
2066
+ );
2067
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2068
+ "Processing wrangler configuration:
2069
+
2070
+ - \\"env.ENV1\\" environment configuration
2071
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2072
+ `);
2073
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2074
+ "Processing wrangler configuration:
2075
+
2076
+ - \\"env.ENV1\\" environment configuration
2077
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got null."
2078
+ `);
2079
+ });
2080
+
2081
+ it("should error if unsafe.bindings are not valid", () => {
2082
+ const { config, diagnostics } = normalizeAndValidateConfig(
2083
+ {
2084
+ env: {
2085
+ ENV1: {
2086
+ unsafe: {
2087
+ bindings: [
2088
+ {},
2089
+ { name: "UNSAFE_BINDING_1" },
2090
+ { name: 2666, type: 2777 },
2091
+ {
2092
+ name: "UNSAFE_BINDING_2",
2093
+ type: "UNSAFE_TYPE_2",
2094
+ extra: 2888,
2095
+ },
2096
+ ],
2097
+ },
2098
+ },
2099
+ },
2100
+ } as unknown as RawConfig,
2101
+ undefined
2102
+ );
2103
+
2104
+ expect(config.env.ENV1).toEqual(
2105
+ expect.not.objectContaining({
2106
+ durable_objects: { bindings: expect.anything },
2107
+ })
2108
+ );
2109
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2110
+ "Processing wrangler configuration:
2111
+
2112
+ - \\"env.ENV1\\" environment configuration
2113
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2114
+ `);
2115
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2116
+ "Processing wrangler configuration:
2117
+
2118
+ - \\"env.ENV1\\" environment configuration
2119
+
2120
+ - \\"env.ENV1.unsafe.bindings[0]\\": {}
2121
+ - ERROR: should have a string \\"name\\" field.
2122
+ - ERROR: should have a string \\"type\\" field.
2123
+
2124
+ - \\"env.ENV1.unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
2125
+ - ERROR: should have a string \\"type\\" field.
2126
+
2127
+ - \\"env.ENV1.unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
2128
+ - ERROR: should have a string \\"name\\" field.
2129
+ - ERROR: should have a string \\"type\\" field."
2130
+ `);
2131
+ });
2132
+ });
2133
+
2134
+ describe("(deprecated)", () => {
2135
+ it("should remove and warn about deprecated properties", () => {
2136
+ const environment: RawEnvironment = {
2137
+ zone_id: "ZONE_ID",
2138
+ experimental_services: [
2139
+ {
2140
+ name: "NAME",
2141
+ service: "SERVICE",
2142
+ environment: "ENV",
2143
+ },
2144
+ ],
2145
+ };
2146
+
2147
+ const { config, diagnostics } = normalizeAndValidateConfig(
2148
+ {
2149
+ env: {
2150
+ ENV1: environment,
2151
+ },
2152
+ },
2153
+ undefined
2154
+ );
2155
+
2156
+ expect("experimental_services" in config.env.ENV1).toBe(false);
2157
+ // Zone is not removed yet, since `route` commands might use it
2158
+ expect(config.env.ENV1.zone_id).toEqual("ZONE_ID");
2159
+ expect(diagnostics.hasErrors()).toBe(false);
2160
+ expect(diagnostics.hasWarnings()).toBe(true);
2161
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2162
+ "Processing wrangler configuration:
2163
+
2164
+ - \\"env.ENV1\\" environment configuration
2165
+ - DEPRECATION: \\"zone_id\\":
2166
+ This is unnecessary since we can deduce this from routes directly.
2167
+ - DEPRECATION: \\"experimental_services\\":
2168
+ The \\"experimental_services\\" field is no longer supported. Instead, use [[unsafe.bindings]] to enable experimental features. Add this to your wrangler.toml:
2169
+ \`\`\`
2170
+ [[unsafe.bindings]]
2171
+ name = \\"NAME\\"
2172
+ type = \\"service\\"
2173
+ service = \\"SERVICE\\"
2174
+ environment = \\"ENV\\"
2175
+ \`\`\`"
2176
+ `);
2177
+ });
2178
+ });
2179
+ });
2180
+ });
2181
+
2182
+ function normalizePath(text: string): string {
2183
+ return text
2184
+ .replace("project\\wrangler.toml", "project/wrangler.toml")
2185
+ .replace("src\\index.ts", "src/index.ts");
2186
+ }