wrangler 0.0.0-ece06ea → 0.0.0-ed4f2ef

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