@@ -38,15 +38,6 @@ describe('Backend Functions - generateVirtualEntryContent', () => {
3838 expect ( result ) . toContain ( 'export async function main($)' ) ;
3939 } ) ;
4040
41- test ( 'Should set globalThis.$ = $' , ( ) => {
42- const result = generateVirtualEntryContent (
43- 'myHandler' ,
44- '/src/handler.ts' ,
45- PROJECT_ROOT ,
46- ) ;
47- expect ( result ) . toContain ( 'globalThis.$ = $' ) ;
48- } ) ;
49-
5041 test ( 'Should read args from $.backendFunctionArgs (no source-text substitution)' , ( ) => {
5142 const result = generateVirtualEntryContent (
5243 'myHandler' ,
@@ -176,8 +167,7 @@ describe('Backend Functions - args round-trip via $.backendFunctionArgs', () =>
176167 . join ( '\n' )
177168 // Replace the call site with a $-bound handler we control.
178169 . replace ( / a w a i t m y H a n d l e r \( \. \. \. a r g s \) / , 'await $.__handler(...args)' ) ;
179- // The generated code declares globalThis.$, which has no effect in this
180- // sandbox but is harmless. Wrap the body so we can return main().
170+ // Wrap the executable body so we can return main().
181171 const wrapper = `${ body } \nreturn main($);` ;
182172 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
183173 const fn = new Function (
@@ -229,4 +219,18 @@ describe('Backend Functions - args round-trip via $.backendFunctionArgs', () =>
229219 await main ( { } ) ;
230220 expect ( received ) . toEqual ( [ ] ) ;
231221 } ) ;
222+
223+ test ( 'Should keep the runtime context out of customer global scope' , async ( ) => {
224+ const source = generateVirtualEntryContent ( 'myHandler' , '/src/handler.ts' , PROJECT_ROOT ) ;
225+ let exposedContext : unknown ;
226+ const handler = ( ) => {
227+ exposedContext = Reflect . get ( global , '$' ) ;
228+ return 'ok' ;
229+ } ;
230+ const main = buildMainFromSource ( source , handler ) ;
231+
232+ await main ( { backendFunctionArgs : [ ] , privateRuntimeValue : 'secret' } ) ;
233+
234+ expect ( exposedContext ) . toBeUndefined ( ) ;
235+ } ) ;
232236} ) ;
0 commit comments