wrangler 0.0.0-e1c615f → 0.0.0-e3a458e

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