wrangler 0.0.0-de73fa2 → 0.0.0-e0b9366

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