@@ -12,6 +12,16 @@ const {NodeVM, VMScript, makeResolverFromLegacyOptions} = require('..');
12
12
13
13
global . isHost = true ;
14
14
15
+ function isVMProxy ( obj ) {
16
+ const key = { } ;
17
+ const proto = Object . getPrototypeOf ( obj ) ;
18
+ if ( ! proto ) return undefined ;
19
+ proto . isVMProxy = key ;
20
+ const proxy = obj . isVMProxy !== key ;
21
+ delete proto . isVMProxy ;
22
+ return proxy ;
23
+ }
24
+
15
25
describe ( 'NodeVM' , ( ) => {
16
26
let vm ;
17
27
@@ -228,7 +238,7 @@ describe('modules', () => {
228
238
assert . ok ( vm . run ( "require('module1')" , __filename ) ) ;
229
239
} ) ;
230
240
231
- it ( 'allows choosing a context by path' , ( ) => {
241
+ it ( 'allows choosing a context by path legacy ' , ( ) => {
232
242
const vm = new NodeVM ( {
233
243
require : {
234
244
external : {
@@ -241,15 +251,20 @@ describe('modules', () => {
241
251
}
242
252
}
243
253
} ) ;
244
- function isVMProxy ( obj ) {
245
- const key = { } ;
246
- const proto = Object . getPrototypeOf ( obj ) ;
247
- if ( ! proto ) return undefined ;
248
- proto . isVMProxy = key ;
249
- const proxy = obj . isVMProxy !== key ;
250
- delete proto . isVMProxy ;
251
- return proxy ;
252
- }
254
+ assert . equal ( isVMProxy ( vm . run ( "module.exports = require('mocha')" , __filename ) ) , false , 'Mocha is a proxy' ) ;
255
+ assert . equal ( isVMProxy ( vm . run ( "module.exports = require('module1')" , __filename ) ) , true , 'Module1 is not a proxy' ) ;
256
+ } ) ;
257
+
258
+ it ( 'allows choosing a context by path' , ( ) => {
259
+ const vm = new NodeVM ( {
260
+ require : {
261
+ external : true ,
262
+ context ( module ) {
263
+ if ( module . includes ( 'mocha' ) ) return 'host' ;
264
+ return 'sandbox' ;
265
+ }
266
+ }
267
+ } ) ;
253
268
assert . equal ( isVMProxy ( vm . run ( "module.exports = require('mocha')" , __filename ) ) , false , 'Mocha is a proxy' ) ;
254
269
assert . equal ( isVMProxy ( vm . run ( "module.exports = require('module1')" , __filename ) ) , true , 'Module1 is not a proxy' ) ;
255
270
} ) ;
0 commit comments