@@ -58,9 +58,8 @@ test("BindingsPlugin: parses options from argv", (t) => {
5858 bindings : { KEY1 : "value1" , KEY2 : "value2" } ,
5959 } ) ;
6060} ) ;
61- test ( "BindingsPlugin: parses options from wrangler config" , ( t ) => {
62- const options = parsePluginWranglerConfig ( BindingsPlugin , {
63- vars : { KEY1 : "value1" , KEY2 : "value2" , KEY3 : true , KEY4 : 42 } ,
61+ test ( "BindingsPlugin: parses options from wrangler config" , async ( t ) => {
62+ let options = parsePluginWranglerConfig ( BindingsPlugin , {
6463 wasm_modules : {
6564 MODULE1 : "module1.wasm" ,
6665 MODULE2 : "module2.wasm" ,
@@ -70,26 +69,45 @@ test("BindingsPlugin: parses options from wrangler config", (t) => {
7069 env_path : ".env.test" ,
7170 } ,
7271 } ) ;
73- t . deepEqual ( options , {
72+ t . like ( options , {
7473 envPath : ".env.test" ,
75- // Bindings should be stringified...
76- bindings : { KEY1 : "value1" , KEY2 : "value2" , KEY3 : "true" , KEY4 : "42" } ,
77- // ...but globals shouldn't
7874 globals : { KEY5 : "value5" , KEY6 : false , KEY7 : 10 } ,
7975 wasmBindings : { MODULE1 : "module1.wasm" , MODULE2 : "module2.wasm" } ,
8076 } ) ;
77+
78+ // Wrangler bindings are stored in the kWranglerBindings symbol, which isn't
79+ // exported, so setup the plugin and check they're included
80+ options = parsePluginWranglerConfig ( BindingsPlugin , {
81+ vars : { KEY1 : "value1" , KEY2 : "value2" , KEY3 : true , KEY4 : 42 } ,
82+ } ) ;
83+ const log = new NoOpLog ( ) ;
84+ const plugin = new BindingsPlugin ( log , compat , options ) ;
85+ const result = await plugin . setup ( ) ;
86+ // Wrangler bindings should be stringified
87+ t . deepEqual ( result . bindings , {
88+ KEY1 : "value1" ,
89+ KEY2 : "value2" ,
90+ KEY3 : "true" ,
91+ KEY4 : "42" ,
92+ } ) ;
8193} ) ;
8294test ( "BindingsPlugin: logs options" , ( t ) => {
95+ // wranglerOptions should contain [kWranglerBindings]
96+ const wranglerOptions = parsePluginWranglerConfig ( BindingsPlugin , {
97+ vars : { KEY1 : "value1" , KEY2 : "value2" } ,
98+ } ) ;
8399 let logs = logPluginOptions ( BindingsPlugin , {
100+ ...wranglerOptions ,
84101 envPath : ".env.custom" ,
85- bindings : { KEY1 : "value1 " , KEY2 : "value2 " } ,
86- globals : { KEY3 : "value3 " , KEY4 : "value4 " } ,
102+ bindings : { KEY3 : "value3 " , KEY4 : "value4 " } ,
103+ globals : { KEY5 : "value5 " , KEY6 : "value6 " } ,
87104 wasmBindings : { MODULE1 : "module1.wasm" , MODULE2 : "module2.wasm" } ,
88105 } ) ;
89106 t . deepEqual ( logs , [
90107 "Env Path: .env.custom" ,
91- "Custom Bindings: KEY1, KEY2" ,
92- "Custom Globals: KEY3, KEY4" ,
108+ "Wrangler Variables: KEY1, KEY2" ,
109+ "Custom Bindings: KEY3, KEY4" ,
110+ "Custom Globals: KEY5, KEY6" ,
93111 "WASM Bindings: MODULE1, MODULE2" ,
94112 ] ) ;
95113 logs = logPluginOptions ( BindingsPlugin , { envPath : true } ) ;
@@ -182,27 +200,37 @@ test("BindingsPlugin: setup: loads WebAssembly bindings", async (t) => {
182200} ) ;
183201test ( "BindingsPlugin: setup: loads bindings from all sources" , async ( t ) => {
184202 const log = new NoOpLog ( ) ;
203+
204+ // Bindings should be loaded in this order, from lowest to highest priority:
205+ // 1) Wrangler [vars]
206+ // 2) .env Variables
207+ // 3) WASM Module Bindings
208+ // 4) Custom Bindings
209+
210+ // wranglerOptions should contain [kWranglerBindings]
211+ const wranglerOptions = parsePluginWranglerConfig ( BindingsPlugin , {
212+ vars : { A : "wrangler" , B : "wrangler" , C : "wrangler" , D : "wrangler" } ,
213+ } ) ;
214+
185215 const tmp = await useTmp ( t ) ;
186216 const envPath = path . join ( tmp , ".env" ) ;
187- await fs . writeFile ( envPath , "C=c" ) ;
188- const obj = { c : 3 } ;
217+ await fs . writeFile ( envPath , "A=env\nB=env\nC=env" ) ;
218+
219+ const obj = { ping : "pong" } ;
189220 const plugin = new BindingsPlugin ( log , compat , {
221+ ...wranglerOptions ,
190222 wasmBindings : {
191223 A : addModulePath ,
192224 B : addModulePath ,
193- C : addModulePath ,
194225 } ,
195- bindings : { B : obj , C : obj } ,
226+ bindings : { A : obj } ,
196227 envPath,
197228 } ) ;
198229 const result = await plugin . setup ( ) ;
199230 assert ( result . bindings ) ;
200231
201- const instance = new WebAssembly . Instance ( result . bindings . A ) ;
202- assert ( typeof instance . exports . add === "function" ) ;
203- t . is ( instance . exports . add ( 1 , 2 ) , 3 ) ;
204-
205- t . is ( result . bindings . B , obj ) ;
206-
207- t . is ( result . bindings . C , "c" ) ;
232+ t . is ( result . bindings . D , "wrangler" ) ;
233+ t . is ( result . bindings . C , "env" ) ;
234+ t . true ( result . bindings . B instanceof WebAssembly . Module ) ;
235+ t . is ( result . bindings . A , obj ) ;
208236} ) ;
0 commit comments