wrangler 0.0.0-eebdb6c → 0.0.0-eec88346

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 (304) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +160 -28
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6314 -0
  6. package/package.json +190 -122
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +209 -0
  9. package/src/__tests__/api-devregistry.test.js +56 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +3972 -0
  14. package/src/__tests__/d1.test.ts +72 -0
  15. package/src/__tests__/delete.test.ts +230 -0
  16. package/src/__tests__/deployments.test.ts +91 -0
  17. package/src/__tests__/dev.test.tsx +1455 -0
  18. package/src/__tests__/dialogs.test.tsx +40 -0
  19. package/src/__tests__/generate.test.ts +235 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +85 -0
  22. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  23. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  24. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  25. package/src/__tests__/helpers/mock-bin.ts +36 -0
  26. package/src/__tests__/helpers/mock-cfetch.ts +333 -0
  27. package/src/__tests__/helpers/mock-console.ts +106 -0
  28. package/src/__tests__/helpers/mock-dialogs.ts +153 -0
  29. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  30. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  31. package/src/__tests__/helpers/mock-istty.ts +74 -0
  32. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  33. package/src/__tests__/helpers/mock-kv.ts +33 -0
  34. package/src/__tests__/helpers/mock-oauth-flow.ts +252 -0
  35. package/src/__tests__/helpers/mock-process.ts +39 -0
  36. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  37. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  38. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  39. package/src/__tests__/helpers/msw/handlers/deployments.ts +86 -0
  40. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  41. package/src/__tests__/helpers/msw/handlers/oauth.ts +36 -0
  42. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  43. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  44. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  45. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  46. package/src/__tests__/helpers/msw/index.ts +21 -0
  47. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  48. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  49. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  51. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  53. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  54. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  55. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  56. package/src/__tests__/https-options.test.ts +163 -0
  57. package/src/__tests__/index.test.ts +252 -284
  58. package/src/__tests__/init.test.ts +2900 -0
  59. package/src/__tests__/jest.setup.ts +190 -10
  60. package/src/__tests__/kv.test.ts +1463 -886
  61. package/src/__tests__/logger.test.ts +141 -0
  62. package/src/__tests__/metrics.test.ts +450 -0
  63. package/src/__tests__/middleware.scheduled.test.ts +135 -0
  64. package/src/__tests__/middleware.test.ts +726 -0
  65. package/src/__tests__/package-manager.test.ts +353 -0
  66. package/src/__tests__/pages.test.ts +2221 -0
  67. package/src/__tests__/parse.test.ts +330 -0
  68. package/src/__tests__/paths.test.ts +39 -0
  69. package/src/__tests__/publish.test.ts +7282 -0
  70. package/src/__tests__/pubsub.test.ts +367 -0
  71. package/src/__tests__/queues.test.ts +368 -0
  72. package/src/__tests__/r2.test.ts +371 -0
  73. package/src/__tests__/route.test.ts +45 -0
  74. package/src/__tests__/secret.test.ts +644 -0
  75. package/src/__tests__/tail.test.ts +800 -0
  76. package/src/__tests__/test-old-node-version.js +31 -0
  77. package/src/__tests__/type-generation.test.ts +217 -0
  78. package/src/__tests__/user.test.ts +155 -0
  79. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  80. package/src/__tests__/validate-dev-props.test.ts +56 -0
  81. package/src/__tests__/version.test.ts +35 -0
  82. package/src/__tests__/whoami.test.tsx +278 -0
  83. package/src/__tests__/worker-namespace.test.ts +321 -0
  84. package/src/abort.d.ts +3 -0
  85. package/src/api/dev.ts +208 -0
  86. package/src/api/index.ts +2 -0
  87. package/src/bundle-reporter.tsx +29 -0
  88. package/src/bundle.ts +782 -0
  89. package/src/cfetch/index.ts +128 -71
  90. package/src/cfetch/internal.ts +240 -46
  91. package/src/cli.ts +26 -6
  92. package/src/config/README.md +107 -0
  93. package/src/config/config.ts +275 -0
  94. package/src/config/diagnostics.ts +80 -0
  95. package/src/config/environment.ts +549 -0
  96. package/src/config/index.ts +306 -0
  97. package/src/config/validation-helpers.ts +597 -0
  98. package/src/config/validation.ts +2095 -0
  99. package/src/config-cache.ts +85 -0
  100. package/src/create-worker-preview.ts +282 -0
  101. package/src/create-worker-upload-form.ts +314 -0
  102. package/src/d1/backups.tsx +218 -0
  103. package/src/d1/constants.ts +2 -0
  104. package/src/d1/create.tsx +54 -0
  105. package/src/d1/delete.tsx +56 -0
  106. package/src/d1/execute.tsx +334 -0
  107. package/src/d1/formatTimeAgo.ts +14 -0
  108. package/src/d1/index.ts +98 -0
  109. package/src/d1/list.tsx +48 -0
  110. package/src/d1/migrations.tsx +446 -0
  111. package/src/d1/options.ts +22 -0
  112. package/src/d1/types.tsx +24 -0
  113. package/src/d1/utils.ts +48 -0
  114. package/src/delete.ts +111 -0
  115. package/src/deprecated/index.ts +453 -0
  116. package/src/dev/dev-vars.ts +36 -0
  117. package/src/dev/dev.tsx +587 -0
  118. package/src/dev/get-local-persistence-path.tsx +31 -0
  119. package/src/dev/local.tsx +717 -0
  120. package/src/dev/remote.tsx +602 -0
  121. package/src/dev/start-server.ts +516 -0
  122. package/src/dev/use-esbuild.ts +200 -0
  123. package/src/dev/validate-dev-props.ts +31 -0
  124. package/src/dev-registry.tsx +201 -0
  125. package/src/dev.tsx +850 -857
  126. package/src/dialogs.tsx +153 -61
  127. package/src/dispatch-namespace.ts +206 -0
  128. package/src/durable.ts +102 -0
  129. package/src/entry.ts +329 -0
  130. package/src/environment-variables.ts +50 -0
  131. package/src/errors.ts +11 -0
  132. package/src/git-client.ts +124 -0
  133. package/src/global-wrangler-config-path.ts +26 -0
  134. package/src/https-options.ts +127 -0
  135. package/src/index.tsx +663 -1889
  136. package/src/init.ts +969 -0
  137. package/src/inspect.ts +767 -0
  138. package/src/intl-polyfill.d.ts +139 -0
  139. package/src/is-ci.ts +14 -0
  140. package/src/is-interactive.ts +16 -0
  141. package/src/kv/helpers.ts +430 -0
  142. package/src/kv/index.ts +645 -0
  143. package/src/logger.ts +92 -0
  144. package/src/metrics/index.ts +5 -0
  145. package/src/metrics/metrics-config.ts +239 -0
  146. package/src/metrics/metrics-dispatcher.ts +96 -0
  147. package/src/metrics/metrics-usage-headers.ts +24 -0
  148. package/src/metrics/send-event.ts +94 -0
  149. package/src/miniflare-cli/README.md +30 -0
  150. package/src/miniflare-cli/assets.ts +215 -0
  151. package/src/miniflare-cli/index.ts +213 -0
  152. package/src/miniflare-cli/request-context.ts +40 -0
  153. package/src/module-collection.ts +256 -53
  154. package/src/open-in-browser.ts +17 -0
  155. package/src/package-manager.ts +219 -0
  156. package/src/pages/build.tsx +238 -0
  157. package/src/pages/constants.ts +18 -0
  158. package/src/pages/deployments.tsx +106 -0
  159. package/src/pages/dev.tsx +693 -0
  160. package/src/pages/errors.ts +66 -0
  161. package/src/pages/functions/buildPlugin.ts +110 -0
  162. package/src/pages/functions/buildWorker.ts +156 -0
  163. package/src/pages/functions/filepath-routing.test.ts +234 -0
  164. package/src/pages/functions/filepath-routing.ts +189 -0
  165. package/src/pages/functions/identifiers.ts +78 -0
  166. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  167. package/src/pages/functions/routes-consolidation.ts +73 -0
  168. package/src/pages/functions/routes-transformation.test.ts +282 -0
  169. package/src/pages/functions/routes-transformation.ts +115 -0
  170. package/src/pages/functions/routes-validation.test.ts +403 -0
  171. package/src/pages/functions/routes-validation.ts +202 -0
  172. package/src/pages/functions/routes.ts +151 -0
  173. package/src/pages/functions.tsx +82 -0
  174. package/src/pages/hash.tsx +13 -0
  175. package/src/pages/index.tsx +94 -0
  176. package/src/pages/projects.tsx +169 -0
  177. package/src/pages/publish.tsx +436 -0
  178. package/src/pages/types.ts +49 -0
  179. package/src/pages/upload.tsx +425 -0
  180. package/src/pages/utils.ts +12 -0
  181. package/src/parse.ts +286 -0
  182. package/src/paths.ts +71 -0
  183. package/src/proxy.ts +678 -92
  184. package/src/publish/index.ts +267 -0
  185. package/src/publish/publish.ts +976 -0
  186. package/src/pubsub/index.ts +286 -0
  187. package/src/pubsub/pubsub-commands.tsx +623 -0
  188. package/src/queues/cli/commands/consumer/add.ts +75 -0
  189. package/src/queues/cli/commands/consumer/index.ts +19 -0
  190. package/src/queues/cli/commands/consumer/remove.ts +44 -0
  191. package/src/queues/cli/commands/create.ts +25 -0
  192. package/src/queues/cli/commands/delete.ts +26 -0
  193. package/src/queues/cli/commands/index.ts +32 -0
  194. package/src/queues/cli/commands/list.ts +25 -0
  195. package/src/queues/client.ts +135 -0
  196. package/src/r2/helpers.ts +131 -0
  197. package/src/r2/index.ts +277 -0
  198. package/src/routes.ts +140 -0
  199. package/src/secret/index.ts +404 -0
  200. package/src/selfsigned.d.ts +29 -0
  201. package/src/sites.tsx +357 -99
  202. package/src/tail/createTail.ts +315 -0
  203. package/src/tail/filters.ts +277 -0
  204. package/src/tail/index.ts +217 -0
  205. package/src/tail/printing.ts +107 -0
  206. package/src/type-generation.ts +159 -0
  207. package/src/update-check.ts +19 -0
  208. package/src/user/access.ts +69 -0
  209. package/src/user/choose-account.tsx +69 -0
  210. package/src/user/env-vars.ts +46 -0
  211. package/src/user/generate-auth-url.ts +33 -0
  212. package/src/user/generate-random-state.ts +16 -0
  213. package/src/user/index.ts +3 -0
  214. package/src/user/user.tsx +1192 -0
  215. package/src/utils/collectKeyValues.ts +14 -0
  216. package/src/whoami.tsx +169 -0
  217. package/src/worker.ts +243 -0
  218. package/src/yargs-types.ts +7 -0
  219. package/src/zones.ts +191 -0
  220. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  221. package/templates/checked-fetch.js +17 -0
  222. package/templates/d1-beta-facade.js +185 -0
  223. package/templates/experimental-local-cache-stubs.js +27 -0
  224. package/templates/first-party-worker-module-facade.ts +18 -0
  225. package/templates/format-dev-errors.ts +32 -0
  226. package/templates/gitignore +171 -0
  227. package/templates/init-tests/test-jest-new-worker.js +25 -0
  228. package/templates/init-tests/test-vitest-new-worker.js +26 -0
  229. package/templates/middleware/common.ts +62 -0
  230. package/templates/middleware/loader-modules.ts +84 -0
  231. package/templates/middleware/loader-sw.ts +213 -0
  232. package/templates/middleware/middleware-pretty-error.ts +40 -0
  233. package/templates/middleware/middleware-scheduled.ts +15 -0
  234. package/templates/new-worker-scheduled.js +17 -0
  235. package/templates/new-worker-scheduled.ts +32 -0
  236. package/templates/new-worker.js +15 -0
  237. package/templates/new-worker.ts +30 -0
  238. package/templates/no-op-worker.js +10 -0
  239. package/templates/pages-dev-pipeline.ts +28 -0
  240. package/templates/pages-dev-util.ts +52 -0
  241. package/templates/pages-shim.ts +9 -0
  242. package/templates/pages-template-plugin.ts +171 -0
  243. package/templates/pages-template-worker.ts +189 -0
  244. package/templates/serve-static-assets.ts +63 -0
  245. package/templates/service-bindings-module-facade.js +54 -0
  246. package/templates/service-bindings-sw-facade.js +42 -0
  247. package/templates/tsconfig.json +106 -0
  248. package/wrangler-dist/cli.d.ts +880 -0
  249. package/wrangler-dist/cli.js +144775 -105026
  250. package/pages/functions/buildWorker.ts +0 -57
  251. package/pages/functions/filepath-routing.test.ts +0 -32
  252. package/pages/functions/filepath-routing.ts +0 -225
  253. package/pages/functions/identifiers.ts +0 -78
  254. package/pages/functions/routes.ts +0 -158
  255. package/pages/functions/template-worker.ts +0 -135
  256. package/src/__tests__/mock-cfetch.ts +0 -171
  257. package/src/__tests__/mock-dialogs.ts +0 -65
  258. package/src/__tests__/run-in-tmp.ts +0 -19
  259. package/src/__tests__/run-wrangler.ts +0 -32
  260. package/src/api/form_data.ts +0 -131
  261. package/src/api/inspect.ts +0 -430
  262. package/src/api/preview.ts +0 -128
  263. package/src/api/worker.ts +0 -155
  264. package/src/config.ts +0 -131
  265. package/src/kv.tsx +0 -267
  266. package/src/pages.tsx +0 -921
  267. package/src/publish.ts +0 -350
  268. package/src/tail.tsx +0 -73
  269. package/src/user.tsx +0 -1022
  270. package/static-asset-facade.js +0 -47
  271. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  272. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  273. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  274. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  275. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  276. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  277. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  278. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  279. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  280. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  281. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  282. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  283. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  284. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  285. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  286. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  287. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  288. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  289. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  290. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  291. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  292. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  293. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  294. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  295. package/vendor/wrangler-mime/LICENSE +0 -21
  296. package/vendor/wrangler-mime/Mime.js +0 -97
  297. package/vendor/wrangler-mime/README.md +0 -187
  298. package/vendor/wrangler-mime/cli.js +0 -46
  299. package/vendor/wrangler-mime/index.js +0 -4
  300. package/vendor/wrangler-mime/lite.js +0 -4
  301. package/vendor/wrangler-mime/package.json +0 -52
  302. package/vendor/wrangler-mime/types/other.js +0 -1
  303. package/vendor/wrangler-mime/types/standard.js +0 -1
  304. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,3972 @@
1
+ import path from "node:path";
2
+ import { normalizeAndValidateConfig } from "../config/validation";
3
+ import { normalizeSlashes } from "./helpers/mock-console";
4
+ import type {
5
+ ConfigFields,
6
+ RawDevConfig,
7
+ RawConfig,
8
+ RawEnvironment,
9
+ } from "../config";
10
+
11
+ describe("normalizeAndValidateConfig()", () => {
12
+ it("should use defaults for empty configuration", () => {
13
+ const { config, diagnostics } = normalizeAndValidateConfig({}, undefined, {
14
+ env: undefined,
15
+ });
16
+
17
+ expect(config).toEqual({
18
+ account_id: undefined,
19
+ assets: undefined,
20
+ build: {
21
+ command: undefined,
22
+ cwd: undefined,
23
+ watch_dir: "./src",
24
+ },
25
+ compatibility_date: undefined,
26
+ compatibility_flags: [],
27
+ configPath: undefined,
28
+ d1_databases: [],
29
+ dev: {
30
+ ip: "0.0.0.0",
31
+ local_protocol: "http",
32
+ port: undefined, // the default of 8787 is set at runtime
33
+ upstream_protocol: "https",
34
+ host: undefined,
35
+ },
36
+ durable_objects: {
37
+ bindings: [],
38
+ },
39
+ jsx_factory: "React.createElement",
40
+ jsx_fragment: "React.Fragment",
41
+ tsconfig: undefined,
42
+ kv_namespaces: [],
43
+ legacy_env: true,
44
+ logfwdr: {
45
+ bindings: [],
46
+ schema: undefined,
47
+ },
48
+ send_metrics: undefined,
49
+ main: undefined,
50
+ migrations: [],
51
+ name: undefined,
52
+ queues: {
53
+ consumers: [],
54
+ producers: [],
55
+ },
56
+ r2_buckets: [],
57
+ services: [],
58
+ route: undefined,
59
+ routes: undefined,
60
+ rules: [],
61
+ site: undefined,
62
+ text_blobs: undefined,
63
+ triggers: {
64
+ crons: [],
65
+ },
66
+ unsafe: {
67
+ bindings: [],
68
+ },
69
+ dispatch_namespaces: [],
70
+ usage_model: undefined,
71
+ vars: {},
72
+ define: {},
73
+ wasm_modules: undefined,
74
+ data_blobs: undefined,
75
+ workers_dev: undefined,
76
+ zone_id: undefined,
77
+ no_bundle: undefined,
78
+ minify: undefined,
79
+ node_compat: undefined,
80
+ first_party_worker: undefined,
81
+ keep_vars: undefined,
82
+ });
83
+ expect(diagnostics.hasErrors()).toBe(false);
84
+ expect(diagnostics.hasWarnings()).toBe(false);
85
+ });
86
+
87
+ describe("top-level non-environment configuration", () => {
88
+ it("should override config defaults with provided values", () => {
89
+ const expectedConfig: Partial<ConfigFields<RawDevConfig>> = {
90
+ legacy_env: true,
91
+ send_metrics: false,
92
+ dev: {
93
+ ip: "255.255.255.255",
94
+ port: 9999,
95
+ local_protocol: "https",
96
+ upstream_protocol: "http",
97
+ },
98
+ };
99
+
100
+ const { config, diagnostics } = normalizeAndValidateConfig(
101
+ expectedConfig,
102
+ undefined,
103
+ { env: undefined }
104
+ );
105
+
106
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
107
+ expect(diagnostics.hasErrors()).toBe(false);
108
+ expect(diagnostics.hasWarnings()).toBe(false);
109
+ });
110
+
111
+ it("should error on invalid top level fields", () => {
112
+ const expectedConfig = {
113
+ legacy_env: "FOO",
114
+ send_metrics: "BAD",
115
+ keep_vars: "NEVER",
116
+ dev: {
117
+ ip: 222,
118
+ port: "FOO",
119
+ local_protocol: "wss",
120
+ upstream_protocol: "ws",
121
+ },
122
+ };
123
+
124
+ const { config, diagnostics } = normalizeAndValidateConfig(
125
+ expectedConfig as unknown as RawConfig,
126
+ undefined,
127
+ { env: undefined }
128
+ );
129
+
130
+ expect(config).toEqual(
131
+ expect.objectContaining({ ...expectedConfig, main: undefined })
132
+ );
133
+ expect(diagnostics.hasWarnings()).toBe(false);
134
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
135
+ "Processing wrangler configuration:
136
+ - Expected \\"legacy_env\\" to be of type boolean but got \\"FOO\\".
137
+ - Expected \\"send_metrics\\" to be of type boolean but got \\"BAD\\".
138
+ - Expected \\"keep_vars\\" to be of type boolean but got \\"NEVER\\".
139
+ - Expected \\"dev.ip\\" to be of type string but got 222.
140
+ - Expected \\"dev.port\\" to be of type number but got \\"FOO\\".
141
+ - Expected \\"dev.local_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"wss\\".
142
+ - Expected \\"dev.upstream_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"ws\\"."
143
+ `);
144
+ });
145
+
146
+ it("should warn on and remove unexpected top level fields", () => {
147
+ const expectedConfig = {
148
+ unexpected: {
149
+ subkey: "some-value",
150
+ },
151
+ };
152
+
153
+ const { config, diagnostics } = normalizeAndValidateConfig(
154
+ expectedConfig as unknown as RawConfig,
155
+ undefined,
156
+ { env: undefined }
157
+ );
158
+
159
+ expect("unexpected" in config).toBe(false);
160
+ expect(diagnostics.hasErrors()).toBe(false);
161
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
162
+ "Processing wrangler configuration:
163
+ - Unexpected fields found in top-level field: \\"unexpected\\""
164
+ `);
165
+ });
166
+
167
+ it("should report a deprecation warning if `miniflare` appears at the top level", () => {
168
+ const expectedConfig = {
169
+ miniflare: {
170
+ host: "localhost",
171
+ },
172
+ };
173
+
174
+ const { config, diagnostics } = normalizeAndValidateConfig(
175
+ expectedConfig as unknown as RawConfig,
176
+ undefined,
177
+ { env: undefined }
178
+ );
179
+
180
+ expect("miniflare" in config).toBe(false);
181
+ expect(diagnostics.hasErrors()).toBe(false);
182
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
183
+ "Processing wrangler configuration:
184
+ - 😶 Ignored: \\"miniflare\\":
185
+ Wrangler does not use configuration in the \`miniflare\` section. Unless you are using Miniflare directly you can remove this section."
186
+ `);
187
+ });
188
+
189
+ it("should normalise a blank route value to be undefined", () => {
190
+ const expectedConfig = {
191
+ route: "",
192
+ };
193
+
194
+ const { config, diagnostics } = normalizeAndValidateConfig(
195
+ expectedConfig as unknown as RawConfig,
196
+ undefined,
197
+ { env: undefined }
198
+ );
199
+
200
+ expect(config.route).toBeUndefined();
201
+ expect(diagnostics.hasErrors()).toBe(false);
202
+ expect(diagnostics.hasWarnings()).toBe(true);
203
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
204
+ "Processing wrangler configuration:
205
+ - The \\"route\\" field in your configuration is an empty string and will be ignored.
206
+ Please remove the \\"route\\" field from your configuration."
207
+ `);
208
+ });
209
+
210
+ it("should normalise a blank account_id value to be undefined", () => {
211
+ const expectedConfig = {
212
+ account_id: "",
213
+ };
214
+
215
+ const { config, diagnostics } = normalizeAndValidateConfig(
216
+ expectedConfig as unknown as RawConfig,
217
+ undefined,
218
+ { env: undefined }
219
+ );
220
+
221
+ expect(config.account_id).toBeUndefined();
222
+ expect(diagnostics.hasErrors()).toBe(false);
223
+ expect(diagnostics.hasWarnings()).toBe(true);
224
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
225
+ "Processing wrangler configuration:
226
+ - The \\"account_id\\" field in your configuration is an empty string and will be ignored.
227
+ Please remove the \\"account_id\\" field from your configuration."
228
+ `);
229
+ });
230
+
231
+ describe("[migrations]", () => {
232
+ it("should override `migrations` config defaults with provided values", () => {
233
+ const expectedConfig: RawConfig = {
234
+ migrations: [
235
+ {
236
+ tag: "TAG",
237
+ new_classes: ["CLASS_1", "CLASS_2"],
238
+ renamed_classes: [
239
+ {
240
+ from: "FROM_CLASS",
241
+ to: "TO_CLASS",
242
+ },
243
+ ],
244
+ deleted_classes: ["CLASS_3", "CLASS_4"],
245
+ },
246
+ ],
247
+ };
248
+
249
+ const { config, diagnostics } = normalizeAndValidateConfig(
250
+ expectedConfig,
251
+ undefined,
252
+ { env: undefined }
253
+ );
254
+
255
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
256
+ expect(diagnostics.hasErrors()).toBe(false);
257
+ expect(diagnostics.hasWarnings()).toBe(false);
258
+ });
259
+
260
+ it("should error on invalid `migrations` values", () => {
261
+ const expectedConfig = {
262
+ migrations: [
263
+ {
264
+ tag: 111,
265
+ new_classes: [222, 333],
266
+ renamed_classes: [
267
+ {
268
+ from: 444,
269
+ to: 555,
270
+ },
271
+ ],
272
+ deleted_classes: [666, 777],
273
+ },
274
+ ],
275
+ };
276
+
277
+ const { config, diagnostics } = normalizeAndValidateConfig(
278
+ expectedConfig as unknown as RawConfig,
279
+ undefined,
280
+ { env: undefined }
281
+ );
282
+
283
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
284
+ expect(diagnostics.hasWarnings()).toBe(false);
285
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
286
+ "Processing wrangler configuration:
287
+ - Expected \\"migrations[0].tag\\" to be of type string but got 111.
288
+ - Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
289
+ - Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
290
+ - Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
291
+ - Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
292
+ - Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
293
+ `);
294
+ });
295
+
296
+ it("should warn/error on unexpected fields on `migrations`", async () => {
297
+ const expectedConfig = {
298
+ migrations: [
299
+ {
300
+ tag: "TAG",
301
+ new_classes: ["CLASS_1", "CLASS_2"],
302
+ renamed_classes: [
303
+ {
304
+ from: "FROM_CLASS",
305
+ to: "TO_CLASS",
306
+ },
307
+ {
308
+ a: "something",
309
+ b: "someone",
310
+ },
311
+ ],
312
+ deleted_classes: ["CLASS_3", "CLASS_4"],
313
+ unrecognized_field: "FOO",
314
+ },
315
+ ],
316
+ };
317
+
318
+ const { config, diagnostics } = normalizeAndValidateConfig(
319
+ expectedConfig as unknown as RawConfig,
320
+ undefined,
321
+ { env: undefined }
322
+ );
323
+
324
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
325
+ expect(diagnostics.hasErrors()).toBe(true);
326
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
327
+ "Processing wrangler configuration:
328
+ - Unexpected fields found in migrations field: \\"unrecognized_field\\""
329
+ `);
330
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
331
+ "Processing wrangler configuration:
332
+ - Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":\\"FROM_CLASS\\",\\"to\\":\\"TO_CLASS\\"},{\\"a\\":\\"something\\",\\"b\\":\\"someone\\"}]."
333
+ `);
334
+ });
335
+ });
336
+
337
+ describe("[site]", () => {
338
+ it("should override `site` config defaults with provided values", () => {
339
+ const expectedConfig: RawConfig = {
340
+ site: {
341
+ bucket: "BUCKET",
342
+ "entry-point": "my-site",
343
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
344
+ include: ["INCLUDE_1", "INCLUDE_2"],
345
+ },
346
+ };
347
+
348
+ const { config, diagnostics } = normalizeAndValidateConfig(
349
+ expectedConfig,
350
+ undefined,
351
+ { env: undefined }
352
+ );
353
+
354
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
355
+ expect(diagnostics.hasErrors()).toBe(false);
356
+ expect(diagnostics.hasWarnings()).toBe(true);
357
+
358
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
359
+ .toMatchInlineSnapshot(`
360
+ "Processing wrangler configuration:
361
+ - Deprecation: \\"site.entry-point\\":
362
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
363
+ \`\`\`
364
+ main = \\"my-site/index.js\\"
365
+ \`\`\`"
366
+ `);
367
+ });
368
+
369
+ it("should error if `site` config is missing `bucket`", () => {
370
+ const expectedConfig: RawConfig = {
371
+ // @ts-expect-error we're intentionally passing an invalid configuration here
372
+ site: {
373
+ "entry-point": "workers-site",
374
+ include: ["INCLUDE_1", "INCLUDE_2"],
375
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
376
+ },
377
+ };
378
+
379
+ const { config, diagnostics } = normalizeAndValidateConfig(
380
+ expectedConfig,
381
+ undefined,
382
+ { env: undefined }
383
+ );
384
+
385
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
386
+ expect(diagnostics.hasWarnings()).toBe(true);
387
+ expect(diagnostics.hasErrors()).toBe(true);
388
+
389
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
390
+ .toMatchInlineSnapshot(`
391
+ "Processing wrangler configuration:
392
+ - Deprecation: \\"site.entry-point\\":
393
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
394
+ \`\`\`
395
+ main = \\"workers-site/index.js\\"
396
+ \`\`\`"
397
+ `);
398
+
399
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
400
+ "Processing wrangler configuration:
401
+ - \\"site.bucket\\" is a required field."
402
+ `);
403
+ });
404
+
405
+ it("should error on invalid `site` values", () => {
406
+ const expectedConfig = {
407
+ site: {
408
+ bucket: "BUCKET",
409
+ "entry-point": 111,
410
+ include: [222, 333],
411
+ exclude: [444, 555],
412
+ },
413
+ };
414
+
415
+ const { config, diagnostics } = normalizeAndValidateConfig(
416
+ expectedConfig as unknown as RawConfig,
417
+ undefined,
418
+ { env: undefined }
419
+ );
420
+
421
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
422
+ expect(diagnostics.hasWarnings()).toBe(true);
423
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
424
+ "Processing wrangler configuration:
425
+ - Expected \\"sites.include.[0]\\" to be of type string but got 222.
426
+ - Expected \\"sites.include.[1]\\" to be of type string but got 333.
427
+ - Expected \\"sites.exclude.[0]\\" to be of type string but got 444.
428
+ - Expected \\"sites.exclude.[1]\\" to be of type string but got 555.
429
+ - Expected \\"site.entry-point\\" to be of type string but got 111."
430
+ `);
431
+
432
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
433
+ .toMatchInlineSnapshot(`
434
+ "Processing wrangler configuration:
435
+ - Deprecation: \\"site.entry-point\\":
436
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
437
+ \`\`\`
438
+ main = \\"111/index.js\\"
439
+ \`\`\`"
440
+ `);
441
+ });
442
+
443
+ it("should log a deprecation warning if entry-point is defined", async () => {
444
+ const { config, diagnostics } = normalizeAndValidateConfig(
445
+ {
446
+ site: {
447
+ bucket: "some/path",
448
+ "entry-point": "some/other/script.js",
449
+ },
450
+ } as unknown as RawConfig,
451
+ undefined,
452
+ { env: undefined }
453
+ );
454
+
455
+ expect(config.site).toMatchInlineSnapshot(`
456
+ Object {
457
+ "bucket": "some/path",
458
+ "entry-point": "some/other/script.js",
459
+ "exclude": Array [],
460
+ "include": Array [],
461
+ }
462
+ `);
463
+ expect(diagnostics.hasWarnings()).toBe(true);
464
+ expect(diagnostics.hasErrors()).toBe(false);
465
+
466
+ expect(normalizeSlashes(diagnostics.renderWarnings()))
467
+ .toMatchInlineSnapshot(`
468
+ "Processing wrangler configuration:
469
+ - Deprecation: \\"site.entry-point\\":
470
+ Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:
471
+ \`\`\`
472
+ main = \\"some/other/script.js\\"
473
+ \`\`\`"
474
+ `);
475
+ });
476
+ });
477
+
478
+ describe("[assets]", () => {
479
+ it("normalizes a string input to an object", () => {
480
+ const { config, diagnostics } = normalizeAndValidateConfig(
481
+ {
482
+ assets: "path/to/assets",
483
+ } as unknown as RawConfig,
484
+ undefined,
485
+ { env: undefined }
486
+ );
487
+
488
+ expect(config.assets).toMatchInlineSnapshot(`
489
+ Object {
490
+ "browser_TTL": undefined,
491
+ "bucket": "path/to/assets",
492
+ "exclude": Array [],
493
+ "include": Array [],
494
+ "serve_single_page_app": false,
495
+ }
496
+ `);
497
+ expect(diagnostics.hasWarnings()).toBe(true);
498
+ expect(diagnostics.hasErrors()).toBe(false);
499
+
500
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
501
+ "Processing wrangler configuration:
502
+ - \\"assets\\" fields are experimental and may change or break at any time."
503
+ `);
504
+ });
505
+
506
+ it("errors when input is not a string or object", () => {
507
+ const { config, diagnostics } = normalizeAndValidateConfig(
508
+ {
509
+ assets: 123,
510
+ } as unknown as RawConfig,
511
+ undefined,
512
+ { env: undefined }
513
+ );
514
+
515
+ expect(config.assets).toBeUndefined();
516
+ expect(diagnostics.hasWarnings()).toBe(true);
517
+ expect(diagnostics.hasErrors()).toBe(true);
518
+
519
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
520
+ "Processing wrangler configuration:
521
+ - \\"assets\\" fields are experimental and may change or break at any time."
522
+ `);
523
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
524
+ "Processing wrangler configuration:
525
+ - Expected the \`assets\` field to be a string or an object, but got number."
526
+ `);
527
+ });
528
+
529
+ it("should error if `assets` config is missing `bucket`", () => {
530
+ const expectedConfig: RawConfig = {
531
+ // @ts-expect-error we're intentionally passing an invalid configuration here
532
+ assets: {
533
+ include: ["INCLUDE_1", "INCLUDE_2"],
534
+ exclude: ["EXCLUDE_1", "EXCLUDE_2"],
535
+ },
536
+ };
537
+
538
+ const { config, diagnostics } = normalizeAndValidateConfig(
539
+ expectedConfig,
540
+ undefined,
541
+ { env: undefined }
542
+ );
543
+
544
+ expect(config.assets).toEqual(
545
+ expect.objectContaining(expectedConfig.assets)
546
+ );
547
+ expect(diagnostics.hasWarnings()).toBe(true);
548
+ expect(diagnostics.hasErrors()).toBe(true);
549
+
550
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
551
+ "Processing wrangler configuration:
552
+ - \\"assets\\" fields are experimental and may change or break at any time."
553
+ `);
554
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
555
+ "Processing wrangler configuration:
556
+ - \\"assets.bucket\\" is a required field."
557
+ `);
558
+ });
559
+
560
+ it("should error on invalid `assets` values", () => {
561
+ const expectedConfig = {
562
+ assets: {
563
+ bucket: "BUCKET",
564
+ include: [222, 333],
565
+ exclude: [444, 555],
566
+ browser_TTL: "not valid",
567
+ serve_single_page_app: "INVALID",
568
+ },
569
+ };
570
+
571
+ const { config, diagnostics } = normalizeAndValidateConfig(
572
+ expectedConfig as unknown as RawConfig,
573
+ undefined,
574
+ { env: undefined }
575
+ );
576
+
577
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
578
+ expect(diagnostics.hasWarnings()).toBe(true);
579
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
580
+ "Processing wrangler configuration:
581
+ - \\"assets\\" fields are experimental and may change or break at any time."
582
+ `);
583
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
584
+ "Processing wrangler configuration:
585
+ - Expected \\"assets.include.[0]\\" to be of type string but got 222.
586
+ - Expected \\"assets.include.[1]\\" to be of type string but got 333.
587
+ - Expected \\"assets.exclude.[0]\\" to be of type string but got 444.
588
+ - Expected \\"assets.exclude.[1]\\" to be of type string but got 555.
589
+ - Expected \\"assets.browser_TTL\\" to be of type number but got \\"not valid\\".
590
+ - Expected \\"assets.serve_single_page_app\\" to be of type boolean but got \\"INVALID\\"."
591
+ `);
592
+ });
593
+ });
594
+
595
+ it("should map `wasm_module` paths from relative to the config path to relative to the cwd", () => {
596
+ const expectedConfig: RawConfig = {
597
+ wasm_modules: {
598
+ MODULE_1: "path/to/module_1.mjs",
599
+ MODULE_2: "module_2.mjs",
600
+ },
601
+ };
602
+
603
+ const { config, diagnostics } = normalizeAndValidateConfig(
604
+ expectedConfig,
605
+ "project/wrangler.toml",
606
+ { env: undefined }
607
+ );
608
+
609
+ expect(config).toEqual(
610
+ expect.objectContaining({
611
+ wasm_modules: {
612
+ MODULE_1: path.normalize("project/path/to/module_1.mjs"),
613
+ MODULE_2: path.normalize("project/module_2.mjs"),
614
+ },
615
+ })
616
+ );
617
+ expect(diagnostics.hasErrors()).toBe(false);
618
+ expect(diagnostics.hasWarnings()).toBe(false);
619
+ });
620
+
621
+ it("should warn on unexpected fields on `triggers`", async () => {
622
+ const expectedConfig: RawConfig = {
623
+ triggers: {
624
+ crons: ["1 * * * *"],
625
+ // @ts-expect-error we're purposely adding a field
626
+ // that doesn't belong here
627
+ someOtherfield: 123,
628
+ },
629
+ };
630
+
631
+ const { config, diagnostics } = normalizeAndValidateConfig(
632
+ expectedConfig,
633
+ "project/wrangler.toml",
634
+ { env: undefined }
635
+ );
636
+
637
+ expect(config).toEqual(
638
+ expect.objectContaining({
639
+ triggers: {
640
+ crons: ["1 * * * *"],
641
+ someOtherfield: 123,
642
+ },
643
+ })
644
+ );
645
+ expect(diagnostics.hasErrors()).toBe(false);
646
+ expect(diagnostics.hasWarnings()).toBe(true);
647
+
648
+ expect(normalizePath(diagnostics.renderWarnings()))
649
+ .toMatchInlineSnapshot(`
650
+ "Processing project/wrangler.toml configuration:
651
+ - Unexpected fields found in triggers field: \\"someOtherfield\\""
652
+ `);
653
+ });
654
+
655
+ it("should error on invalid `wasm_modules` paths", () => {
656
+ const expectedConfig = {
657
+ wasm_modules: {
658
+ MODULE_1: 111,
659
+ MODULE_2: 222,
660
+ },
661
+ };
662
+
663
+ const { config, diagnostics } = normalizeAndValidateConfig(
664
+ expectedConfig as unknown as RawConfig,
665
+ "project/wrangler.toml",
666
+ { env: undefined }
667
+ );
668
+
669
+ expect(config).toEqual(
670
+ expect.objectContaining({
671
+ wasm_modules: {},
672
+ })
673
+ );
674
+ expect(diagnostics.hasWarnings()).toBe(false);
675
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
676
+ "Processing project/wrangler.toml configuration:
677
+ - Expected \\"wasm_modules['MODULE_1']\\" to be of type string but got 111.
678
+ - Expected \\"wasm_modules['MODULE_2']\\" to be of type string but got 222."
679
+ `);
680
+ });
681
+
682
+ it("should map `text_blobs` paths from relative to the config path to relative to the cwd", () => {
683
+ const expectedConfig: RawConfig = {
684
+ text_blobs: {
685
+ BLOB_1: "path/to/text1.txt",
686
+ BLOB_2: "text2.md",
687
+ },
688
+ };
689
+
690
+ const { config, diagnostics } = normalizeAndValidateConfig(
691
+ expectedConfig,
692
+ "project/wrangler.toml",
693
+ { env: undefined }
694
+ );
695
+
696
+ expect(config).toEqual(
697
+ expect.objectContaining({
698
+ text_blobs: {
699
+ BLOB_1: path.normalize("project/path/to/text1.txt"),
700
+ BLOB_2: path.normalize("project/text2.md"),
701
+ },
702
+ })
703
+ );
704
+ expect(diagnostics.hasErrors()).toBe(false);
705
+ expect(diagnostics.hasWarnings()).toBe(false);
706
+ });
707
+
708
+ it("should error on invalid `text_blob` paths", () => {
709
+ const expectedConfig = {
710
+ text_blobs: {
711
+ MODULE_1: 111,
712
+ MODULE_2: 222,
713
+ },
714
+ };
715
+
716
+ const { config, diagnostics } = normalizeAndValidateConfig(
717
+ expectedConfig as unknown as RawConfig,
718
+ "project/wrangler.toml",
719
+ { env: undefined }
720
+ );
721
+
722
+ expect(config).toEqual(
723
+ expect.objectContaining({
724
+ text_blobs: {},
725
+ })
726
+ );
727
+ expect(diagnostics.hasWarnings()).toBe(false);
728
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
729
+ "Processing project/wrangler.toml configuration:
730
+ - Expected \\"text_blobs['MODULE_1']\\" to be of type string but got 111.
731
+ - Expected \\"text_blobs['MODULE_2']\\" to be of type string but got 222."
732
+ `);
733
+ });
734
+
735
+ it("should map `data_blobs` paths from relative to the config path to relative to the cwd", () => {
736
+ const expectedConfig: RawConfig = {
737
+ data_blobs: {
738
+ BLOB_1: "path/to/data1.bin",
739
+ BLOB_2: "data2.bin",
740
+ },
741
+ };
742
+
743
+ const { config, diagnostics } = normalizeAndValidateConfig(
744
+ expectedConfig,
745
+ "project/wrangler.toml",
746
+ { env: undefined }
747
+ );
748
+
749
+ expect(config).toEqual(
750
+ expect.objectContaining({
751
+ data_blobs: {
752
+ BLOB_1: path.normalize("project/path/to/data1.bin"),
753
+ BLOB_2: path.normalize("project/data2.bin"),
754
+ },
755
+ })
756
+ );
757
+ expect(diagnostics.hasErrors()).toBe(false);
758
+ expect(diagnostics.hasWarnings()).toBe(false);
759
+ });
760
+
761
+ it("should error on invalid `data_blob` paths", () => {
762
+ const expectedConfig = {
763
+ data_blobs: {
764
+ MODULE_1: 111,
765
+ MODULE_2: 222,
766
+ },
767
+ };
768
+
769
+ const { config, diagnostics } = normalizeAndValidateConfig(
770
+ expectedConfig as unknown as RawConfig,
771
+ "project/wrangler.toml",
772
+ { env: undefined }
773
+ );
774
+
775
+ expect(config).toEqual(
776
+ expect.objectContaining({
777
+ data_blobs: {},
778
+ })
779
+ );
780
+ expect(diagnostics.hasWarnings()).toBe(false);
781
+ expect(normalizePath(diagnostics.renderErrors())).toMatchInlineSnapshot(`
782
+ "Processing project/wrangler.toml configuration:
783
+ - Expected \\"data_blobs['MODULE_1']\\" to be of type string but got 111.
784
+ - Expected \\"data_blobs['MODULE_2']\\" to be of type string but got 222."
785
+ `);
786
+ });
787
+
788
+ it("should resolve tsconfig relative to wrangler.toml", async () => {
789
+ const expectedConfig: RawEnvironment = {
790
+ tsconfig: "path/to/some-tsconfig.json",
791
+ };
792
+
793
+ const { config, diagnostics } = normalizeAndValidateConfig(
794
+ expectedConfig,
795
+ "project/wrangler.toml",
796
+ { env: undefined }
797
+ );
798
+
799
+ expect(config).toEqual(
800
+ expect.objectContaining({
801
+ tsconfig: path.normalize("project/path/to/some-tsconfig.json"),
802
+ })
803
+ );
804
+
805
+ expect(diagnostics.hasErrors()).toBe(false);
806
+ expect(diagnostics.hasWarnings()).toBe(false);
807
+ });
808
+
809
+ describe("(deprecated)", () => {
810
+ it("should remove and warn about deprecated properties", () => {
811
+ const rawConfig: RawConfig = {
812
+ type: "webpack",
813
+ webpack_config: "CONFIG",
814
+ };
815
+
816
+ const { config, diagnostics } = normalizeAndValidateConfig(
817
+ rawConfig,
818
+ undefined,
819
+ { env: undefined }
820
+ );
821
+
822
+ // Note the `.not.` here...
823
+ expect(config).toEqual(
824
+ expect.not.objectContaining({
825
+ type: expect.anything(),
826
+ webpack_config: expect.anything(),
827
+ })
828
+ );
829
+ expect(diagnostics.hasErrors()).toBe(false);
830
+ expect(diagnostics.hasWarnings()).toBe(true);
831
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
832
+ "Processing wrangler configuration:
833
+ - 😶 Ignored: \\"type\\":
834
+ 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.
835
+ - 😶 Ignored: \\"webpack_config\\":
836
+ 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."
837
+ `);
838
+ });
839
+ });
840
+ });
841
+
842
+ describe("top-level environment configuration", () => {
843
+ it("should override config defaults with provided values", () => {
844
+ const main = "src/index.ts";
845
+ const resolvedMain = path.resolve(process.cwd(), main);
846
+
847
+ const expectedConfig: RawEnvironment = {
848
+ name: "mock-name",
849
+ account_id: "ACCOUNT_ID",
850
+ compatibility_date: "2022-01-01",
851
+ compatibility_flags: ["FLAG1", "FLAG2"],
852
+ workers_dev: false,
853
+ routes: [
854
+ "ROUTE_1",
855
+ "ROUTE_2",
856
+ { pattern: "ROUTE3", zone_id: "ZONE_ID_3" },
857
+ "ROUTE_4",
858
+ ],
859
+ jsx_factory: "JSX_FACTORY",
860
+ jsx_fragment: "JSX_FRAGMENT",
861
+ tsconfig: "path/to/tsconfig",
862
+ triggers: { crons: ["CRON_1", "CRON_2"] },
863
+ usage_model: "bundled",
864
+ main,
865
+ build: {
866
+ command: "COMMAND",
867
+ cwd: "CWD",
868
+ watch_dir: "WATCH_DIR",
869
+ },
870
+ define: {
871
+ DEF1: "DEFINE_1",
872
+ DEF2: "DEFINE_2",
873
+ },
874
+ vars: {
875
+ VAR1: "VALUE_1",
876
+ VAR2: "VALUE_2",
877
+ },
878
+ durable_objects: {
879
+ bindings: [
880
+ { name: "DO_BINDING_1", class_name: "CLASS1" },
881
+ {
882
+ name: "DO_BINDING_2",
883
+ class_name: "CLASS2",
884
+ script_name: "SCRIPT2",
885
+ },
886
+ {
887
+ name: "DO_BINDING_3",
888
+ class_name: "CLASS3",
889
+ script_name: "SCRIPT3",
890
+ environment: "ENV3",
891
+ },
892
+ ],
893
+ },
894
+ kv_namespaces: [
895
+ { binding: "KV_BINDING_1", id: "KV_ID_1" },
896
+ {
897
+ binding: "KV_BINDING_2",
898
+ id: "KV_ID_2",
899
+ preview_id: "KV_PREVIEW_1",
900
+ },
901
+ ],
902
+ r2_buckets: [
903
+ { binding: "R2_BINDING_1", bucket_name: "R2_BUCKET_1" },
904
+ {
905
+ binding: "R2_BINDING_2",
906
+ bucket_name: "R2_BUCKET_2",
907
+ preview_bucket_name: "R2_PREVIEW_2",
908
+ },
909
+ ],
910
+ services: [
911
+ {
912
+ binding: "SERVICE_BINDING_1",
913
+ service: "SERVICE_TYPE_1",
914
+ environment: "SERVICE_BINDING_ENVIRONMENT_1",
915
+ },
916
+ ],
917
+ unsafe: {
918
+ bindings: [
919
+ { name: "UNSAFE_BINDING_1", type: "UNSAFE_TYPE_1" },
920
+ {
921
+ name: "UNSAFE_BINDING_2",
922
+ type: "UNSAFE_TYPE_2",
923
+ extra: "UNSAFE_EXTRA_1",
924
+ },
925
+ ],
926
+ },
927
+ no_bundle: true,
928
+ minify: true,
929
+ node_compat: true,
930
+ first_party_worker: true,
931
+ };
932
+
933
+ const { config, diagnostics } = normalizeAndValidateConfig(
934
+ expectedConfig,
935
+ undefined,
936
+ { env: undefined }
937
+ );
938
+
939
+ expect(config).toEqual(
940
+ expect.objectContaining({ ...expectedConfig, main: resolvedMain })
941
+ );
942
+ expect(diagnostics.hasErrors()).toBe(false);
943
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
944
+ "Processing wrangler configuration:
945
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
946
+ - \\"services\\" fields are experimental and may change or break at any time.
947
+ - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS1), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
948
+
949
+ \`\`\`
950
+ [[migrations]]
951
+ tag = \\"v1\\" # Should be unique for each entry
952
+ new_classes = [\\"CLASS1\\"]
953
+ \`\`\`
954
+
955
+ Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details."
956
+ `);
957
+ });
958
+
959
+ it("should error on invalid environment values", () => {
960
+ const expectedConfig: RawEnvironment = {
961
+ name: 111,
962
+ account_id: 222,
963
+ compatibility_date: 333,
964
+ compatibility_flags: [444, 555],
965
+ workers_dev: "BAD",
966
+ routes: [
967
+ 666,
968
+ 777,
969
+ // this one's valid, but we add it here to make sure
970
+ // it doesn't get included in the error message
971
+ "example.com/*",
972
+ { pattern: 123, zone_id: "zone_id_1" },
973
+ { pattern: "route_2", zone_id: 123 },
974
+ { pattern: "route_2", zone_name: 123 },
975
+ { pattern: "route_3" },
976
+ { zone_id: "zone_id_4" },
977
+ { zone_name: "zone_name_4" },
978
+ { pattern: undefined },
979
+ { pattern: "route_5", zone_id: "zone_id_5", some_other_key: 123 },
980
+ { pattern: "route_5", zone_name: "zone_name_5", some_other_key: 123 },
981
+ // this one's valid too
982
+ { pattern: "route_6", zone_id: "zone_id_6" },
983
+ // as well as this one
984
+ { pattern: "route_6", zone_name: "zone_name_6" },
985
+ ],
986
+ route: 888,
987
+ jsx_factory: 999,
988
+ jsx_fragment: 1000,
989
+ tsconfig: true,
990
+ triggers: { crons: [1111, 1222] },
991
+ usage_model: "INVALID",
992
+ main: 1333,
993
+ build: {
994
+ command: 1444,
995
+ cwd: 1555,
996
+ watch_dir: 1666,
997
+ },
998
+ define: {
999
+ DEF1: 1777,
1000
+ },
1001
+ no_bundle: "INVALID",
1002
+ minify: "INVALID",
1003
+ node_compat: "INVALID",
1004
+ first_party_worker: "INVALID",
1005
+ } as unknown as RawEnvironment;
1006
+
1007
+ const { config, diagnostics } = normalizeAndValidateConfig(
1008
+ expectedConfig,
1009
+ undefined,
1010
+ { env: undefined }
1011
+ );
1012
+
1013
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1014
+ expect(diagnostics.hasWarnings()).toBe(false);
1015
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1016
+ "Processing wrangler configuration:
1017
+ - Expected \\"route\\" to be either a string, or an object with shape { pattern, custom_domain, zone_id | zone_name }, but got 888.
1018
+ - Expected \\"account_id\\" to be of type string but got 222.
1019
+ - Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, custom_domain, zone_id | zone_name }, but these weren't valid: [
1020
+ 666,
1021
+ 777,
1022
+ {
1023
+ \\"pattern\\": 123,
1024
+ \\"zone_id\\": \\"zone_id_1\\"
1025
+ },
1026
+ {
1027
+ \\"pattern\\": \\"route_2\\",
1028
+ \\"zone_id\\": 123
1029
+ },
1030
+ {
1031
+ \\"pattern\\": \\"route_2\\",
1032
+ \\"zone_name\\": 123
1033
+ },
1034
+ {
1035
+ \\"pattern\\": \\"route_3\\"
1036
+ },
1037
+ {
1038
+ \\"zone_id\\": \\"zone_id_4\\"
1039
+ },
1040
+ {
1041
+ \\"zone_name\\": \\"zone_name_4\\"
1042
+ },
1043
+ {},
1044
+ {
1045
+ \\"pattern\\": \\"route_5\\",
1046
+ \\"zone_id\\": \\"zone_id_5\\",
1047
+ \\"some_other_key\\": 123
1048
+ },
1049
+ {
1050
+ \\"pattern\\": \\"route_5\\",
1051
+ \\"zone_name\\": \\"zone_name_5\\",
1052
+ \\"some_other_key\\": 123
1053
+ }
1054
+ ].
1055
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
1056
+ - Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
1057
+ - Expected \\"build.command\\" to be of type string but got 1444.
1058
+ - Expected \\"build.cwd\\" to be of type string but got 1555.
1059
+ - Expected \\"build.watch_dir\\" to be of type string but got 1666.
1060
+ - Expected \\"compatibility_date\\" to be of type string but got 333.
1061
+ - Expected \\"compatibility_flags\\" to be of type string array but got [444,555].
1062
+ - Expected \\"jsx_factory\\" to be of type string but got 999.
1063
+ - Expected \\"jsx_fragment\\" to be of type string but got 1000.
1064
+ - Expected \\"tsconfig\\" to be of type string but got true.
1065
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got 111.
1066
+ - Expected \\"main\\" to be of type string but got 1333.
1067
+ - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\".
1068
+ - The field \\"define.DEF1\\" should be a string but got 1777.
1069
+ - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\".
1070
+ - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\".
1071
+ - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\".
1072
+ - Expected \\"first_party_worker\\" to be of type boolean but got \\"INVALID\\"."
1073
+ `);
1074
+ });
1075
+
1076
+ describe("name", () => {
1077
+ it("should error on invalid `name` value with spaces", () => {
1078
+ const expectedConfig: RawEnvironment = {
1079
+ name: "NCC 1701 D",
1080
+ } as unknown as RawEnvironment;
1081
+
1082
+ const { config, diagnostics } = normalizeAndValidateConfig(
1083
+ expectedConfig,
1084
+ undefined,
1085
+ { env: undefined }
1086
+ );
1087
+
1088
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1089
+ expect(diagnostics.hasWarnings()).toBe(false);
1090
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1091
+ "Processing wrangler configuration:
1092
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"NCC 1701 D\\"."
1093
+ `);
1094
+ });
1095
+
1096
+ it("should be valid `name` with underscores", () => {
1097
+ const expectedConfig: RawEnvironment = {
1098
+ name: "enterprise_nx_01",
1099
+ } as unknown as RawEnvironment;
1100
+
1101
+ const { config, diagnostics } = normalizeAndValidateConfig(
1102
+ expectedConfig,
1103
+ undefined,
1104
+ { env: undefined }
1105
+ );
1106
+
1107
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1108
+ expect(diagnostics.hasWarnings()).toBe(false);
1109
+ expect(diagnostics.hasErrors()).toBe(false);
1110
+ });
1111
+
1112
+ it("should error on invalid `name` value with special characters", () => {
1113
+ const expectedConfig: RawEnvironment = {
1114
+ name: "Thy'lek-Shran",
1115
+ } as unknown as RawEnvironment;
1116
+
1117
+ const { config, diagnostics } = normalizeAndValidateConfig(
1118
+ expectedConfig,
1119
+ undefined,
1120
+ { env: undefined }
1121
+ );
1122
+
1123
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1124
+ expect(diagnostics.hasWarnings()).toBe(false);
1125
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1126
+ "Processing wrangler configuration:
1127
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"Thy'lek-Shran\\"."
1128
+ `);
1129
+ });
1130
+
1131
+ it("should error on invalid `name` value with only special characters", () => {
1132
+ const expectedConfig: RawEnvironment = {
1133
+ name: "!@#$%^&*(()",
1134
+ } as unknown as RawEnvironment;
1135
+
1136
+ const { config, diagnostics } = normalizeAndValidateConfig(
1137
+ expectedConfig,
1138
+ undefined,
1139
+ { env: undefined }
1140
+ );
1141
+
1142
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
1143
+ expect(diagnostics.hasWarnings()).toBe(false);
1144
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1145
+ "Processing wrangler configuration:
1146
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got \\"!@#$%^&*(()\\"."
1147
+ `);
1148
+ });
1149
+ });
1150
+
1151
+ describe("[build]", () => {
1152
+ it("should override build.upload config defaults with provided values and warn about deprecations", () => {
1153
+ const expectedConfig: RawEnvironment = {
1154
+ build: {
1155
+ upload: {
1156
+ dir: "src",
1157
+ format: "modules",
1158
+ main: "index.ts",
1159
+ rules: [{ type: "Text", globs: ["GLOB"], fallthrough: true }],
1160
+ },
1161
+ },
1162
+ };
1163
+
1164
+ const { config, diagnostics } = normalizeAndValidateConfig(
1165
+ expectedConfig,
1166
+ path.resolve("project/wrangler.toml"),
1167
+ { env: undefined }
1168
+ );
1169
+
1170
+ expect(config.main).toEqual(path.resolve("project/src/index.ts"));
1171
+ expect(config.build.upload).toBeUndefined();
1172
+ expect(diagnostics.hasErrors()).toBe(false);
1173
+ expect(diagnostics.hasWarnings()).toBe(true);
1174
+ expect(normalizePath(diagnostics.renderWarnings()))
1175
+ .toMatchInlineSnapshot(`
1176
+ "Processing project/wrangler.toml configuration:
1177
+ - Deprecation: \\"build.upload.format\\":
1178
+ The format is inferred automatically from the code.
1179
+ - Deprecation: \\"build.upload.main\\":
1180
+ Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
1181
+ Then add the top level \`main\` field to your configuration file:
1182
+ \`\`\`
1183
+ main = \\"src/index.ts\\"
1184
+ \`\`\`
1185
+ - Deprecation: \\"build.upload.dir\\":
1186
+ Use the top level \\"main\\" field or a command-line argument to specify the entry-point for the Worker.
1187
+ - 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:
1188
+ \`\`\`
1189
+ [[rules]]
1190
+ type = \\"Text\\"
1191
+ globs = [ \\"GLOB\\" ]
1192
+ fallthrough = true
1193
+ \`\`\`"
1194
+ `);
1195
+ });
1196
+
1197
+ it("should default custom build watch directories to src", () => {
1198
+ const expectedConfig: RawEnvironment = {
1199
+ build: {
1200
+ command: "execute some --build",
1201
+ },
1202
+ };
1203
+
1204
+ const { config, diagnostics } = normalizeAndValidateConfig(
1205
+ expectedConfig,
1206
+ undefined,
1207
+ { env: undefined }
1208
+ );
1209
+
1210
+ expect(config.build).toEqual(
1211
+ expect.objectContaining({
1212
+ command: "execute some --build",
1213
+ watch_dir: "./src",
1214
+ })
1215
+ );
1216
+
1217
+ expect(diagnostics.hasErrors()).toBe(false);
1218
+ expect(diagnostics.hasWarnings()).toBe(false);
1219
+ });
1220
+
1221
+ it("should resolve custom build watch directories relative to wrangler.toml", async () => {
1222
+ const expectedConfig: RawEnvironment = {
1223
+ build: {
1224
+ command: "execute some --build",
1225
+ watch_dir: "some/path",
1226
+ },
1227
+ };
1228
+
1229
+ const { config, diagnostics } = normalizeAndValidateConfig(
1230
+ expectedConfig,
1231
+ "project/wrangler.toml",
1232
+ { env: undefined }
1233
+ );
1234
+
1235
+ expect(config.build).toEqual(
1236
+ expect.objectContaining({
1237
+ command: "execute some --build",
1238
+ watch_dir: path.normalize("project/some/path"),
1239
+ })
1240
+ );
1241
+
1242
+ expect(diagnostics.hasErrors()).toBe(false);
1243
+ expect(diagnostics.hasWarnings()).toBe(false);
1244
+ });
1245
+
1246
+ it("should allow watch_dir to be an array of paths", () => {
1247
+ const expectedConfig: RawEnvironment = {
1248
+ build: {
1249
+ command: "execute some --build",
1250
+ watch_dir: ["some/path/a", "some/path/b", "some/path/c"],
1251
+ },
1252
+ };
1253
+
1254
+ const { config, diagnostics } = normalizeAndValidateConfig(
1255
+ expectedConfig,
1256
+ "project/wrangler.toml",
1257
+ { env: undefined }
1258
+ );
1259
+
1260
+ expect(config.build).toEqual(
1261
+ expect.objectContaining({
1262
+ command: "execute some --build",
1263
+ watch_dir: [
1264
+ path.normalize("project/some/path/a"),
1265
+ path.normalize("project/some/path/b"),
1266
+ path.normalize("project/some/path/c"),
1267
+ ],
1268
+ })
1269
+ );
1270
+
1271
+ expect(diagnostics.hasErrors()).toBe(false);
1272
+ expect(diagnostics.hasWarnings()).toBe(false);
1273
+ });
1274
+
1275
+ it("should error when the watch_dir array isn't an array of strings", () => {
1276
+ const expectedConfig: RawEnvironment = {
1277
+ build: {
1278
+ command: "execute some --build",
1279
+ watch_dir: [
1280
+ "some/path/a",
1281
+ "some/path/b",
1282
+ // @ts-expect-error intentionally bad "paths"
1283
+ 123,
1284
+ "some/path/c",
1285
+ // @ts-expect-error intentionally bad "paths"
1286
+ false,
1287
+ ],
1288
+ },
1289
+ };
1290
+
1291
+ const { config, diagnostics } = normalizeAndValidateConfig(
1292
+ expectedConfig,
1293
+ "project/wrangler.toml",
1294
+ { env: undefined }
1295
+ );
1296
+
1297
+ expect(config.build).toEqual(
1298
+ expect.objectContaining({
1299
+ command: "execute some --build",
1300
+ watch_dir: [
1301
+ path.normalize("project/some/path/a"),
1302
+ path.normalize("project/some/path/b"),
1303
+ path.normalize("project/123"),
1304
+ path.normalize("project/some/path/c"),
1305
+ path.normalize("project/false"),
1306
+ ],
1307
+ })
1308
+ );
1309
+
1310
+ expect(diagnostics.hasWarnings()).toBe(false);
1311
+ expect(diagnostics.hasErrors()).toBe(true);
1312
+
1313
+ expect(normalizePath(diagnostics.renderErrors()))
1314
+ .toMatchInlineSnapshot(`
1315
+ "Processing project/wrangler.toml configuration:
1316
+ - Expected \\"build.watch_dir.[2]\\" to be of type string but got 123.
1317
+ - Expected \\"build.watch_dir.[4]\\" to be of type string but got false."
1318
+ `);
1319
+ });
1320
+ });
1321
+
1322
+ describe("[durable_objects]", () => {
1323
+ it("should error if durable_objects is an array", () => {
1324
+ const { diagnostics } = normalizeAndValidateConfig(
1325
+ { durable_objects: [] } as unknown as RawConfig,
1326
+ undefined,
1327
+ { env: undefined }
1328
+ );
1329
+
1330
+ expect(diagnostics.hasWarnings()).toBe(false);
1331
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1332
+ "Processing wrangler configuration:
1333
+ - The field \\"durable_objects\\" should be an object but got []."
1334
+ `);
1335
+ });
1336
+
1337
+ it("should error if durable_objects is a string", () => {
1338
+ const { diagnostics } = normalizeAndValidateConfig(
1339
+ { durable_objects: "BAD" } as unknown as RawConfig,
1340
+ undefined,
1341
+ { env: undefined }
1342
+ );
1343
+
1344
+ expect(diagnostics.hasWarnings()).toBe(false);
1345
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1346
+ "Processing wrangler configuration:
1347
+ - The field \\"durable_objects\\" should be an object but got \\"BAD\\"."
1348
+ `);
1349
+ });
1350
+
1351
+ it("should error if durable_objects is a number", () => {
1352
+ const { diagnostics } = normalizeAndValidateConfig(
1353
+ { durable_objects: 999 } as unknown as RawConfig,
1354
+ undefined,
1355
+ { env: undefined }
1356
+ );
1357
+
1358
+ expect(diagnostics.hasWarnings()).toBe(false);
1359
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1360
+ "Processing wrangler configuration:
1361
+ - The field \\"durable_objects\\" should be an object but got 999."
1362
+ `);
1363
+ });
1364
+
1365
+ it("should error if durable_objects is null", () => {
1366
+ const { diagnostics } = normalizeAndValidateConfig(
1367
+ { durable_objects: null } as unknown as RawConfig,
1368
+ undefined,
1369
+ { env: undefined }
1370
+ );
1371
+
1372
+ expect(diagnostics.hasWarnings()).toBe(false);
1373
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1374
+ "Processing wrangler configuration:
1375
+ - The field \\"durable_objects\\" should be an object but got null."
1376
+ `);
1377
+ });
1378
+
1379
+ it("should error if durable_objects.bindings is not defined", () => {
1380
+ const { diagnostics } = normalizeAndValidateConfig(
1381
+ { durable_objects: {} } as unknown as RawConfig,
1382
+ undefined,
1383
+ { env: undefined }
1384
+ );
1385
+
1386
+ expect(diagnostics.hasWarnings()).toBe(false);
1387
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1388
+ "Processing wrangler configuration:
1389
+ - The field \\"durable_objects\\" is missing the required \\"bindings\\" property."
1390
+ `);
1391
+ });
1392
+
1393
+ it("should error if durable_objects.bindings is an object", () => {
1394
+ const { diagnostics } = normalizeAndValidateConfig(
1395
+ { durable_objects: { bindings: {} } } as unknown as RawConfig,
1396
+ undefined,
1397
+ { env: undefined }
1398
+ );
1399
+
1400
+ expect(diagnostics.hasWarnings()).toBe(false);
1401
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1402
+ "Processing wrangler configuration:
1403
+ - The field \\"durable_objects.bindings\\" should be an array but got {}."
1404
+ `);
1405
+ });
1406
+
1407
+ it("should error if durable_objects.bindings is a string", () => {
1408
+ const { diagnostics } = normalizeAndValidateConfig(
1409
+ { durable_objects: { bindings: "BAD" } } as unknown as RawConfig,
1410
+ undefined,
1411
+ { env: undefined }
1412
+ );
1413
+
1414
+ expect(diagnostics.hasWarnings()).toBe(false);
1415
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1416
+ "Processing wrangler configuration:
1417
+ - The field \\"durable_objects.bindings\\" should be an array but got \\"BAD\\"."
1418
+ `);
1419
+ });
1420
+
1421
+ it("should error if durable_objects.bindings is a number", () => {
1422
+ const { diagnostics } = normalizeAndValidateConfig(
1423
+ { durable_objects: { bindings: 999 } } as unknown as RawConfig,
1424
+ undefined,
1425
+ { env: undefined }
1426
+ );
1427
+
1428
+ expect(diagnostics.hasWarnings()).toBe(false);
1429
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1430
+ "Processing wrangler configuration:
1431
+ - The field \\"durable_objects.bindings\\" should be an array but got 999."
1432
+ `);
1433
+ });
1434
+
1435
+ it("should error if durable_objects.bindings is null", () => {
1436
+ const { diagnostics } = normalizeAndValidateConfig(
1437
+ { durable_objects: { bindings: null } } as unknown as RawConfig,
1438
+ undefined,
1439
+ { env: undefined }
1440
+ );
1441
+
1442
+ expect(diagnostics.hasWarnings()).toBe(false);
1443
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1444
+ "Processing wrangler configuration:
1445
+ - The field \\"durable_objects.bindings\\" should be an array but got null."
1446
+ `);
1447
+ });
1448
+
1449
+ it("should error if durable_objects.bindings are not valid", () => {
1450
+ const { diagnostics } = normalizeAndValidateConfig(
1451
+ {
1452
+ durable_objects: {
1453
+ bindings: [
1454
+ {},
1455
+ { name: "MISSING_CLASS" },
1456
+ { name: 1555, class_name: 1666 },
1457
+ {
1458
+ name: 1777,
1459
+ class_name: 1888,
1460
+ script_name: 1999,
1461
+ },
1462
+ {
1463
+ name: "SOMENAME",
1464
+ class_name: "SomeClass",
1465
+ environment: "staging",
1466
+ },
1467
+ {
1468
+ name: 1778,
1469
+ class_name: 1889,
1470
+ script_name: 1992,
1471
+ environment: 2111,
1472
+ },
1473
+ {
1474
+ name: 1772,
1475
+ class_name: 1883,
1476
+ environment: 2112,
1477
+ },
1478
+ ],
1479
+ },
1480
+ } as unknown as RawConfig,
1481
+ undefined,
1482
+ { env: undefined }
1483
+ );
1484
+
1485
+ expect(diagnostics.hasWarnings()).toBe(false);
1486
+
1487
+ expect(diagnostics.hasErrors()).toBe(true);
1488
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1489
+ "Processing wrangler configuration:
1490
+
1491
+ - \\"durable_objects.bindings[0]\\": {}
1492
+ - binding should have a string \\"name\\" field.
1493
+ - binding should have a string \\"class_name\\" field.
1494
+
1495
+ - \\"durable_objects.bindings[1]\\": {\\"name\\":\\"MISSING_CLASS\\"}
1496
+ - binding should have a string \\"class_name\\" field.
1497
+
1498
+ - \\"durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
1499
+ - binding should have a string \\"name\\" field.
1500
+ - binding should have a string \\"class_name\\" field.
1501
+
1502
+ - \\"durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
1503
+ - binding should have a string \\"name\\" field.
1504
+ - binding should have a string \\"class_name\\" field.
1505
+ - the field \\"script_name\\", when present, should be a string.
1506
+
1507
+ - \\"durable_objects.bindings[4]\\": {\\"name\\":\\"SOMENAME\\",\\"class_name\\":\\"SomeClass\\",\\"environment\\":\\"staging\\"}
1508
+ - binding should have a \\"script_name\\" field if \\"environment\\" is present.
1509
+
1510
+ - \\"durable_objects.bindings[5]\\": {\\"name\\":1778,\\"class_name\\":1889,\\"script_name\\":1992,\\"environment\\":2111}
1511
+ - binding should have a string \\"name\\" field.
1512
+ - binding should have a string \\"class_name\\" field.
1513
+ - the field \\"script_name\\", when present, should be a string.
1514
+ - the field \\"environment\\", when present, should be a string.
1515
+
1516
+ - \\"durable_objects.bindings[6]\\": {\\"name\\":1772,\\"class_name\\":1883,\\"environment\\":2112}
1517
+ - binding should have a string \\"name\\" field.
1518
+ - binding should have a string \\"class_name\\" field.
1519
+ - the field \\"environment\\", when present, should be a string.
1520
+ - binding should have a \\"script_name\\" field if \\"environment\\" is present."
1521
+ `);
1522
+ });
1523
+ });
1524
+
1525
+ describe("[kv_namespaces]", () => {
1526
+ it("should error if kv_namespaces is an object", () => {
1527
+ const { diagnostics } = normalizeAndValidateConfig(
1528
+ { kv_namespaces: {} } as unknown as RawConfig,
1529
+ undefined,
1530
+ { env: undefined }
1531
+ );
1532
+
1533
+ expect(diagnostics.hasWarnings()).toBe(false);
1534
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1535
+ "Processing wrangler configuration:
1536
+ - The field \\"kv_namespaces\\" should be an array but got {}."
1537
+ `);
1538
+ });
1539
+
1540
+ it("should error if kv_namespaces is a string", () => {
1541
+ const { diagnostics } = normalizeAndValidateConfig(
1542
+ { kv_namespaces: "BAD" } as unknown as RawConfig,
1543
+ undefined,
1544
+ { env: undefined }
1545
+ );
1546
+
1547
+ expect(diagnostics.hasWarnings()).toBe(false);
1548
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1549
+ "Processing wrangler configuration:
1550
+ - The field \\"kv_namespaces\\" should be an array but got \\"BAD\\"."
1551
+ `);
1552
+ });
1553
+
1554
+ it("should error if kv_namespaces is a number", () => {
1555
+ const { diagnostics } = normalizeAndValidateConfig(
1556
+ { kv_namespaces: 999 } as unknown as RawConfig,
1557
+ undefined,
1558
+ { env: undefined }
1559
+ );
1560
+
1561
+ expect(diagnostics.hasWarnings()).toBe(false);
1562
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1563
+ "Processing wrangler configuration:
1564
+ - The field \\"kv_namespaces\\" should be an array but got 999."
1565
+ `);
1566
+ });
1567
+
1568
+ it("should error if kv_namespaces is null", () => {
1569
+ const { diagnostics } = normalizeAndValidateConfig(
1570
+ { kv_namespaces: null } as unknown as RawConfig,
1571
+ undefined,
1572
+ { env: undefined }
1573
+ );
1574
+
1575
+ expect(diagnostics.hasWarnings()).toBe(false);
1576
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1577
+ "Processing wrangler configuration:
1578
+ - The field \\"kv_namespaces\\" should be an array but got null."
1579
+ `);
1580
+ });
1581
+
1582
+ it("should error if kv_namespaces.bindings are not valid", () => {
1583
+ const { diagnostics } = normalizeAndValidateConfig(
1584
+ {
1585
+ kv_namespaces: [
1586
+ {},
1587
+ { binding: "VALID" },
1588
+ { binding: 2000, id: 2111 },
1589
+ {
1590
+ binding: "KV_BINDING_2",
1591
+ id: "KV_ID_2",
1592
+ preview_id: 2222,
1593
+ },
1594
+ { binding: "VALID", id: "" },
1595
+ ],
1596
+ } as unknown as RawConfig,
1597
+ undefined,
1598
+ { env: undefined }
1599
+ );
1600
+
1601
+ expect(diagnostics.hasWarnings()).toBe(false);
1602
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1603
+ "Processing wrangler configuration:
1604
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
1605
+ - \\"kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
1606
+ - \\"kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
1607
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1608
+ - \\"kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1609
+ - \\"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}.
1610
+ - \\"kv_namespaces[4]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\",\\"id\\":\\"\\"}."
1611
+ `);
1612
+ });
1613
+ });
1614
+
1615
+ describe("[d1_databases]", () => {
1616
+ it("should error if d1_databases is an object", () => {
1617
+ const { diagnostics } = normalizeAndValidateConfig(
1618
+ { d1_databases: {} } as unknown as RawConfig,
1619
+ undefined,
1620
+ { env: undefined }
1621
+ );
1622
+
1623
+ expect(diagnostics.hasWarnings()).toBe(false);
1624
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1625
+ "Processing wrangler configuration:
1626
+ - The field \\"d1_databases\\" should be an array but got {}."
1627
+ `);
1628
+ });
1629
+
1630
+ it("should error if d1_databases is a string", () => {
1631
+ const { diagnostics } = normalizeAndValidateConfig(
1632
+ { d1_databases: "BAD" } as unknown as RawConfig,
1633
+ undefined,
1634
+ { env: undefined }
1635
+ );
1636
+
1637
+ expect(diagnostics.hasWarnings()).toBe(false);
1638
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1639
+ "Processing wrangler configuration:
1640
+ - The field \\"d1_databases\\" should be an array but got \\"BAD\\"."
1641
+ `);
1642
+ });
1643
+
1644
+ it("should error if d1_databases is a number", () => {
1645
+ const { diagnostics } = normalizeAndValidateConfig(
1646
+ { d1_databases: 999 } as unknown as RawConfig,
1647
+ undefined,
1648
+ { env: undefined }
1649
+ );
1650
+
1651
+ expect(diagnostics.hasWarnings()).toBe(false);
1652
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1653
+ "Processing wrangler configuration:
1654
+ - The field \\"d1_databases\\" should be an array but got 999."
1655
+ `);
1656
+ });
1657
+
1658
+ it("should error if d1_databases is null", () => {
1659
+ const { diagnostics } = normalizeAndValidateConfig(
1660
+ { d1_databases: null } as unknown as RawConfig,
1661
+ undefined,
1662
+ { env: undefined }
1663
+ );
1664
+
1665
+ expect(diagnostics.hasWarnings()).toBe(false);
1666
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1667
+ "Processing wrangler configuration:
1668
+ - The field \\"d1_databases\\" should be an array but got null."
1669
+ `);
1670
+ });
1671
+
1672
+ it("should error if d1_databases.bindings are not valid", () => {
1673
+ const { diagnostics } = normalizeAndValidateConfig(
1674
+ {
1675
+ d1_databases: [
1676
+ {},
1677
+ { binding: "VALID" },
1678
+ { binding: 2000, id: 2111 },
1679
+ {
1680
+ binding: "D1_BINDING_2",
1681
+ id: "my-db",
1682
+ preview_id: 2222,
1683
+ },
1684
+ { binding: "VALID", id: "" },
1685
+ ],
1686
+ } as unknown as RawConfig,
1687
+ undefined,
1688
+ { env: undefined }
1689
+ );
1690
+
1691
+ expect(diagnostics.hasWarnings()).toBe(false);
1692
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1693
+ "Processing wrangler configuration:
1694
+ - \\"d1_databases[0]\\" bindings should have a string \\"binding\\" field but got {}.
1695
+ - \\"d1_databases[0]\\" bindings must have a \\"database_id\\" field but got {}.
1696
+ - \\"d1_databases[1]\\" bindings must have a \\"database_id\\" field but got {\\"binding\\":\\"VALID\\"}.
1697
+ - \\"d1_databases[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1698
+ - \\"d1_databases[2]\\" bindings must have a \\"database_id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
1699
+ - \\"d1_databases[3]\\" bindings must have a \\"database_id\\" field but got {\\"binding\\":\\"D1_BINDING_2\\",\\"id\\":\\"my-db\\",\\"preview_id\\":2222}.
1700
+ - \\"d1_databases[4]\\" bindings must have a \\"database_id\\" field but got {\\"binding\\":\\"VALID\\",\\"id\\":\\"\\"}."
1701
+ `);
1702
+ });
1703
+ });
1704
+
1705
+ describe("[queues]", () => {
1706
+ it("should error if queues is not an object", () => {
1707
+ const { config, diagnostics } = normalizeAndValidateConfig(
1708
+ { queues: [] } as unknown as RawConfig,
1709
+ undefined,
1710
+ { env: undefined }
1711
+ );
1712
+
1713
+ expect(config).toEqual(
1714
+ expect.not.objectContaining({ queues: expect.anything })
1715
+ );
1716
+ expect(diagnostics.hasWarnings()).toBe(false);
1717
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1718
+ "Processing wrangler configuration:
1719
+ - The field \\"queues\\" should be an object but got []."
1720
+ `);
1721
+ });
1722
+
1723
+ it("should error if queues producer bindings are not valid", () => {
1724
+ const { config, diagnostics } = normalizeAndValidateConfig(
1725
+ {
1726
+ queues: {
1727
+ invalidField: "madeupValue",
1728
+ producers: [
1729
+ {},
1730
+ { binding: "QUEUE_BINDING_1" },
1731
+ { binding: 2333, queue: 2444 },
1732
+ { binding: "QUEUE_BINDING_3", queue: "" },
1733
+ ],
1734
+ },
1735
+ } as unknown as RawConfig,
1736
+ undefined,
1737
+ { env: undefined }
1738
+ );
1739
+
1740
+ expect(config).toEqual(
1741
+ expect.not.objectContaining({
1742
+ queues: { producers: expect.anything },
1743
+ })
1744
+ );
1745
+ expect(diagnostics.hasWarnings()).toBe(true);
1746
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1747
+ "Processing wrangler configuration:
1748
+ - Unexpected fields found in queues field: \\"invalidField\\""
1749
+ `);
1750
+
1751
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1752
+ "Processing wrangler configuration:
1753
+ - \\"queues.producers[0]\\" bindings should have a string \\"binding\\" field but got {}.
1754
+ - \\"queues.producers[0]\\" bindings should have a string \\"queue\\" field but got {}.
1755
+ - \\"queues.producers[1]\\" bindings should have a string \\"queue\\" field but got {\\"binding\\":\\"QUEUE_BINDING_1\\"}.
1756
+ - \\"queues.producers[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"queue\\":2444}.
1757
+ - \\"queues.producers[2]\\" bindings should have a string \\"queue\\" field but got {\\"binding\\":2333,\\"queue\\":2444}.
1758
+ - \\"queues.producers[3]\\" bindings should have a string \\"queue\\" field but got {\\"binding\\":\\"QUEUE_BINDING_3\\",\\"queue\\":\\"\\"}."
1759
+ `);
1760
+ });
1761
+
1762
+ it("should error if queues consumers are not valid", () => {
1763
+ const { config, diagnostics } = normalizeAndValidateConfig(
1764
+ {
1765
+ queues: {
1766
+ invalidField: "madeupValue",
1767
+ consumers: [
1768
+ {},
1769
+ { queue: 22 },
1770
+ { queue: "myQueue", invalidField: "madeupValue" },
1771
+ {
1772
+ queue: "myQueue",
1773
+ max_batch_size: "3",
1774
+ max_batch_timeout: null,
1775
+ max_retries: "hello",
1776
+ dead_letter_queue: 5,
1777
+ },
1778
+ ],
1779
+ },
1780
+ } as unknown as RawConfig,
1781
+ undefined,
1782
+ { env: undefined }
1783
+ );
1784
+
1785
+ expect(config).toEqual(
1786
+ expect.not.objectContaining({
1787
+ queues: { producers: expect.anything },
1788
+ })
1789
+ );
1790
+ expect(diagnostics.hasWarnings()).toBe(true);
1791
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1792
+ "Processing wrangler configuration:
1793
+ - Unexpected fields found in queues field: \\"invalidField\\"
1794
+ - Unexpected fields found in queues.consumers[2] field: \\"invalidField\\""
1795
+ `);
1796
+
1797
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1798
+ "Processing wrangler configuration:
1799
+ - \\"queues.consumers[0]\\" should have a string \\"queue\\" field but got {}.
1800
+ - \\"queues.consumers[1]\\" should have a string \\"queue\\" field but got {\\"queue\\":22}.
1801
+ - \\"queues.consumers[3]\\" should, optionally, have a number \\"max_batch_size\\" field but got {\\"queue\\":\\"myQueue\\",\\"max_batch_size\\":\\"3\\",\\"max_batch_timeout\\":null,\\"max_retries\\":\\"hello\\",\\"dead_letter_queue\\":5}.
1802
+ - \\"queues.consumers[3]\\" should, optionally, have a number \\"max_batch_timeout\\" field but got {\\"queue\\":\\"myQueue\\",\\"max_batch_size\\":\\"3\\",\\"max_batch_timeout\\":null,\\"max_retries\\":\\"hello\\",\\"dead_letter_queue\\":5}.
1803
+ - \\"queues.consumers[3]\\" should, optionally, have a number \\"max_retries\\" field but got {\\"queue\\":\\"myQueue\\",\\"max_batch_size\\":\\"3\\",\\"max_batch_timeout\\":null,\\"max_retries\\":\\"hello\\",\\"dead_letter_queue\\":5}.
1804
+ - \\"queues.consumers[3]\\" should, optionally, have a string \\"dead_letter_queue\\" field but got {\\"queue\\":\\"myQueue\\",\\"max_batch_size\\":\\"3\\",\\"max_batch_timeout\\":null,\\"max_retries\\":\\"hello\\",\\"dead_letter_queue\\":5}."
1805
+ `);
1806
+ });
1807
+ });
1808
+
1809
+ describe("[r2_buckets]", () => {
1810
+ it("should error if r2_buckets is an object", () => {
1811
+ const { diagnostics } = normalizeAndValidateConfig(
1812
+ { r2_buckets: {} } as unknown as RawConfig,
1813
+ undefined,
1814
+ { env: undefined }
1815
+ );
1816
+
1817
+ expect(diagnostics.hasWarnings()).toBe(false);
1818
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1819
+ "Processing wrangler configuration:
1820
+ - The field \\"r2_buckets\\" should be an array but got {}."
1821
+ `);
1822
+ });
1823
+
1824
+ it("should error if r2_buckets is a string", () => {
1825
+ const { diagnostics } = normalizeAndValidateConfig(
1826
+ { r2_buckets: "BAD" } as unknown as RawConfig,
1827
+ undefined,
1828
+ { env: undefined }
1829
+ );
1830
+
1831
+ expect(diagnostics.hasWarnings()).toBe(false);
1832
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1833
+ "Processing wrangler configuration:
1834
+ - The field \\"r2_buckets\\" should be an array but got \\"BAD\\"."
1835
+ `);
1836
+ });
1837
+
1838
+ it("should error if r2_buckets is a number", () => {
1839
+ const { diagnostics } = normalizeAndValidateConfig(
1840
+ { r2_buckets: 999 } as unknown as RawConfig,
1841
+ undefined,
1842
+ { env: undefined }
1843
+ );
1844
+
1845
+ expect(diagnostics.hasWarnings()).toBe(false);
1846
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1847
+ "Processing wrangler configuration:
1848
+ - The field \\"r2_buckets\\" should be an array but got 999."
1849
+ `);
1850
+ });
1851
+
1852
+ it("should error if r2_buckets is null", () => {
1853
+ const { diagnostics } = normalizeAndValidateConfig(
1854
+ { r2_buckets: null } as unknown as RawConfig,
1855
+ undefined,
1856
+ { env: undefined }
1857
+ );
1858
+
1859
+ expect(diagnostics.hasWarnings()).toBe(false);
1860
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1861
+ "Processing wrangler configuration:
1862
+ - The field \\"r2_buckets\\" should be an array but got null."
1863
+ `);
1864
+ });
1865
+
1866
+ it("should error if r2_buckets.bindings are not valid", () => {
1867
+ const { diagnostics } = normalizeAndValidateConfig(
1868
+ {
1869
+ r2_buckets: [
1870
+ {},
1871
+ { binding: "R2_BINDING_1" },
1872
+ { binding: 2333, bucket_name: 2444 },
1873
+ {
1874
+ binding: "R2_BINDING_2",
1875
+ bucket_name: "R2_BUCKET_2",
1876
+ preview_bucket_name: 2555,
1877
+ },
1878
+ { binding: "R2_BINDING_1", bucket_name: "" },
1879
+ ],
1880
+ } as unknown as RawConfig,
1881
+ undefined,
1882
+ { env: undefined }
1883
+ );
1884
+
1885
+ expect(diagnostics.hasWarnings()).toBe(false);
1886
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1887
+ "Processing wrangler configuration:
1888
+ - \\"r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
1889
+ - \\"r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
1890
+ - \\"r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
1891
+ - \\"r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1892
+ - \\"r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
1893
+ - \\"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}.
1894
+ - \\"r2_buckets[4]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\",\\"bucket_name\\":\\"\\"}."
1895
+ `);
1896
+ });
1897
+ });
1898
+
1899
+ describe("[services]", () => {
1900
+ it("should error if services is an object", () => {
1901
+ const { diagnostics } = normalizeAndValidateConfig(
1902
+ { services: {} } as unknown as RawConfig,
1903
+ undefined,
1904
+ { env: undefined }
1905
+ );
1906
+
1907
+ expect(diagnostics.hasWarnings()).toBe(true);
1908
+ expect(diagnostics.hasErrors()).toBe(true);
1909
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1910
+ "Processing wrangler configuration:
1911
+ - \\"services\\" fields are experimental and may change or break at any time."
1912
+ `);
1913
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1914
+ "Processing wrangler configuration:
1915
+ - The field \\"services\\" should be an array but got {}."
1916
+ `);
1917
+ });
1918
+
1919
+ it("should error if services is a string", () => {
1920
+ const { diagnostics } = normalizeAndValidateConfig(
1921
+ { services: "BAD" } as unknown as RawConfig,
1922
+ undefined,
1923
+ { env: undefined }
1924
+ );
1925
+
1926
+ expect(diagnostics.hasWarnings()).toBe(true);
1927
+ expect(diagnostics.hasErrors()).toBe(true);
1928
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1929
+ "Processing wrangler configuration:
1930
+ - \\"services\\" fields are experimental and may change or break at any time."
1931
+ `);
1932
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1933
+ "Processing wrangler configuration:
1934
+ - The field \\"services\\" should be an array but got \\"BAD\\"."
1935
+ `);
1936
+ });
1937
+
1938
+ it("should error if services is a number", () => {
1939
+ const { diagnostics } = normalizeAndValidateConfig(
1940
+ { services: 999 } as unknown as RawConfig,
1941
+ undefined,
1942
+ { env: undefined }
1943
+ );
1944
+
1945
+ expect(diagnostics.hasWarnings()).toBe(true);
1946
+ expect(diagnostics.hasErrors()).toBe(true);
1947
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1948
+ "Processing wrangler configuration:
1949
+ - \\"services\\" fields are experimental and may change or break at any time."
1950
+ `);
1951
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1952
+ "Processing wrangler configuration:
1953
+ - The field \\"services\\" should be an array but got 999."
1954
+ `);
1955
+ });
1956
+
1957
+ it("should error if services is null", () => {
1958
+ const { diagnostics } = normalizeAndValidateConfig(
1959
+ { services: null } as unknown as RawConfig,
1960
+ undefined,
1961
+ { env: undefined }
1962
+ );
1963
+
1964
+ expect(diagnostics.hasWarnings()).toBe(true);
1965
+ expect(diagnostics.hasErrors()).toBe(true);
1966
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
1967
+ "Processing wrangler configuration:
1968
+ - \\"services\\" fields are experimental and may change or break at any time."
1969
+ `);
1970
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1971
+ "Processing wrangler configuration:
1972
+ - The field \\"services\\" should be an array but got null."
1973
+ `);
1974
+ });
1975
+
1976
+ it("should error if services bindings are not valid", () => {
1977
+ const { diagnostics } = normalizeAndValidateConfig(
1978
+ {
1979
+ services: [
1980
+ {},
1981
+ { binding: "SERVICE_BINDING_1" },
1982
+ { binding: 123, service: 456 },
1983
+ { binding: 123, service: 456, environment: 789 },
1984
+ { binding: "SERVICE_BINDING_1", service: 456, environment: 789 },
1985
+ {
1986
+ binding: 123,
1987
+ service: "SERVICE_BINDING_SERVICE_1",
1988
+ environment: 789,
1989
+ },
1990
+ {
1991
+ binding: 123,
1992
+ service: 456,
1993
+ environment: "SERVICE_BINDING_ENVIRONMENT_1",
1994
+ },
1995
+ ],
1996
+ } as unknown as RawConfig,
1997
+ undefined,
1998
+ { env: undefined }
1999
+ );
2000
+
2001
+ expect(diagnostics.hasWarnings()).toBe(true);
2002
+ expect(diagnostics.hasErrors()).toBe(true);
2003
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2004
+ "Processing wrangler configuration:
2005
+ - \\"services\\" fields are experimental and may change or break at any time."
2006
+ `);
2007
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2008
+ "Processing wrangler configuration:
2009
+ - \\"services[0]\\" bindings should have a string \\"binding\\" field but got {}.
2010
+ - \\"services[0]\\" bindings should have a string \\"service\\" field but got {}.
2011
+ - \\"services[1]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\"}.
2012
+ - \\"services[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456}.
2013
+ - \\"services[2]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456}.
2014
+ - \\"services[3]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
2015
+ - \\"services[3]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
2016
+ - \\"services[3]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":789}.
2017
+ - \\"services[4]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\",\\"service\\":456,\\"environment\\":789}.
2018
+ - \\"services[4]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":\\"SERVICE_BINDING_1\\",\\"service\\":456,\\"environment\\":789}.
2019
+ - \\"services[5]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":\\"SERVICE_BINDING_SERVICE_1\\",\\"environment\\":789}.
2020
+ - \\"services[5]\\" bindings should have a string \\"environment\\" field but got {\\"binding\\":123,\\"service\\":\\"SERVICE_BINDING_SERVICE_1\\",\\"environment\\":789}.
2021
+ - \\"services[6]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":\\"SERVICE_BINDING_ENVIRONMENT_1\\"}.
2022
+ - \\"services[6]\\" bindings should have a string \\"service\\" field but got {\\"binding\\":123,\\"service\\":456,\\"environment\\":\\"SERVICE_BINDING_ENVIRONMENT_1\\"}."
2023
+ `);
2024
+ });
2025
+ });
2026
+
2027
+ describe("[dispatch_namespaces]", () => {
2028
+ it("should log an experimental warning when dispatch_namespaces is used", () => {
2029
+ const { diagnostics } = normalizeAndValidateConfig(
2030
+ {
2031
+ dispatch_namespaces: [
2032
+ {
2033
+ binding: "BINDING_1",
2034
+ namespace: "NAMESPACE_1",
2035
+ },
2036
+ ],
2037
+ } as unknown as RawConfig,
2038
+ undefined,
2039
+ { env: undefined }
2040
+ );
2041
+ expect(diagnostics.hasWarnings()).toBe(true);
2042
+ expect(diagnostics.hasErrors()).toBe(false);
2043
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2044
+ "Processing wrangler configuration:
2045
+ - \\"dispatch_namespaces\\" fields are experimental and may change or break at any time."
2046
+ `);
2047
+ });
2048
+
2049
+ it("should error if dispatch_namespaces is not an array", () => {
2050
+ const { diagnostics } = normalizeAndValidateConfig(
2051
+ {
2052
+ dispatch_namespaces: "just a string",
2053
+ } as unknown as RawConfig,
2054
+ undefined,
2055
+ { env: undefined }
2056
+ );
2057
+
2058
+ expect(diagnostics.hasWarnings()).toBe(true);
2059
+ expect(diagnostics.hasErrors()).toBe(true);
2060
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2061
+ "Processing wrangler configuration:
2062
+ - \\"dispatch_namespaces\\" fields are experimental and may change or break at any time."
2063
+ `);
2064
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2065
+ "Processing wrangler configuration:
2066
+ - The field \\"dispatch_namespaces\\" should be an array but got \\"just a string\\"."
2067
+ `);
2068
+ });
2069
+
2070
+ it("should error on non valid dispatch_namespaces", () => {
2071
+ const { diagnostics } = normalizeAndValidateConfig(
2072
+ {
2073
+ dispatch_namespaces: [
2074
+ "a string",
2075
+ 123,
2076
+ {
2077
+ binding: 123,
2078
+ namespace: 456,
2079
+ },
2080
+ {
2081
+ binding: "DISPATCH_NAMESPACE_BINDING_1",
2082
+ namespace: 456,
2083
+ },
2084
+ // this one is valid
2085
+ {
2086
+ binding: "DISPATCH_NAMESPACE_BINDING_1",
2087
+ namespace: "DISPATCH_NAMESPACE_BINDING_NAMESPACE_1",
2088
+ },
2089
+ {
2090
+ binding: 123,
2091
+ namespace: "DISPATCH_NAMESPACE_BINDING_SERVICE_1",
2092
+ },
2093
+ {
2094
+ binding: 123,
2095
+ service: 456,
2096
+ },
2097
+ ],
2098
+ } as unknown as RawConfig,
2099
+ undefined,
2100
+ { env: undefined }
2101
+ );
2102
+ expect(diagnostics.hasWarnings()).toBe(true);
2103
+ expect(diagnostics.hasErrors()).toBe(true);
2104
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2105
+ "Processing wrangler configuration:
2106
+ - \\"dispatch_namespaces\\" fields are experimental and may change or break at any time."
2107
+ `);
2108
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2109
+ "Processing wrangler configuration:
2110
+ - \\"dispatch_namespaces[0]\\" binding should be objects, but got \\"a string\\"
2111
+ - \\"dispatch_namespaces[1]\\" binding should be objects, but got 123
2112
+ - \\"dispatch_namespaces[2]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"namespace\\":456}.
2113
+ - \\"dispatch_namespaces[2]\\" should have a string \\"namespace\\" field but got {\\"binding\\":123,\\"namespace\\":456}.
2114
+ - \\"dispatch_namespaces[3]\\" should have a string \\"namespace\\" field but got {\\"binding\\":\\"DISPATCH_NAMESPACE_BINDING_1\\",\\"namespace\\":456}.
2115
+ - \\"dispatch_namespaces[5]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"namespace\\":\\"DISPATCH_NAMESPACE_BINDING_SERVICE_1\\"}.
2116
+ - \\"dispatch_namespaces[6]\\" should have a string \\"binding\\" field but got {\\"binding\\":123,\\"service\\":456}.
2117
+ - \\"dispatch_namespaces[6]\\" should have a string \\"namespace\\" field but got {\\"binding\\":123,\\"service\\":456}."
2118
+ `);
2119
+ });
2120
+ });
2121
+
2122
+ describe("[unsafe.bindings]", () => {
2123
+ it("should error if unsafe is an array", () => {
2124
+ const { diagnostics } = normalizeAndValidateConfig(
2125
+ { unsafe: [] } as unknown as RawConfig,
2126
+ undefined,
2127
+ { env: undefined }
2128
+ );
2129
+
2130
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2131
+ "Processing wrangler configuration:
2132
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2133
+ `);
2134
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2135
+ "Processing wrangler configuration:
2136
+ - The field \\"unsafe\\" should be an object but got []."
2137
+ `);
2138
+ });
2139
+
2140
+ it("should error if unsafe is a string", () => {
2141
+ const { diagnostics } = normalizeAndValidateConfig(
2142
+ { unsafe: "BAD" } as unknown as RawConfig,
2143
+ undefined,
2144
+ { env: undefined }
2145
+ );
2146
+
2147
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2148
+ "Processing wrangler configuration:
2149
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2150
+ `);
2151
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2152
+ "Processing wrangler configuration:
2153
+ - The field \\"unsafe\\" should be an object but got \\"BAD\\"."
2154
+ `);
2155
+ });
2156
+
2157
+ it("should error if unsafe is a number", () => {
2158
+ const { diagnostics } = normalizeAndValidateConfig(
2159
+ { unsafe: 999 } as unknown as RawConfig,
2160
+ undefined,
2161
+ { env: undefined }
2162
+ );
2163
+
2164
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2165
+ "Processing wrangler configuration:
2166
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2167
+ `);
2168
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2169
+ "Processing wrangler configuration:
2170
+ - The field \\"unsafe\\" should be an object but got 999."
2171
+ `);
2172
+ });
2173
+
2174
+ it("should error if unsafe is null", () => {
2175
+ const { diagnostics } = normalizeAndValidateConfig(
2176
+ { unsafe: null } as unknown as RawConfig,
2177
+ undefined,
2178
+ { env: undefined }
2179
+ );
2180
+
2181
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2182
+ "Processing wrangler configuration:
2183
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2184
+ `);
2185
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2186
+ "Processing wrangler configuration:
2187
+ - The field \\"unsafe\\" should be an object but got null."
2188
+ `);
2189
+ });
2190
+
2191
+ it("should error if unsafe.bindings is not defined", () => {
2192
+ const { diagnostics } = normalizeAndValidateConfig(
2193
+ { unsafe: {} } as unknown as RawConfig,
2194
+ undefined,
2195
+ { env: undefined }
2196
+ );
2197
+
2198
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2199
+ "Processing wrangler configuration:
2200
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2201
+ `);
2202
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2203
+ "Processing wrangler configuration:
2204
+ - The field \\"unsafe\\" is missing the required \\"bindings\\" property."
2205
+ `);
2206
+ });
2207
+
2208
+ it("should error if unsafe.bindings is an object", () => {
2209
+ const { diagnostics } = normalizeAndValidateConfig(
2210
+ { unsafe: { bindings: {} } } as unknown as RawConfig,
2211
+ undefined,
2212
+ { env: undefined }
2213
+ );
2214
+
2215
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2216
+ "Processing wrangler configuration:
2217
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2218
+ `);
2219
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2220
+ "Processing wrangler configuration:
2221
+ - The field \\"unsafe.bindings\\" should be an array but got {}."
2222
+ `);
2223
+ });
2224
+
2225
+ it("should error if unsafe.bindings is a string", () => {
2226
+ const { diagnostics } = normalizeAndValidateConfig(
2227
+ { unsafe: { bindings: "BAD" } } as unknown as RawConfig,
2228
+ undefined,
2229
+ { env: undefined }
2230
+ );
2231
+
2232
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2233
+ "Processing wrangler configuration:
2234
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2235
+ `);
2236
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2237
+ "Processing wrangler configuration:
2238
+ - The field \\"unsafe.bindings\\" should be an array but got \\"BAD\\"."
2239
+ `);
2240
+ });
2241
+
2242
+ it("should error if unsafe.bindings is a number", () => {
2243
+ const { diagnostics } = normalizeAndValidateConfig(
2244
+ { unsafe: { bindings: 999 } } as unknown as RawConfig,
2245
+ undefined,
2246
+ { env: undefined }
2247
+ );
2248
+
2249
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2250
+ "Processing wrangler configuration:
2251
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2252
+ `);
2253
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2254
+ "Processing wrangler configuration:
2255
+ - The field \\"unsafe.bindings\\" should be an array but got 999."
2256
+ `);
2257
+ });
2258
+
2259
+ it("should error if unsafe.bindings is null", () => {
2260
+ const { diagnostics } = normalizeAndValidateConfig(
2261
+ { unsafe: { bindings: null } } as unknown as RawConfig,
2262
+ undefined,
2263
+ { env: undefined }
2264
+ );
2265
+
2266
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2267
+ "Processing wrangler configuration:
2268
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2269
+ `);
2270
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2271
+ "Processing wrangler configuration:
2272
+ - The field \\"unsafe.bindings\\" should be an array but got null."
2273
+ `);
2274
+ });
2275
+
2276
+ it("should error if durable_objects.bindings are not valid", () => {
2277
+ const { diagnostics } = normalizeAndValidateConfig(
2278
+ {
2279
+ unsafe: {
2280
+ bindings: [
2281
+ {},
2282
+ { name: "UNSAFE_BINDING_1" },
2283
+ { name: 2666, type: 2777 },
2284
+ {
2285
+ name: "UNSAFE_BINDING_2",
2286
+ type: "UNSAFE_TYPE_2",
2287
+ extra: 2888,
2288
+ },
2289
+ ],
2290
+ },
2291
+ } as unknown as RawConfig,
2292
+ undefined,
2293
+ { env: undefined }
2294
+ );
2295
+
2296
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2297
+ "Processing wrangler configuration:
2298
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
2299
+ `);
2300
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2301
+ "Processing wrangler configuration:
2302
+
2303
+ - \\"unsafe.bindings[0]\\": {}
2304
+ - binding should have a string \\"name\\" field.
2305
+ - binding should have a string \\"type\\" field.
2306
+
2307
+ - \\"unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
2308
+ - binding should have a string \\"type\\" field.
2309
+
2310
+ - \\"unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
2311
+ - binding should have a string \\"name\\" field.
2312
+ - binding should have a string \\"type\\" field."
2313
+ `);
2314
+ });
2315
+ });
2316
+
2317
+ describe("(deprecated)", () => {
2318
+ it("should remove and warn about deprecated properties", () => {
2319
+ const rawConfig: RawConfig = {
2320
+ zone_id: "ZONE_ID",
2321
+ experimental_services: [
2322
+ {
2323
+ name: "mock-name",
2324
+ service: "SERVICE",
2325
+ environment: "ENV",
2326
+ },
2327
+ ],
2328
+ };
2329
+
2330
+ const { config, diagnostics } = normalizeAndValidateConfig(
2331
+ rawConfig,
2332
+ undefined,
2333
+ { env: undefined }
2334
+ );
2335
+
2336
+ expect("experimental_services" in config).toBe(false);
2337
+ // Zone is not removed yet, since `route` commands might use it
2338
+ expect(config.zone_id).toEqual("ZONE_ID");
2339
+ expect(diagnostics.hasErrors()).toBe(false);
2340
+ expect(diagnostics.hasWarnings()).toBe(true);
2341
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2342
+ "Processing wrangler configuration:
2343
+ - Deprecation: \\"zone_id\\":
2344
+ This is unnecessary since we can deduce this from routes directly.
2345
+ - Deprecation: \\"experimental_services\\":
2346
+ The \\"experimental_services\\" field is no longer supported. Simply rename the [experimental_services] field to [services]."
2347
+ `);
2348
+ });
2349
+ });
2350
+
2351
+ describe("route & routes fields", () => {
2352
+ it("should error if both route and routes are specified", () => {
2353
+ const rawConfig: RawConfig = {
2354
+ route: "route1",
2355
+ routes: ["route2", "route3"],
2356
+ };
2357
+
2358
+ const { diagnostics } = normalizeAndValidateConfig(
2359
+ rawConfig,
2360
+ undefined,
2361
+ { env: undefined }
2362
+ );
2363
+
2364
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2365
+ "Processing wrangler configuration:
2366
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
2367
+ `);
2368
+ });
2369
+ });
2370
+ });
2371
+
2372
+ describe("named environments", () => {
2373
+ it("should warn if we specify an environment but there are no named environments", () => {
2374
+ const rawConfig: RawConfig = {};
2375
+ const { diagnostics } = normalizeAndValidateConfig(rawConfig, undefined, {
2376
+ env: "DEV",
2377
+ });
2378
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2379
+ "Processing wrangler configuration:
2380
+ "
2381
+ `);
2382
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2383
+ "Processing wrangler configuration:
2384
+ - No environment found in configuration with name \\"DEV\\".
2385
+ Before using \`--env=DEV\` there should be an equivalent environment section in the configuration.
2386
+
2387
+ Consider adding an environment configuration section to the wrangler.toml file:
2388
+ \`\`\`
2389
+ [env.DEV]
2390
+ \`\`\`
2391
+ "
2392
+ `);
2393
+ });
2394
+
2395
+ it("should error if we specify an environment that does not match the named environments", () => {
2396
+ const rawConfig: RawConfig = { env: { ENV1: {} } };
2397
+ const { diagnostics } = normalizeAndValidateConfig(rawConfig, undefined, {
2398
+ env: "DEV",
2399
+ });
2400
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2401
+ "Processing wrangler configuration:
2402
+ - No environment found in configuration with name \\"DEV\\".
2403
+ Before using \`--env=DEV\` there should be an equivalent environment section in the configuration.
2404
+ The available configured environment names are: [\\"ENV1\\"]
2405
+
2406
+ Consider adding an environment configuration section to the wrangler.toml file:
2407
+ \`\`\`
2408
+ [env.DEV]
2409
+ \`\`\`
2410
+ "
2411
+ `);
2412
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2413
+ "Processing wrangler configuration:
2414
+ "
2415
+ `);
2416
+ });
2417
+
2418
+ it("should use top-level values for inheritable config fields", () => {
2419
+ const main = "src/index.ts";
2420
+ const resolvedMain = path.resolve(process.cwd(), main);
2421
+ const rawConfig: RawConfig = {
2422
+ name: "mock-name",
2423
+ account_id: "ACCOUNT_ID",
2424
+ compatibility_date: "2022-01-01",
2425
+ compatibility_flags: ["FLAG1", "FLAG2"],
2426
+ workers_dev: false,
2427
+ routes: ["ROUTE_1", "ROUTE_2"],
2428
+ jsx_factory: "JSX_FACTORY",
2429
+ jsx_fragment: "JSX_FRAGMENT",
2430
+ tsconfig: "path/to/tsconfig.json",
2431
+ triggers: { crons: ["CRON_1", "CRON_2"] },
2432
+ usage_model: "bundled",
2433
+ main,
2434
+ build: {
2435
+ command: "COMMAND",
2436
+ cwd: "CWD",
2437
+ watch_dir: "WATCH_DIR",
2438
+ },
2439
+ no_bundle: true,
2440
+ minify: true,
2441
+ node_compat: true,
2442
+ first_party_worker: true,
2443
+ };
2444
+
2445
+ const { config, diagnostics } = normalizeAndValidateConfig(
2446
+ { ...rawConfig, env: { dev: {} } },
2447
+ undefined,
2448
+ { env: "dev" }
2449
+ );
2450
+
2451
+ expect(config).toEqual(
2452
+ expect.objectContaining({
2453
+ ...rawConfig,
2454
+ main: resolvedMain,
2455
+ name: "mock-name-dev",
2456
+ })
2457
+ );
2458
+ expect(diagnostics.hasErrors()).toBe(false);
2459
+ expect(diagnostics.hasWarnings()).toBe(false);
2460
+ });
2461
+
2462
+ it("should override top-level values for inheritable config fields", () => {
2463
+ const main = "src/index.ts";
2464
+ const resolvedMain = path.resolve(process.cwd(), main);
2465
+ const rawEnv: RawEnvironment = {
2466
+ name: "mock-env-name",
2467
+ account_id: "ENV_ACCOUNT_ID",
2468
+ compatibility_date: "2022-02-02",
2469
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
2470
+ workers_dev: true,
2471
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
2472
+ jsx_factory: "ENV_JSX_FACTORY",
2473
+ jsx_fragment: "ENV_JSX_FRAGMENT",
2474
+ tsconfig: "ENV_path/to/tsconfig.json",
2475
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
2476
+ usage_model: "unbound",
2477
+ main,
2478
+ build: {
2479
+ command: "ENV_COMMAND",
2480
+ cwd: "ENV_CWD",
2481
+ watch_dir: "ENV_WATCH_DIR",
2482
+ },
2483
+ no_bundle: false,
2484
+ minify: false,
2485
+ node_compat: false,
2486
+ first_party_worker: false,
2487
+ };
2488
+ const rawConfig: RawConfig = {
2489
+ name: "mock-name",
2490
+ account_id: "ACCOUNT_ID",
2491
+ compatibility_date: "2022-01-01",
2492
+ compatibility_flags: ["FLAG1", "FLAG2"],
2493
+ workers_dev: false,
2494
+ routes: ["ROUTE_1", "ROUTE_2"],
2495
+ jsx_factory: "JSX_FACTORY",
2496
+ jsx_fragment: "JSX_FRAGMENT",
2497
+ tsconfig: "path/to/tsconfig.json",
2498
+ triggers: { crons: ["CRON_1", "CRON_2"] },
2499
+ usage_model: "bundled",
2500
+ main: "top-level.js",
2501
+ build: {
2502
+ command: "COMMAND",
2503
+ cwd: "CWD",
2504
+ watch_dir: "WATCH_DIR",
2505
+ },
2506
+ no_bundle: true,
2507
+ minify: true,
2508
+ node_compat: true,
2509
+ first_party_worker: true,
2510
+ env: {
2511
+ ENV1: rawEnv,
2512
+ },
2513
+ };
2514
+
2515
+ const { config, diagnostics } = normalizeAndValidateConfig(
2516
+ rawConfig,
2517
+ undefined,
2518
+ { env: "ENV1" }
2519
+ );
2520
+
2521
+ expect(config).toEqual(
2522
+ expect.objectContaining({ ...rawEnv, main: resolvedMain })
2523
+ );
2524
+ expect(diagnostics.hasErrors()).toBe(false);
2525
+ expect(diagnostics.hasWarnings()).toBe(false);
2526
+ });
2527
+
2528
+ describe("non-legacy", () => {
2529
+ it("should use top-level `name` field", () => {
2530
+ const rawConfig: RawConfig = {
2531
+ name: "mock-name",
2532
+ legacy_env: false,
2533
+ env: { DEV: {} },
2534
+ };
2535
+
2536
+ const { config, diagnostics } = normalizeAndValidateConfig(
2537
+ rawConfig,
2538
+ undefined,
2539
+ { env: "DEV" }
2540
+ );
2541
+
2542
+ expect(config.name).toEqual("mock-name");
2543
+ expect(diagnostics.hasErrors()).toBe(false);
2544
+ expect(diagnostics.hasWarnings()).toBe(true);
2545
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2546
+ "Processing wrangler configuration:
2547
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2548
+ `);
2549
+ });
2550
+
2551
+ it("should error if named environment contains a `name` field, even if there is no top-level name", () => {
2552
+ const rawConfig: RawConfig = {
2553
+ legacy_env: false,
2554
+ env: {
2555
+ DEV: {
2556
+ name: "mock-env-name",
2557
+ },
2558
+ },
2559
+ };
2560
+
2561
+ const { config, diagnostics } = normalizeAndValidateConfig(
2562
+ rawConfig,
2563
+ undefined,
2564
+ { env: "DEV" }
2565
+ );
2566
+
2567
+ expect(config.name).toBeUndefined();
2568
+ expect(diagnostics.hasWarnings()).toBe(true);
2569
+ expect(diagnostics.hasErrors()).toBe(true);
2570
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2571
+ "Processing wrangler configuration:
2572
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2573
+ `);
2574
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2575
+ "Processing wrangler configuration:
2576
+
2577
+ - \\"env.DEV\\" environment configuration
2578
+ - The \\"name\\" field is not allowed in named service environments.
2579
+ Please remove the field from this environment."
2580
+ `);
2581
+ });
2582
+
2583
+ it("should error if top-level config and a named environment both contain a `name` field", () => {
2584
+ const rawConfig: RawConfig = {
2585
+ name: "mock-name",
2586
+ legacy_env: false,
2587
+ env: {
2588
+ DEV: {
2589
+ name: "mock-env-name",
2590
+ },
2591
+ },
2592
+ };
2593
+
2594
+ const { config, diagnostics } = normalizeAndValidateConfig(
2595
+ rawConfig,
2596
+ undefined,
2597
+ { env: "DEV" }
2598
+ );
2599
+
2600
+ expect(config.name).toEqual("mock-name");
2601
+ expect(diagnostics.hasWarnings()).toBe(true);
2602
+ expect(diagnostics.hasErrors()).toBe(true);
2603
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2604
+ "Processing wrangler configuration:
2605
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2606
+ `);
2607
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2608
+ "Processing wrangler configuration:
2609
+
2610
+ - \\"env.DEV\\" environment configuration
2611
+ - The \\"name\\" field is not allowed in named service environments.
2612
+ Please remove the field from this environment."
2613
+ `);
2614
+ });
2615
+ });
2616
+
2617
+ it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
2618
+ const rawConfig: RawConfig = {
2619
+ legacy_env: false,
2620
+ env: {
2621
+ DEV: {
2622
+ account_id: "some_account_id",
2623
+ },
2624
+ },
2625
+ };
2626
+
2627
+ const { config, diagnostics } = normalizeAndValidateConfig(
2628
+ rawConfig,
2629
+ undefined,
2630
+ { env: "DEV" }
2631
+ );
2632
+
2633
+ expect(diagnostics.hasWarnings()).toBe(true);
2634
+ expect(config.account_id).toBeUndefined();
2635
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2636
+ "Processing wrangler configuration:
2637
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2638
+ `);
2639
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2640
+ "Processing wrangler configuration:
2641
+
2642
+ - \\"env.DEV\\" environment configuration
2643
+ - The \\"account_id\\" field is not allowed in named service environments.
2644
+ Please remove the field from this environment."
2645
+ `);
2646
+ });
2647
+
2648
+ it("should error if top-level config and a named environment both contain a `account_id` field", () => {
2649
+ const rawConfig: RawConfig = {
2650
+ account_id: "ACCOUNT_ID",
2651
+ legacy_env: false,
2652
+ env: {
2653
+ DEV: {
2654
+ account_id: "ENV_ACCOUNT_ID",
2655
+ },
2656
+ },
2657
+ };
2658
+
2659
+ const { config, diagnostics } = normalizeAndValidateConfig(
2660
+ rawConfig,
2661
+ undefined,
2662
+ { env: "DEV" }
2663
+ );
2664
+
2665
+ expect(config.account_id).toEqual("ACCOUNT_ID");
2666
+ expect(diagnostics.hasErrors()).toBe(true);
2667
+ expect(diagnostics.hasWarnings()).toBe(true);
2668
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2669
+ "Processing wrangler configuration:
2670
+ - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
2671
+ `);
2672
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2673
+ "Processing wrangler configuration:
2674
+
2675
+ - \\"env.DEV\\" environment configuration
2676
+ - The \\"account_id\\" field is not allowed in named service environments.
2677
+ Please remove the field from this environment."
2678
+ `);
2679
+ });
2680
+
2681
+ it("should warn for non-inherited fields that are missing in environments", () => {
2682
+ const define: RawConfig["define"] = {
2683
+ abc: "123",
2684
+ };
2685
+ const vars: RawConfig["vars"] = {
2686
+ FOO: "foo",
2687
+ };
2688
+ const durable_objects: RawConfig["durable_objects"] = {
2689
+ bindings: [],
2690
+ };
2691
+ const kv_namespaces: RawConfig["kv_namespaces"] = [];
2692
+ const r2_buckets: RawConfig["r2_buckets"] = [];
2693
+ const unsafe: RawConfig["unsafe"] = { bindings: [] };
2694
+ const rawConfig: RawConfig = {
2695
+ define,
2696
+ vars,
2697
+ durable_objects,
2698
+ kv_namespaces,
2699
+ r2_buckets,
2700
+ unsafe,
2701
+ env: {
2702
+ ENV1: {},
2703
+ },
2704
+ };
2705
+
2706
+ const { config, diagnostics } = normalizeAndValidateConfig(
2707
+ rawConfig,
2708
+ undefined,
2709
+ { env: "ENV1" }
2710
+ );
2711
+
2712
+ expect(config).toEqual(
2713
+ expect.not.objectContaining({
2714
+ define,
2715
+ vars,
2716
+ durable_objects,
2717
+ kv_namespaces,
2718
+ r2_buckets,
2719
+ unsafe,
2720
+ })
2721
+ );
2722
+ expect(diagnostics.hasErrors()).toBe(false);
2723
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2724
+ "Processing wrangler configuration:
2725
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
2726
+ - \\"env.ENV1\\" environment configuration
2727
+ - \\"vars\\" exists at the top level, but not on \\"env.ENV1\\".
2728
+ This is not what you probably want, since \\"vars\\" is not inherited by environments.
2729
+ Please add \\"vars\\" to \\"env.ENV1\\".
2730
+ - \\"define\\" exists at the top level, but not on \\"env.ENV1\\".
2731
+ This is not what you probably want, since \\"define\\" is not inherited by environments.
2732
+ Please add \\"define\\" to \\"env.ENV1\\".
2733
+ - \\"durable_objects\\" exists at the top level, but not on \\"env.ENV1\\".
2734
+ This is not what you probably want, since \\"durable_objects\\" is not inherited by environments.
2735
+ Please add \\"durable_objects\\" to \\"env.ENV1\\".
2736
+ - \\"kv_namespaces\\" exists at the top level, but not on \\"env.ENV1\\".
2737
+ This is not what you probably want, since \\"kv_namespaces\\" is not inherited by environments.
2738
+ Please add \\"kv_namespaces\\" to \\"env.ENV1\\".
2739
+ - \\"r2_buckets\\" exists at the top level, but not on \\"env.ENV1\\".
2740
+ This is not what you probably want, since \\"r2_buckets\\" is not inherited by environments.
2741
+ Please add \\"r2_buckets\\" to \\"env.ENV1\\".
2742
+ - \\"unsafe\\" exists at the top level, but not on \\"env.ENV1\\".
2743
+ This is not what you probably want, since \\"unsafe\\" is not inherited by environments.
2744
+ Please add \\"unsafe\\" to \\"env.ENV1\\"."
2745
+ `);
2746
+ });
2747
+
2748
+ it("should error on invalid environment values", () => {
2749
+ const expectedConfig: RawEnvironment = {
2750
+ name: 111,
2751
+ account_id: 222,
2752
+ compatibility_date: 333,
2753
+ compatibility_flags: [444, 555],
2754
+ workers_dev: "BAD",
2755
+ routes: [666, 777],
2756
+ route: 888,
2757
+ jsx_factory: 999,
2758
+ jsx_fragment: 1000,
2759
+ tsconfig: 123,
2760
+ triggers: { crons: [1111, 1222] },
2761
+ usage_model: "INVALID",
2762
+ main: 1333,
2763
+ build: {
2764
+ command: 1444,
2765
+ cwd: 1555,
2766
+ watch_dir: 1666,
2767
+ },
2768
+ no_bundle: "INVALID",
2769
+ minify: "INVALID",
2770
+ node_compat: "INVALID",
2771
+ first_party_worker: "INVALID",
2772
+ } as unknown as RawEnvironment;
2773
+
2774
+ const { config, diagnostics } = normalizeAndValidateConfig(
2775
+ { env: { ENV1: expectedConfig } },
2776
+ undefined,
2777
+ { env: "ENV1" }
2778
+ );
2779
+
2780
+ expect(config).toEqual(expect.objectContaining(expectedConfig));
2781
+ expect(diagnostics.hasWarnings()).toBe(false);
2782
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2783
+ "Processing wrangler configuration:
2784
+
2785
+ - \\"env.ENV1\\" environment configuration
2786
+ - Expected \\"route\\" to be either a string, or an object with shape { pattern, custom_domain, zone_id | zone_name }, but got 888.
2787
+ - Expected \\"account_id\\" to be of type string but got 222.
2788
+ - Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, custom_domain, zone_id | zone_name }, but these weren't valid: [
2789
+ 666,
2790
+ 777
2791
+ ].
2792
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
2793
+ - Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
2794
+ - Expected \\"build.command\\" to be of type string but got 1444.
2795
+ - Expected \\"build.cwd\\" to be of type string but got 1555.
2796
+ - Expected \\"build.watch_dir\\" to be of type string but got 1666.
2797
+ - Expected \\"compatibility_date\\" to be of type string but got 333.
2798
+ - Expected \\"compatibility_flags\\" to be of type string array but got [444,555].
2799
+ - Expected \\"jsx_factory\\" to be of type string but got 999.
2800
+ - Expected \\"jsx_fragment\\" to be of type string but got 1000.
2801
+ - Expected \\"tsconfig\\" to be of type string but got 123.
2802
+ - Expected \\"name\\" to be of type string, alphanumeric and lowercase with dashes only but got 111.
2803
+ - Expected \\"main\\" to be of type string but got 1333.
2804
+ - Expected \\"usage_model\\" field to be one of [\\"bundled\\",\\"unbound\\"] but got \\"INVALID\\".
2805
+ - Expected \\"no_bundle\\" to be of type boolean but got \\"INVALID\\".
2806
+ - Expected \\"minify\\" to be of type boolean but got \\"INVALID\\".
2807
+ - Expected \\"node_compat\\" to be of type boolean but got \\"INVALID\\".
2808
+ - Expected \\"first_party_worker\\" to be of type boolean but got \\"INVALID\\"."
2809
+ `);
2810
+ });
2811
+
2812
+ describe("[define]", () => {
2813
+ it("should accept valid values for config.define", () => {
2814
+ const rawConfig: RawConfig = {
2815
+ define: {
2816
+ abc: "def",
2817
+ ghi: "123",
2818
+ },
2819
+ };
2820
+ const { config, diagnostics } = normalizeAndValidateConfig(
2821
+ rawConfig,
2822
+ undefined,
2823
+ { env: undefined }
2824
+ );
2825
+
2826
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2827
+ expect(diagnostics.hasWarnings()).toBe(false);
2828
+ expect(diagnostics.hasErrors()).toBe(false);
2829
+ });
2830
+
2831
+ it("should error if config.define is not an object", () => {
2832
+ const rawConfig: RawConfig = {
2833
+ // @ts-expect-error purposely using an invalid value
2834
+ define: 123,
2835
+ };
2836
+ const { config, diagnostics } = normalizeAndValidateConfig(
2837
+ rawConfig,
2838
+ undefined,
2839
+ { env: undefined }
2840
+ );
2841
+
2842
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2843
+ expect(diagnostics.hasWarnings()).toBe(false);
2844
+ expect(diagnostics.hasErrors()).toBe(true);
2845
+
2846
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2847
+ "Processing wrangler configuration:
2848
+ - The field \\"define\\" should be an object but got 123.
2849
+ "
2850
+ `);
2851
+ });
2852
+
2853
+ it("should error if the values on config.define are not strings", () => {
2854
+ const rawConfig: RawConfig = {
2855
+ define: {
2856
+ // @ts-expect-error purposely using an invalid value
2857
+ abc: 123,
2858
+ // This one's valid
2859
+ def: "xyz",
2860
+ // @ts-expect-error purposely using an invalid value
2861
+ ghi: true,
2862
+ // @ts-expect-error purposely using an invalid value
2863
+ jkl: {
2864
+ nested: "value",
2865
+ },
2866
+ },
2867
+ };
2868
+ const { config, diagnostics } = normalizeAndValidateConfig(
2869
+ rawConfig,
2870
+ undefined,
2871
+ { env: undefined }
2872
+ );
2873
+
2874
+ expect(config).toEqual(expect.objectContaining(rawConfig));
2875
+ expect(diagnostics.hasWarnings()).toBe(false);
2876
+ expect(diagnostics.hasErrors()).toBe(true);
2877
+
2878
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2879
+ "Processing wrangler configuration:
2880
+ - The field \\"define.abc\\" should be a string but got 123.
2881
+ - The field \\"define.ghi\\" should be a string but got true.
2882
+ - The field \\"define.jkl\\" should be a string but got {\\"nested\\":\\"value\\"}."
2883
+ `);
2884
+ });
2885
+
2886
+ describe("named environments", () => {
2887
+ it("should accept valid values for config.define inside an environment", () => {
2888
+ const rawConfig: RawConfig = {
2889
+ define: {
2890
+ abc: "def",
2891
+ ghi: "123",
2892
+ },
2893
+ env: {
2894
+ ENV1: {
2895
+ define: {
2896
+ abc: "xyz",
2897
+ ghi: "456",
2898
+ },
2899
+ },
2900
+ },
2901
+ };
2902
+ const { config, diagnostics } = normalizeAndValidateConfig(
2903
+ rawConfig,
2904
+ undefined,
2905
+ { env: "ENV1" }
2906
+ );
2907
+
2908
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2909
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2910
+ expect(diagnostics.hasWarnings()).toBe(false);
2911
+ expect(diagnostics.hasErrors()).toBe(false);
2912
+ });
2913
+
2914
+ it("should error if config.define is not an object inside an environment", () => {
2915
+ const rawConfig: RawConfig = {
2916
+ define: {
2917
+ abc: "def",
2918
+ ghi: "123",
2919
+ },
2920
+ env: {
2921
+ ENV1: {
2922
+ // @ts-expect-error purposely using an invalid value
2923
+ define: 123,
2924
+ },
2925
+ },
2926
+ };
2927
+ const { config, diagnostics } = normalizeAndValidateConfig(
2928
+ rawConfig,
2929
+ undefined,
2930
+ { env: "ENV1" }
2931
+ );
2932
+
2933
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2934
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2935
+ expect(diagnostics.hasWarnings()).toBe(false);
2936
+ expect(diagnostics.hasErrors()).toBe(true);
2937
+
2938
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
2939
+ "Processing wrangler configuration:
2940
+
2941
+ - \\"env.ENV1\\" environment configuration
2942
+ - The field \\"env.ENV1.define\\" should be an object but got 123.
2943
+ "
2944
+ `);
2945
+ });
2946
+
2947
+ it("should warn if if the shape of .define inside an environment doesn't match the shape of the top level .define", () => {
2948
+ const rawConfig: RawConfig = {
2949
+ define: {
2950
+ abc: "def",
2951
+ ghi: "123",
2952
+ },
2953
+ env: {
2954
+ ENV1: {
2955
+ define: {
2956
+ abc: "def",
2957
+ xyz: "123",
2958
+ },
2959
+ },
2960
+ },
2961
+ };
2962
+ const { config, diagnostics } = normalizeAndValidateConfig(
2963
+ rawConfig,
2964
+ undefined,
2965
+ { env: "ENV1" }
2966
+ );
2967
+
2968
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2969
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
2970
+ expect(diagnostics.hasWarnings()).toBe(true);
2971
+ expect(diagnostics.hasErrors()).toBe(false);
2972
+
2973
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
2974
+ "Processing wrangler configuration:
2975
+
2976
+ - \\"env.ENV1\\" environment configuration
2977
+ - \\"define.ghi\\" exists at the top level, but not on \\"env.ENV1.define\\".
2978
+ This is not what you probably want, since \\"define\\" configuration is not inherited by environments.
2979
+ Please add \\"define.ghi\\" to \\"env.ENV1\\".
2980
+ - \\"xyz\\" exists on \\"env.ENV1\\", but not on the top level.
2981
+ This is not what you probably want, since \\"define\\" configuration within environments can only override existing top level \\"define\\" configuration
2982
+ Please remove \\"env.ENV1.define.xyz\\", or add \\"define.xyz\\"."
2983
+ `);
2984
+ });
2985
+
2986
+ it("should error if the values on config.define in an environment are not strings", () => {
2987
+ const rawConfig: RawConfig = {
2988
+ define: {
2989
+ abc: "123",
2990
+ def: "xyz",
2991
+ ghi: "true",
2992
+ jkl: "some value",
2993
+ },
2994
+ env: {
2995
+ ENV1: {
2996
+ define: {
2997
+ // @ts-expect-error purposely using an invalid value
2998
+ abc: 123,
2999
+ // This one's valid
3000
+ def: "xyz",
3001
+ // @ts-expect-error purposely using an invalid value
3002
+ ghi: true,
3003
+ // @ts-expect-error purposely using an invalid value
3004
+ jkl: {
3005
+ nested: "value",
3006
+ },
3007
+ },
3008
+ },
3009
+ },
3010
+ };
3011
+ const { config, diagnostics } = normalizeAndValidateConfig(
3012
+ rawConfig,
3013
+ undefined,
3014
+ { env: "ENV1" }
3015
+ );
3016
+
3017
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3018
+ expect(config).toEqual(expect.objectContaining(rawConfig.env!.ENV1));
3019
+ expect(diagnostics.hasWarnings()).toBe(false);
3020
+ expect(diagnostics.hasErrors()).toBe(true);
3021
+
3022
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3023
+ "Processing wrangler configuration:
3024
+
3025
+ - \\"env.ENV1\\" environment configuration
3026
+ - The field \\"env.ENV1.define.abc\\" should be a string but got 123.
3027
+ - The field \\"env.ENV1.define.ghi\\" should be a string but got true.
3028
+ - The field \\"env.ENV1.define.jkl\\" should be a string but got {\\"nested\\":\\"value\\"}."
3029
+ `);
3030
+ });
3031
+ });
3032
+ });
3033
+
3034
+ describe("[durable_objects]", () => {
3035
+ it("should error if durable_objects is an array", () => {
3036
+ const { diagnostics } = normalizeAndValidateConfig(
3037
+ { env: { ENV1: { durable_objects: [] } } } as unknown as RawConfig,
3038
+ undefined,
3039
+ { env: "ENV1" }
3040
+ );
3041
+
3042
+ expect(diagnostics.hasWarnings()).toBe(false);
3043
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3044
+ "Processing wrangler configuration:
3045
+
3046
+ - \\"env.ENV1\\" environment configuration
3047
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got []."
3048
+ `);
3049
+ });
3050
+
3051
+ it("should error if durable_objects is a string", () => {
3052
+ const { diagnostics } = normalizeAndValidateConfig(
3053
+ { env: { ENV1: { durable_objects: "BAD" } } } as unknown as RawConfig,
3054
+ undefined,
3055
+ { env: "ENV1" }
3056
+ );
3057
+
3058
+ expect(diagnostics.hasWarnings()).toBe(false);
3059
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3060
+ "Processing wrangler configuration:
3061
+
3062
+ - \\"env.ENV1\\" environment configuration
3063
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got \\"BAD\\"."
3064
+ `);
3065
+ });
3066
+
3067
+ it("should error if durable_objects is a number", () => {
3068
+ const { diagnostics } = normalizeAndValidateConfig(
3069
+ { env: { ENV1: { durable_objects: 999 } } } as unknown as RawConfig,
3070
+ undefined,
3071
+ { env: "ENV1" }
3072
+ );
3073
+
3074
+ expect(diagnostics.hasWarnings()).toBe(false);
3075
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3076
+ "Processing wrangler configuration:
3077
+
3078
+ - \\"env.ENV1\\" environment configuration
3079
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got 999."
3080
+ `);
3081
+ });
3082
+
3083
+ it("should error if durable_objects is null", () => {
3084
+ const { diagnostics } = normalizeAndValidateConfig(
3085
+ { env: { ENV1: { durable_objects: null } } } as unknown as RawConfig,
3086
+ undefined,
3087
+ { env: "ENV1" }
3088
+ );
3089
+
3090
+ expect(diagnostics.hasWarnings()).toBe(false);
3091
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3092
+ "Processing wrangler configuration:
3093
+
3094
+ - \\"env.ENV1\\" environment configuration
3095
+ - The field \\"env.ENV1.durable_objects\\" should be an object but got null."
3096
+ `);
3097
+ });
3098
+
3099
+ it("should error if durable_objects.bindings is not defined", () => {
3100
+ const { diagnostics } = normalizeAndValidateConfig(
3101
+ { env: { ENV1: { durable_objects: {} } } } as unknown as RawConfig,
3102
+ undefined,
3103
+ { env: "ENV1" }
3104
+ );
3105
+
3106
+ expect(diagnostics.hasWarnings()).toBe(false);
3107
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3108
+ "Processing wrangler configuration:
3109
+
3110
+ - \\"env.ENV1\\" environment configuration
3111
+ - The field \\"env.ENV1.durable_objects\\" is missing the required \\"bindings\\" property."
3112
+ `);
3113
+ });
3114
+
3115
+ it("should error if durable_objects.bindings is an object", () => {
3116
+ const { diagnostics } = normalizeAndValidateConfig(
3117
+ {
3118
+ env: { ENV1: { durable_objects: { bindings: {} } } },
3119
+ } as unknown as RawConfig,
3120
+ undefined,
3121
+ { env: "ENV1" }
3122
+ );
3123
+
3124
+ expect(diagnostics.hasWarnings()).toBe(false);
3125
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3126
+ "Processing wrangler configuration:
3127
+
3128
+ - \\"env.ENV1\\" environment configuration
3129
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got {}."
3130
+ `);
3131
+ });
3132
+
3133
+ it("should error if durable_objects.bindings is a string", () => {
3134
+ const { diagnostics } = normalizeAndValidateConfig(
3135
+ {
3136
+ env: { ENV1: { durable_objects: { bindings: "BAD" } } },
3137
+ } as unknown as RawConfig,
3138
+ undefined,
3139
+ { env: "ENV1" }
3140
+ );
3141
+
3142
+ expect(diagnostics.hasWarnings()).toBe(false);
3143
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3144
+ "Processing wrangler configuration:
3145
+
3146
+ - \\"env.ENV1\\" environment configuration
3147
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got \\"BAD\\"."
3148
+ `);
3149
+ });
3150
+
3151
+ it("should error if durable_objects.bindings is a number", () => {
3152
+ const { diagnostics } = normalizeAndValidateConfig(
3153
+ {
3154
+ env: { ENV1: { durable_objects: { bindings: 999 } } },
3155
+ } as unknown as RawConfig,
3156
+ undefined,
3157
+ { env: "ENV1" }
3158
+ );
3159
+
3160
+ expect(diagnostics.hasWarnings()).toBe(false);
3161
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3162
+ "Processing wrangler configuration:
3163
+
3164
+ - \\"env.ENV1\\" environment configuration
3165
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got 999."
3166
+ `);
3167
+ });
3168
+
3169
+ it("should error if durable_objects.bindings is null", () => {
3170
+ const { diagnostics } = normalizeAndValidateConfig(
3171
+ {
3172
+ env: { ENV1: { durable_objects: { bindings: null } } },
3173
+ } as unknown as RawConfig,
3174
+ undefined,
3175
+ { env: "ENV1" }
3176
+ );
3177
+
3178
+ expect(diagnostics.hasWarnings()).toBe(false);
3179
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3180
+ "Processing wrangler configuration:
3181
+
3182
+ - \\"env.ENV1\\" environment configuration
3183
+ - The field \\"env.ENV1.durable_objects.bindings\\" should be an array but got null."
3184
+ `);
3185
+ });
3186
+
3187
+ it("should error if durable_objects.bindings are not valid", () => {
3188
+ const { diagnostics } = normalizeAndValidateConfig(
3189
+ {
3190
+ env: {
3191
+ ENV1: {
3192
+ durable_objects: {
3193
+ bindings: [
3194
+ {},
3195
+ { name: "VALID" },
3196
+ { name: 1555, class_name: 1666 },
3197
+ {
3198
+ name: 1777,
3199
+ class_name: 1888,
3200
+ script_name: 1999,
3201
+ },
3202
+ ],
3203
+ },
3204
+ },
3205
+ },
3206
+ } as unknown as RawConfig,
3207
+ undefined,
3208
+ { env: "ENV1" }
3209
+ );
3210
+
3211
+ expect(diagnostics.hasWarnings()).toBe(false);
3212
+ expect(diagnostics.hasErrors()).toBe(true);
3213
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3214
+ "Processing wrangler configuration:
3215
+
3216
+ - \\"env.ENV1\\" environment configuration
3217
+
3218
+ - \\"env.ENV1.durable_objects.bindings[0]\\": {}
3219
+ - binding should have a string \\"name\\" field.
3220
+ - binding should have a string \\"class_name\\" field.
3221
+
3222
+ - \\"env.ENV1.durable_objects.bindings[1]\\": {\\"name\\":\\"VALID\\"}
3223
+ - binding should have a string \\"class_name\\" field.
3224
+
3225
+ - \\"env.ENV1.durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
3226
+ - binding should have a string \\"name\\" field.
3227
+ - binding should have a string \\"class_name\\" field.
3228
+
3229
+ - \\"env.ENV1.durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
3230
+ - binding should have a string \\"name\\" field.
3231
+ - binding should have a string \\"class_name\\" field.
3232
+ - the field \\"script_name\\", when present, should be a string."
3233
+ `);
3234
+ });
3235
+ });
3236
+
3237
+ describe("[kv_namespaces]", () => {
3238
+ it("should error if kv_namespaces is an object", () => {
3239
+ const { diagnostics } = normalizeAndValidateConfig(
3240
+ { env: { ENV1: { kv_namespaces: {} } } } as unknown as RawConfig,
3241
+ undefined,
3242
+ { env: "ENV1" }
3243
+ );
3244
+
3245
+ expect(diagnostics.hasWarnings()).toBe(false);
3246
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3247
+ "Processing wrangler configuration:
3248
+
3249
+ - \\"env.ENV1\\" environment configuration
3250
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got {}."
3251
+ `);
3252
+ });
3253
+
3254
+ it("should error if kv_namespaces is a string", () => {
3255
+ const { diagnostics } = normalizeAndValidateConfig(
3256
+ { env: { ENV1: { kv_namespaces: "BAD" } } } as unknown as RawConfig,
3257
+ undefined,
3258
+ { env: "ENV1" }
3259
+ );
3260
+
3261
+ expect(diagnostics.hasWarnings()).toBe(false);
3262
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3263
+ "Processing wrangler configuration:
3264
+
3265
+ - \\"env.ENV1\\" environment configuration
3266
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got \\"BAD\\"."
3267
+ `);
3268
+ });
3269
+
3270
+ it("should error if kv_namespaces is a number", () => {
3271
+ const { diagnostics } = normalizeAndValidateConfig(
3272
+ { env: { ENV1: { kv_namespaces: 999 } } } as unknown as RawConfig,
3273
+ undefined,
3274
+ { env: "ENV1" }
3275
+ );
3276
+
3277
+ expect(diagnostics.hasWarnings()).toBe(false);
3278
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3279
+ "Processing wrangler configuration:
3280
+
3281
+ - \\"env.ENV1\\" environment configuration
3282
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got 999."
3283
+ `);
3284
+ });
3285
+
3286
+ it("should error if kv_namespaces is null", () => {
3287
+ const { diagnostics } = normalizeAndValidateConfig(
3288
+ { env: { ENV1: { kv_namespaces: null } } } as unknown as RawConfig,
3289
+ undefined,
3290
+ { env: "ENV1" }
3291
+ );
3292
+
3293
+ expect(diagnostics.hasWarnings()).toBe(false);
3294
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3295
+ "Processing wrangler configuration:
3296
+
3297
+ - \\"env.ENV1\\" environment configuration
3298
+ - The field \\"env.ENV1.kv_namespaces\\" should be an array but got null."
3299
+ `);
3300
+ });
3301
+
3302
+ it("should error if kv_namespaces.bindings are not valid", () => {
3303
+ const { diagnostics } = normalizeAndValidateConfig(
3304
+ {
3305
+ env: {
3306
+ ENV1: {
3307
+ kv_namespaces: [
3308
+ {},
3309
+ { binding: "VALID" },
3310
+ { binding: 2000, id: 2111 },
3311
+ {
3312
+ binding: "KV_BINDING_2",
3313
+ id: "KV_ID_2",
3314
+ preview_id: 2222,
3315
+ },
3316
+ ],
3317
+ },
3318
+ },
3319
+ } as unknown as RawConfig,
3320
+ undefined,
3321
+ { env: "ENV1" }
3322
+ );
3323
+
3324
+ expect(diagnostics.hasWarnings()).toBe(false);
3325
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3326
+ "Processing wrangler configuration:
3327
+
3328
+ - \\"env.ENV1\\" environment configuration
3329
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"binding\\" field but got {}.
3330
+ - \\"env.ENV1.kv_namespaces[0]\\" bindings should have a string \\"id\\" field but got {}.
3331
+ - \\"env.ENV1.kv_namespaces[1]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\"}.
3332
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
3333
+ - \\"env.ENV1.kv_namespaces[2]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":2000,\\"id\\":2111}.
3334
+ - \\"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}."
3335
+ `);
3336
+ });
3337
+ });
3338
+
3339
+ describe("[r2_buckets]", () => {
3340
+ it("should error if r2_buckets is an object", () => {
3341
+ const { diagnostics } = normalizeAndValidateConfig(
3342
+ { env: { ENV1: { r2_buckets: {} } } } as unknown as RawConfig,
3343
+ undefined,
3344
+ { env: "ENV1" }
3345
+ );
3346
+
3347
+ expect(diagnostics.hasWarnings()).toBe(false);
3348
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3349
+ "Processing wrangler configuration:
3350
+
3351
+ - \\"env.ENV1\\" environment configuration
3352
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got {}."
3353
+ `);
3354
+ });
3355
+
3356
+ it("should error if r2_buckets is a string", () => {
3357
+ const { diagnostics } = normalizeAndValidateConfig(
3358
+ { env: { ENV1: { r2_buckets: "BAD" } } } as unknown as RawConfig,
3359
+ undefined,
3360
+ { env: "ENV1" }
3361
+ );
3362
+
3363
+ expect(diagnostics.hasWarnings()).toBe(false);
3364
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3365
+ "Processing wrangler configuration:
3366
+
3367
+ - \\"env.ENV1\\" environment configuration
3368
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got \\"BAD\\"."
3369
+ `);
3370
+ });
3371
+
3372
+ it("should error if r2_buckets is a number", () => {
3373
+ const { diagnostics } = normalizeAndValidateConfig(
3374
+ { env: { ENV1: { r2_buckets: 999 } } } as unknown as RawConfig,
3375
+ undefined,
3376
+ { env: "ENV1" }
3377
+ );
3378
+
3379
+ expect(diagnostics.hasWarnings()).toBe(false);
3380
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3381
+ "Processing wrangler configuration:
3382
+
3383
+ - \\"env.ENV1\\" environment configuration
3384
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got 999."
3385
+ `);
3386
+ });
3387
+
3388
+ it("should error if r2_buckets is null", () => {
3389
+ const { diagnostics } = normalizeAndValidateConfig(
3390
+ { env: { ENV1: { r2_buckets: null } } } as unknown as RawConfig,
3391
+ undefined,
3392
+ { env: "ENV1" }
3393
+ );
3394
+
3395
+ expect(diagnostics.hasWarnings()).toBe(false);
3396
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3397
+ "Processing wrangler configuration:
3398
+
3399
+ - \\"env.ENV1\\" environment configuration
3400
+ - The field \\"env.ENV1.r2_buckets\\" should be an array but got null."
3401
+ `);
3402
+ });
3403
+
3404
+ it("should error if r2_buckets.bindings are not valid", () => {
3405
+ const { diagnostics } = normalizeAndValidateConfig(
3406
+ {
3407
+ env: {
3408
+ ENV1: {
3409
+ r2_buckets: [
3410
+ {},
3411
+ { binding: "R2_BINDING_1" },
3412
+ { binding: 2333, bucket_name: 2444 },
3413
+ {
3414
+ binding: "R2_BINDING_2",
3415
+ bucket_name: "R2_BUCKET_2",
3416
+ preview_bucket_name: 2555,
3417
+ },
3418
+ ],
3419
+ },
3420
+ },
3421
+ } as unknown as RawConfig,
3422
+ undefined,
3423
+ { env: "ENV1" }
3424
+ );
3425
+
3426
+ expect(diagnostics.hasWarnings()).toBe(false);
3427
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3428
+ "Processing wrangler configuration:
3429
+
3430
+ - \\"env.ENV1\\" environment configuration
3431
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"binding\\" field but got {}.
3432
+ - \\"env.ENV1.r2_buckets[0]\\" bindings should have a string \\"bucket_name\\" field but got {}.
3433
+ - \\"env.ENV1.r2_buckets[1]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\"}.
3434
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
3435
+ - \\"env.ENV1.r2_buckets[2]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":2333,\\"bucket_name\\":2444}.
3436
+ - \\"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}."
3437
+ `);
3438
+ });
3439
+ });
3440
+
3441
+ describe("[unsafe.bindings]", () => {
3442
+ it("should error if unsafe is an array", () => {
3443
+ const { diagnostics } = normalizeAndValidateConfig(
3444
+ { env: { ENV1: { unsafe: [] } } } as unknown as RawConfig,
3445
+ undefined,
3446
+ { env: "ENV1" }
3447
+ );
3448
+
3449
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3450
+ "Processing wrangler configuration:
3451
+
3452
+ - \\"env.ENV1\\" environment configuration
3453
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3454
+ `);
3455
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3456
+ "Processing wrangler configuration:
3457
+
3458
+ - \\"env.ENV1\\" environment configuration
3459
+ - The field \\"env.ENV1.unsafe\\" should be an object but got []."
3460
+ `);
3461
+ });
3462
+
3463
+ it("should error if unsafe is a string", () => {
3464
+ const { diagnostics } = normalizeAndValidateConfig(
3465
+ { env: { ENV1: { unsafe: "BAD" } } } as unknown as RawConfig,
3466
+ undefined,
3467
+ { env: "ENV1" }
3468
+ );
3469
+
3470
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3471
+ "Processing wrangler configuration:
3472
+
3473
+ - \\"env.ENV1\\" environment configuration
3474
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3475
+ `);
3476
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3477
+ "Processing wrangler configuration:
3478
+
3479
+ - \\"env.ENV1\\" environment configuration
3480
+ - The field \\"env.ENV1.unsafe\\" should be an object but got \\"BAD\\"."
3481
+ `);
3482
+ });
3483
+
3484
+ it("should error if unsafe is a number", () => {
3485
+ const { diagnostics } = normalizeAndValidateConfig(
3486
+ { env: { ENV1: { unsafe: 999 } } } as unknown as RawConfig,
3487
+ undefined,
3488
+ { env: "ENV1" }
3489
+ );
3490
+
3491
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3492
+ "Processing wrangler configuration:
3493
+
3494
+ - \\"env.ENV1\\" environment configuration
3495
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3496
+ `);
3497
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3498
+ "Processing wrangler configuration:
3499
+
3500
+ - \\"env.ENV1\\" environment configuration
3501
+ - The field \\"env.ENV1.unsafe\\" should be an object but got 999."
3502
+ `);
3503
+ });
3504
+
3505
+ it("should error if unsafe is null", () => {
3506
+ const { diagnostics } = normalizeAndValidateConfig(
3507
+ { env: { ENV1: { unsafe: null } } } as unknown as RawConfig,
3508
+ undefined,
3509
+ { env: "ENV1" }
3510
+ );
3511
+
3512
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3513
+ "Processing wrangler configuration:
3514
+
3515
+ - \\"env.ENV1\\" environment configuration
3516
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3517
+ `);
3518
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3519
+ "Processing wrangler configuration:
3520
+
3521
+ - \\"env.ENV1\\" environment configuration
3522
+ - The field \\"env.ENV1.unsafe\\" should be an object but got null."
3523
+ `);
3524
+ });
3525
+
3526
+ it("should error if unsafe.bindings is not defined", () => {
3527
+ const { diagnostics } = normalizeAndValidateConfig(
3528
+ { env: { ENV1: { unsafe: {} } } } as unknown as RawConfig,
3529
+ undefined,
3530
+ { env: "ENV1" }
3531
+ );
3532
+
3533
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3534
+ "Processing wrangler configuration:
3535
+
3536
+ - \\"env.ENV1\\" environment configuration
3537
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3538
+ `);
3539
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3540
+ "Processing wrangler configuration:
3541
+
3542
+ - \\"env.ENV1\\" environment configuration
3543
+ - The field \\"env.ENV1.unsafe\\" is missing the required \\"bindings\\" property."
3544
+ `);
3545
+ });
3546
+
3547
+ it("should error if unsafe.bindings is an object", () => {
3548
+ const { diagnostics } = normalizeAndValidateConfig(
3549
+ {
3550
+ env: { ENV1: { unsafe: { bindings: {} } } },
3551
+ } as unknown as RawConfig,
3552
+ undefined,
3553
+ { env: "ENV1" }
3554
+ );
3555
+
3556
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3557
+ "Processing wrangler configuration:
3558
+
3559
+ - \\"env.ENV1\\" environment configuration
3560
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3561
+ `);
3562
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3563
+ "Processing wrangler configuration:
3564
+
3565
+ - \\"env.ENV1\\" environment configuration
3566
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got {}."
3567
+ `);
3568
+ });
3569
+
3570
+ it("should error if unsafe.bindings is a string", () => {
3571
+ const { diagnostics } = normalizeAndValidateConfig(
3572
+ {
3573
+ env: { ENV1: { unsafe: { bindings: "BAD" } } },
3574
+ } as unknown as RawConfig,
3575
+ undefined,
3576
+ { env: "ENV1" }
3577
+ );
3578
+
3579
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3580
+ "Processing wrangler configuration:
3581
+
3582
+ - \\"env.ENV1\\" environment configuration
3583
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3584
+ `);
3585
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3586
+ "Processing wrangler configuration:
3587
+
3588
+ - \\"env.ENV1\\" environment configuration
3589
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got \\"BAD\\"."
3590
+ `);
3591
+ });
3592
+
3593
+ it("should error if unsafe.bindings is a number", () => {
3594
+ const { diagnostics } = normalizeAndValidateConfig(
3595
+ {
3596
+ env: { ENV1: { unsafe: { bindings: 999 } } },
3597
+ } as unknown as RawConfig,
3598
+ undefined,
3599
+ { env: "ENV1" }
3600
+ );
3601
+
3602
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3603
+ "Processing wrangler configuration:
3604
+
3605
+ - \\"env.ENV1\\" environment configuration
3606
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3607
+ `);
3608
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3609
+ "Processing wrangler configuration:
3610
+
3611
+ - \\"env.ENV1\\" environment configuration
3612
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got 999."
3613
+ `);
3614
+ });
3615
+
3616
+ it("should error if unsafe.bindings is null", () => {
3617
+ const { diagnostics } = normalizeAndValidateConfig(
3618
+ {
3619
+ env: { ENV1: { unsafe: { bindings: null } } },
3620
+ } as unknown as RawConfig,
3621
+ undefined,
3622
+ { env: "ENV1" }
3623
+ );
3624
+
3625
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3626
+ "Processing wrangler configuration:
3627
+
3628
+ - \\"env.ENV1\\" environment configuration
3629
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3630
+ `);
3631
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3632
+ "Processing wrangler configuration:
3633
+
3634
+ - \\"env.ENV1\\" environment configuration
3635
+ - The field \\"env.ENV1.unsafe.bindings\\" should be an array but got null."
3636
+ `);
3637
+ });
3638
+
3639
+ it("should error if unsafe.bindings are not valid", () => {
3640
+ const { diagnostics } = normalizeAndValidateConfig(
3641
+ {
3642
+ env: {
3643
+ ENV1: {
3644
+ unsafe: {
3645
+ bindings: [
3646
+ {},
3647
+ { name: "UNSAFE_BINDING_1" },
3648
+ { name: 2666, type: 2777 },
3649
+ {
3650
+ name: "UNSAFE_BINDING_2",
3651
+ type: "UNSAFE_TYPE_2",
3652
+ extra: 2888,
3653
+ },
3654
+ ],
3655
+ },
3656
+ },
3657
+ },
3658
+ } as unknown as RawConfig,
3659
+ undefined,
3660
+ { env: "ENV1" }
3661
+ );
3662
+
3663
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3664
+ "Processing wrangler configuration:
3665
+
3666
+ - \\"env.ENV1\\" environment configuration
3667
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
3668
+ `);
3669
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3670
+ "Processing wrangler configuration:
3671
+
3672
+ - \\"env.ENV1\\" environment configuration
3673
+
3674
+ - \\"env.ENV1.unsafe.bindings[0]\\": {}
3675
+ - binding should have a string \\"name\\" field.
3676
+ - binding should have a string \\"type\\" field.
3677
+
3678
+ - \\"env.ENV1.unsafe.bindings[1]\\": {\\"name\\":\\"UNSAFE_BINDING_1\\"}
3679
+ - binding should have a string \\"type\\" field.
3680
+
3681
+ - \\"env.ENV1.unsafe.bindings[2]\\": {\\"name\\":2666,\\"type\\":2777}
3682
+ - binding should have a string \\"name\\" field.
3683
+ - binding should have a string \\"type\\" field."
3684
+ `);
3685
+ });
3686
+ });
3687
+
3688
+ describe("(deprecated)", () => {
3689
+ it("should remove and warn about deprecated properties", () => {
3690
+ const environment: RawEnvironment = {
3691
+ zone_id: "ZONE_ID",
3692
+ "kv-namespaces": "BAD_KV_NAMESPACE",
3693
+ experimental_services: [
3694
+ {
3695
+ name: "mock-name",
3696
+ service: "SERVICE",
3697
+ environment: "ENV",
3698
+ },
3699
+ ],
3700
+ };
3701
+
3702
+ const { config, diagnostics } = normalizeAndValidateConfig(
3703
+ {
3704
+ env: {
3705
+ ENV1: environment,
3706
+ },
3707
+ },
3708
+ undefined,
3709
+ { env: "ENV1" }
3710
+ );
3711
+
3712
+ expect("experimental_services" in config).toBe(false);
3713
+ // Zone is not removed yet, since `route` commands might use it
3714
+ expect(config.zone_id).toEqual("ZONE_ID");
3715
+ expect(diagnostics.hasErrors()).toBe(false);
3716
+ expect(diagnostics.hasWarnings()).toBe(true);
3717
+ expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3718
+ "Processing wrangler configuration:
3719
+
3720
+ - \\"env.ENV1\\" environment configuration
3721
+ - Deprecation: \\"kv-namespaces\\":
3722
+ The \\"kv-namespaces\\" field is no longer supported, please rename to \\"kv_namespaces\\"
3723
+ - Deprecation: \\"zone_id\\":
3724
+ This is unnecessary since we can deduce this from routes directly.
3725
+ - Deprecation: \\"experimental_services\\":
3726
+ The \\"experimental_services\\" field is no longer supported. Simply rename the [experimental_services] field to [services]."
3727
+ `);
3728
+ });
3729
+ });
3730
+
3731
+ describe("route & routes fields", () => {
3732
+ it("should error if both route and routes are specified in the same environment", () => {
3733
+ const environment: RawEnvironment = {
3734
+ name: "mock-env-name",
3735
+ account_id: "ENV_ACCOUNT_ID",
3736
+ compatibility_date: "2022-02-02",
3737
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3738
+ workers_dev: true,
3739
+ route: "ENV_ROUTE_1",
3740
+ routes: ["ENV_ROUTE_2", "ENV_ROUTE_3"],
3741
+ jsx_factory: "ENV_JSX_FACTORY",
3742
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3743
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3744
+ usage_model: "unbound",
3745
+ };
3746
+ const expectedConfig: RawConfig = {
3747
+ name: "mock-name",
3748
+ account_id: "ACCOUNT_ID",
3749
+ compatibility_date: "2022-01-01",
3750
+ compatibility_flags: ["FLAG1", "FLAG2"],
3751
+ workers_dev: false,
3752
+ routes: ["ROUTE_1", "ROUTE_2"],
3753
+ jsx_factory: "JSX_FACTORY",
3754
+ jsx_fragment: "JSX_FRAGMENT",
3755
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3756
+ usage_model: "bundled",
3757
+ env: {
3758
+ ENV1: environment,
3759
+ },
3760
+ };
3761
+
3762
+ const { config, diagnostics } = normalizeAndValidateConfig(
3763
+ expectedConfig,
3764
+ undefined,
3765
+ { env: "ENV1" }
3766
+ );
3767
+
3768
+ expect(config).toEqual(expect.objectContaining(environment));
3769
+ expect(diagnostics.hasErrors()).toBe(true);
3770
+ expect(diagnostics.hasWarnings()).toBe(false);
3771
+
3772
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3773
+ "Processing wrangler configuration:
3774
+
3775
+ - \\"env.ENV1\\" environment configuration
3776
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
3777
+ `);
3778
+ });
3779
+
3780
+ it("should error if both route and routes are specified in the top-level environment", () => {
3781
+ const environment: RawEnvironment = {
3782
+ name: "mock-env-name",
3783
+ account_id: "ENV_ACCOUNT_ID",
3784
+ compatibility_date: "2022-02-02",
3785
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3786
+ workers_dev: true,
3787
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
3788
+ jsx_factory: "ENV_JSX_FACTORY",
3789
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3790
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3791
+ usage_model: "unbound",
3792
+ };
3793
+ const expectedConfig: RawConfig = {
3794
+ name: "mock-name",
3795
+ account_id: "ACCOUNT_ID",
3796
+ compatibility_date: "2022-01-01",
3797
+ compatibility_flags: ["FLAG1", "FLAG2"],
3798
+ workers_dev: false,
3799
+ route: "ROUTE_1",
3800
+ routes: ["ROUTE_2", "ROUTE_3"],
3801
+ jsx_factory: "JSX_FACTORY",
3802
+ jsx_fragment: "JSX_FRAGMENT",
3803
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3804
+ usage_model: "bundled",
3805
+ env: {
3806
+ ENV1: environment,
3807
+ },
3808
+ };
3809
+
3810
+ const { config, diagnostics } = normalizeAndValidateConfig(
3811
+ expectedConfig,
3812
+ undefined,
3813
+ { env: "ENV1" }
3814
+ );
3815
+
3816
+ expect(config).toEqual(expect.objectContaining(environment));
3817
+ expect(diagnostics.hasErrors()).toBe(true);
3818
+ expect(diagnostics.hasWarnings()).toBe(false);
3819
+
3820
+ expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3821
+ "Processing wrangler configuration:
3822
+ - Expected exactly one of the following fields [\\"routes\\",\\"route\\"]."
3823
+ `);
3824
+ });
3825
+
3826
+ it("should not error if <env>.route and <top-level>.routes are specified", () => {
3827
+ const environment: RawEnvironment = {
3828
+ name: "mock-env-name",
3829
+ account_id: "ENV_ACCOUNT_ID",
3830
+ compatibility_date: "2022-02-02",
3831
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3832
+ workers_dev: true,
3833
+ route: "ENV_ROUTE_1",
3834
+ jsx_factory: "ENV_JSX_FACTORY",
3835
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3836
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3837
+ usage_model: "unbound",
3838
+ };
3839
+ const expectedConfig: RawConfig = {
3840
+ name: "mock-name",
3841
+ account_id: "ACCOUNT_ID",
3842
+ compatibility_date: "2022-01-01",
3843
+ compatibility_flags: ["FLAG1", "FLAG2"],
3844
+ workers_dev: false,
3845
+ routes: ["ROUTE_1", "ROUTE_2"],
3846
+ jsx_factory: "JSX_FACTORY",
3847
+ jsx_fragment: "JSX_FRAGMENT",
3848
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3849
+ usage_model: "bundled",
3850
+ env: {
3851
+ ENV1: environment,
3852
+ },
3853
+ };
3854
+
3855
+ const { config, diagnostics } = normalizeAndValidateConfig(
3856
+ expectedConfig,
3857
+ undefined,
3858
+ { env: "ENV1" }
3859
+ );
3860
+
3861
+ expect(config).toEqual(expect.objectContaining(environment));
3862
+ expect(diagnostics.hasErrors()).toBe(false);
3863
+ expect(diagnostics.hasWarnings()).toBe(false);
3864
+ });
3865
+
3866
+ it("should not error if <env>.routes and <top-level>.route are specified", () => {
3867
+ const environment: RawEnvironment = {
3868
+ name: "mock-env-name",
3869
+ account_id: "ENV_ACCOUNT_ID",
3870
+ compatibility_date: "2022-02-02",
3871
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3872
+ workers_dev: true,
3873
+ routes: ["ENV_ROUTE_1", "ENV_ROUTE_2"],
3874
+ jsx_factory: "ENV_JSX_FACTORY",
3875
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3876
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3877
+ usage_model: "unbound",
3878
+ };
3879
+ const expectedConfig: RawConfig = {
3880
+ name: "mock-name",
3881
+ account_id: "ACCOUNT_ID",
3882
+ compatibility_date: "2022-01-01",
3883
+ compatibility_flags: ["FLAG1", "FLAG2"],
3884
+ workers_dev: false,
3885
+ route: "ROUTE_1",
3886
+ jsx_factory: "JSX_FACTORY",
3887
+ jsx_fragment: "JSX_FRAGMENT",
3888
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3889
+ usage_model: "bundled",
3890
+ env: {
3891
+ ENV1: environment,
3892
+ },
3893
+ };
3894
+
3895
+ const { config, diagnostics } = normalizeAndValidateConfig(
3896
+ expectedConfig,
3897
+ undefined,
3898
+ { env: "ENV1" }
3899
+ );
3900
+
3901
+ expect(config).toEqual(expect.objectContaining(environment));
3902
+ expect(diagnostics.hasErrors()).toBe(false);
3903
+ expect(diagnostics.hasWarnings()).toBe(false);
3904
+ });
3905
+
3906
+ it("should not error if <env1>.route and <env2>.routes are specified", () => {
3907
+ const environment1: RawEnvironment = {
3908
+ name: "mock-env-name",
3909
+ account_id: "ENV_ACCOUNT_ID",
3910
+ compatibility_date: "2022-02-02",
3911
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3912
+ workers_dev: true,
3913
+ routes: ["ENV1_ROUTE_1", "ENV2_ROUTE_2"],
3914
+ jsx_factory: "ENV_JSX_FACTORY",
3915
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3916
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3917
+ usage_model: "unbound",
3918
+ };
3919
+ const environment2: RawEnvironment = {
3920
+ name: "mock-env-name",
3921
+ account_id: "ENV_ACCOUNT_ID",
3922
+ compatibility_date: "2022-02-02",
3923
+ compatibility_flags: ["ENV_FLAG1", "ENV_FLAG2"],
3924
+ workers_dev: true,
3925
+ route: "ENV2_ROUTE_1",
3926
+ jsx_factory: "ENV_JSX_FACTORY",
3927
+ jsx_fragment: "ENV_JSX_FRAGMENT",
3928
+ triggers: { crons: ["ENV_CRON_1", "ENV_CRON_2"] },
3929
+ usage_model: "unbound",
3930
+ };
3931
+ const expectedConfig: RawConfig = {
3932
+ name: "mock-name",
3933
+ account_id: "ACCOUNT_ID",
3934
+ compatibility_date: "2022-01-01",
3935
+ compatibility_flags: ["FLAG1", "FLAG2"],
3936
+ workers_dev: false,
3937
+ route: "ROUTE_1",
3938
+ jsx_factory: "JSX_FACTORY",
3939
+ jsx_fragment: "JSX_FRAGMENT",
3940
+ triggers: { crons: ["CRON_1", "CRON_2"] },
3941
+ usage_model: "bundled",
3942
+ env: {
3943
+ ENV1: environment1,
3944
+ ENV2: environment2,
3945
+ },
3946
+ };
3947
+
3948
+ const result1 = normalizeAndValidateConfig(expectedConfig, undefined, {
3949
+ env: "ENV1",
3950
+ });
3951
+
3952
+ expect(result1.config).toEqual(expect.objectContaining(environment1));
3953
+ expect(result1.diagnostics.hasErrors()).toBe(false);
3954
+ expect(result1.diagnostics.hasWarnings()).toBe(false);
3955
+
3956
+ const result2 = normalizeAndValidateConfig(expectedConfig, undefined, {
3957
+ env: "ENV2",
3958
+ });
3959
+
3960
+ expect(result2.config).toEqual(expect.objectContaining(environment2));
3961
+ expect(result2.diagnostics.hasErrors()).toBe(false);
3962
+ expect(result2.diagnostics.hasWarnings()).toBe(false);
3963
+ });
3964
+ });
3965
+ });
3966
+ });
3967
+
3968
+ function normalizePath(text: string): string {
3969
+ return text
3970
+ .replace("project\\wrangler.toml", "project/wrangler.toml")
3971
+ .replace("src\\index.ts", "src/index.ts");
3972
+ }