@@ -96,4 +96,36 @@ describe('vm', function () {
9696 'abc'
9797 ) ;
9898 } ) ;
99+ it ( 'should have correct globalThis' , function ( ) {
100+ var sandbox = { } ;
101+ var fooValue = 42 ;
102+ Object . defineProperty ( sandbox , 'foo' , { value : fooValue } ) ;
103+ Object . defineProperty ( sandbox , 'setTimeout' , { value : setTimeout } ) ;
104+ var context = vm . createContext ( sandbox ) ;
105+ assert . strictEqual ( vm . runInContext ( 'foo' , context ) , fooValue ) ;
106+ assert . strictEqual ( vm . runInContext ( 'globalThis.foo' , context ) , fooValue ) ;
107+ var desc = vm . runInContext ( 'Reflect.getOwnPropertyDescriptor(globalThis, "setTimeout")' , context ) ;
108+ assert . ok ( desc ) ;
109+ assert . strictEqual ( desc . value , setTimeout ) ;
110+ assert . strictEqual ( desc . writable , false ) ;
111+ assert . strictEqual ( desc . enumerable , false ) ;
112+ assert . strictEqual ( desc . configurable , false ) ;
113+ } ) ;
114+ it ( 'should allow overriding globalThis' , function ( ) {
115+ var sandbox = { } ;
116+ var fooValue = 42 ;
117+ var globalThisOverride = { foo : 43 } ;
118+ Object . defineProperty ( sandbox , 'foo' , { value : fooValue } ) ;
119+ Object . defineProperty ( sandbox , 'globalThis' , { value : globalThisOverride , configurable : true , writable : true } ) ;
120+ var context = vm . createContext ( sandbox ) ;
121+ assert . strictEqual ( vm . runInContext ( 'foo' , context ) , fooValue ) ;
122+ assert . strictEqual ( vm . runInContext ( 'globalThis' , context ) , globalThisOverride ) ;
123+ assert . strictEqual ( vm . runInContext ( 'globalThis.foo' , context ) , globalThisOverride . foo ) ;
124+ assert . strictEqual ( sandbox . globalThis , globalThisOverride ) ;
125+ } ) ;
126+ it ( 'should not have "global" property' , function ( ) {
127+ var context = vm . createContext ( ) ;
128+ assert . strictEqual ( vm . runInContext ( 'typeof global' , context ) , 'undefined' ) ;
129+ assert . strictEqual ( vm . runInContext ( 'typeof globalThis.global' , context ) , 'undefined' ) ;
130+ } ) ;
99131} ) ;
0 commit comments