11import path from "node:path" ;
22import { readConfig } from "../config" ;
33import { normalizeAndValidateConfig } from "../config/validation" ;
4+ import { mockConsoleMethods } from "./helpers/mock-console" ;
45import { normalizeString } from "./helpers/normalize" ;
56import { runInTempDir } from "./helpers/run-in-tmp" ;
6- import { writeWranglerToml } from "./helpers/write-wrangler-toml" ;
7+ import {
8+ writeExtraJson ,
9+ writeWranglerToml ,
10+ } from "./helpers/write-wrangler-toml" ;
711import type {
812 ConfigFields ,
913 RawConfig ,
@@ -12,6 +16,7 @@ import type {
1216} from "../config" ;
1317
1418describe ( "readConfig()" , ( ) => {
19+ const std = mockConsoleMethods ( ) ;
1520 runInTempDir ( ) ;
1621 it ( "should not error if a python entrypoint is used with the right compatibility_flag" , ( ) => {
1722 writeWranglerToml ( {
@@ -43,6 +48,194 @@ describe("readConfig()", () => {
4348 ) ;
4449 }
4550 } ) ;
51+
52+ describe ( "extended configuration" , ( ) => {
53+ it ( "should extend the user config with config from .wrangler/config/extra.json" , ( ) => {
54+ const main = "src/index.ts" ;
55+ const resolvedMain = path . resolve ( process . cwd ( ) , main ) ;
56+
57+ writeWranglerToml ( {
58+ main,
59+ } ) ;
60+ writeExtraJson ( {
61+ compatibility_date : "2024-11-01" ,
62+ compatibility_flags : [ "nodejs_compat" ] ,
63+ } ) ;
64+
65+ const config = readConfig ( "wrangler.toml" , { } ) ;
66+ expect ( config ) . toEqual (
67+ expect . objectContaining ( {
68+ compatibility_date : "2024-11-01" ,
69+ compatibility_flags : [ "nodejs_compat" ] ,
70+ configPath : "wrangler.toml" ,
71+ main : resolvedMain ,
72+ } )
73+ ) ;
74+
75+ expect ( std ) . toMatchInlineSnapshot ( `
76+ Object {
77+ "debug": "",
78+ "err": "",
79+ "info": "Extending with configuration found in .wrangler/config/extra.json.",
80+ "out": "",
81+ "warn": "",
82+ }
83+ ` ) ;
84+ } ) ;
85+
86+ it ( "should overwrite config with matching properties from .wrangler/config/extra.json" , ( ) => {
87+ writeWranglerToml ( {
88+ main : "src/index.js" ,
89+ compatibility_date : "2021-01-01" ,
90+ } ) ;
91+
92+ // Note that paths are relative ot the extra.json file.
93+ const main = "../../dist/index.ts" ;
94+ const resolvedMain = path . resolve (
95+ process . cwd ( ) ,
96+ ".wrangler/config" ,
97+ main
98+ ) ;
99+
100+ writeExtraJson ( {
101+ main,
102+ compatibility_date : "2024-11-01" ,
103+ compatibility_flags : [ "nodejs_compat" ] ,
104+ } ) ;
105+
106+ const config = readConfig ( "wrangler.toml" , { } ) ;
107+ expect ( config ) . toEqual (
108+ expect . objectContaining ( {
109+ compatibility_date : "2024-11-01" ,
110+ compatibility_flags : [ "nodejs_compat" ] ,
111+ main : resolvedMain ,
112+ } )
113+ ) ;
114+ } ) ;
115+
116+ it ( "should concatenate array-based config with matching properties from .wrangler/config/extra.json" , ( ) => {
117+ writeWranglerToml ( {
118+ main : "src/index.js" ,
119+ compatibility_flags : [ "allow_custom_ports" ] ,
120+ } ) ;
121+
122+ writeExtraJson ( {
123+ compatibility_flags : [ "nodejs_compat" ] ,
124+ } ) ;
125+
126+ const config = readConfig ( "wrangler.toml" , { } ) ;
127+ expect ( config ) . toEqual (
128+ expect . objectContaining ( {
129+ compatibility_flags : [ "nodejs_compat" , "allow_custom_ports" ] ,
130+ } )
131+ ) ;
132+ } ) ;
133+
134+ it ( "should merge object-based config with matching properties from .wrangler/config/extra.json" , ( ) => {
135+ writeWranglerToml ( {
136+ main : "src/index.js" ,
137+ assets : {
138+ directory : "./public" ,
139+ not_found_handling : "404-page" ,
140+ } ,
141+ } ) ;
142+
143+ writeExtraJson ( {
144+ assets : {
145+ // Note that Environment validation and typings require that directory exists,
146+ // so the extra.json would always have to provide this property
147+ // even if it just wanted to augment the rest of the object with extra properties.
148+ directory : "./public" ,
149+ binding : "ASSETS" ,
150+ } ,
151+ } ) ;
152+
153+ const config = readConfig ( "wrangler.toml" , { } ) ;
154+ expect ( config ) . toEqual (
155+ expect . objectContaining ( {
156+ assets : {
157+ binding : "ASSETS" ,
158+ directory : "./public" ,
159+ not_found_handling : "404-page" ,
160+ } ,
161+ } )
162+ ) ;
163+ } ) ;
164+
165+ it ( "should error and warn if the extra config is not valid Environment config" , ( ) => {
166+ writeWranglerToml ( { } ) ;
167+ writeExtraJson ( {
168+ compatibility_date : 2021 ,
169+ unexpected_property : true ,
170+ } as unknown as RawEnvironment ) ;
171+
172+ expect ( ( ) => readConfig ( "wrangler.toml" , { } ) )
173+ . toThrowErrorMatchingInlineSnapshot ( `
174+ [Error: Extending with configuration found in .wrangler/config/extra.json.
175+ - Expected "compatibility_date" to be of type string but got 2021.]
176+ ` ) ;
177+ expect ( std ) . toMatchInlineSnapshot ( `
178+ Object {
179+ "debug": "",
180+ "err": "",
181+ "info": "",
182+ "out": "",
183+ "warn": "[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mExtending with configuration found in .wrangler/config/extra.json.[0m
184+
185+ - Unexpected fields found in extended config field: \\"unexpected_property\\"
186+
187+ ",
188+ }
189+ ` ) ;
190+ } ) ;
191+
192+ it ( "should override the selected named environment" , ( ) => {
193+ const main = "src/index.ts" ;
194+ const resolvedMain = path . resolve ( process . cwd ( ) , main ) ;
195+
196+ writeWranglerToml ( {
197+ main,
198+ env : {
199+ prod : {
200+ compatibility_date : "2021-01-01" ,
201+ logpush : true ,
202+ } ,
203+ dev : {
204+ compatibility_date : "2022-02-02" ,
205+ no_bundle : true ,
206+ } ,
207+ } ,
208+ } ) ;
209+ writeExtraJson ( {
210+ compatibility_date : "2024-11-01" ,
211+ compatibility_flags : [ "nodejs_compat" ] ,
212+ no_bundle : true ,
213+ } ) ;
214+
215+ const prodConfig = readConfig ( "wrangler.toml" , { env : "prod" } ) ;
216+ expect ( prodConfig ) . toEqual (
217+ expect . objectContaining ( {
218+ compatibility_date : "2024-11-01" ,
219+ compatibility_flags : [ "nodejs_compat" ] ,
220+ configPath : "wrangler.toml" ,
221+ main : resolvedMain ,
222+ logpush : true ,
223+ no_bundle : true ,
224+ } )
225+ ) ;
226+
227+ const devConfig = readConfig ( "wrangler.toml" , { env : "dev" } ) ;
228+ expect ( devConfig ) . toEqual (
229+ expect . objectContaining ( {
230+ compatibility_date : "2024-11-01" ,
231+ compatibility_flags : [ "nodejs_compat" ] ,
232+ configPath : "wrangler.toml" ,
233+ main : resolvedMain ,
234+ no_bundle : true ,
235+ } )
236+ ) ;
237+ } ) ;
238+ } ) ;
46239} ) ;
47240
48241describe ( "normalizeAndValidateConfig()" , ( ) => {
0 commit comments