@@ -305,6 +305,7 @@ const describeCases = config => {
305305 if ( testConfig . beforeExecute ) testConfig . beforeExecute ( ) ;
306306 const results = [ ] ;
307307 for ( let i = 0 ; i < optionsArr . length ; i ++ ) {
308+ const options = optionsArr [ i ] ;
308309 const bundlePath = testConfig . findBundle ( i , optionsArr [ i ] ) ;
309310 if ( bundlePath ) {
310311 filesCount ++ ;
@@ -327,6 +328,43 @@ const describeCases = config => {
327328 const requireCache = Object . create ( null ) ;
328329 const esmCache = new Map ( ) ;
329330 const esmIdentifier = `${ category . name } -${ testName } -${ i } ` ;
331+ const baseModuleScope = {
332+ console : console ,
333+ it : _it ,
334+ beforeEach : _beforeEach ,
335+ afterEach : _afterEach ,
336+ expect,
337+ jest,
338+ __STATS__ : jsonStats ,
339+ nsObj : m => {
340+ Object . defineProperty ( m , Symbol . toStringTag , {
341+ value : "Module"
342+ } ) ;
343+ return m ;
344+ }
345+ } ;
346+
347+ let runInNewContext = false ;
348+ if (
349+ options . target === "web" ||
350+ options . target === "webworker"
351+ ) {
352+ baseModuleScope . window = globalContext ;
353+ baseModuleScope . self = globalContext ;
354+ baseModuleScope . URL = URL ;
355+ baseModuleScope . Worker =
356+ require ( "./helpers/createFakeWorker" ) ( {
357+ outputDirectory
358+ } ) ;
359+ runInNewContext = true ;
360+ }
361+ if ( testConfig . moduleScope ) {
362+ testConfig . moduleScope ( baseModuleScope ) ;
363+ }
364+ const esmContext = vm . createContext ( baseModuleScope , {
365+ name : "context for esm"
366+ } ) ;
367+
330368 // eslint-disable-next-line no-loop-func
331369 const _require = (
332370 currentDirectory ,
@@ -380,41 +418,7 @@ const describeCases = config => {
380418 options . experiments &&
381419 options . experiments . outputModule ;
382420
383- let runInNewContext = false ;
384-
385- const moduleScope = {
386- console : console ,
387- it : _it ,
388- beforeEach : _beforeEach ,
389- afterEach : _afterEach ,
390- expect,
391- jest,
392- __STATS__ : jsonStats ,
393- nsObj : m => {
394- Object . defineProperty ( m , Symbol . toStringTag , {
395- value : "Module"
396- } ) ;
397- return m ;
398- }
399- } ;
400-
401- if (
402- options . target === "web" ||
403- options . target === "webworker"
404- ) {
405- moduleScope . window = globalContext ;
406- moduleScope . self = globalContext ;
407- moduleScope . URL = URL ;
408- moduleScope . Worker =
409- require ( "./helpers/createFakeWorker" ) ( {
410- outputDirectory
411- } ) ;
412- runInNewContext = true ;
413- }
414421 if ( isModule ) {
415- if ( testConfig . moduleScope ) {
416- testConfig . moduleScope ( moduleScope ) ;
417- }
418422 if ( ! vm . SourceTextModule )
419423 throw new Error (
420424 "Running this test requires '--experimental-vm-modules'.\nRun with 'node --experimental-vm-modules node_modules/jest-cli/bin/jest'."
@@ -424,11 +428,7 @@ const describeCases = config => {
424428 esm = new vm . SourceTextModule ( content , {
425429 identifier : esmIdentifier + "-" + p ,
426430 url : pathToFileURL ( p ) . href + "?" + esmIdentifier ,
427- context :
428- ( parentModule && parentModule . context ) ||
429- vm . createContext ( moduleScope , {
430- name : `context for ${ p } `
431- } ) ,
431+ context : esmContext ,
432432 initializeImportMeta : ( meta , module ) => {
433433 meta . url = pathToFileURL ( p ) . href ;
434434 } ,
@@ -488,7 +488,8 @@ const describeCases = config => {
488488 exports : { }
489489 } ;
490490 requireCache [ p ] = m ;
491- Object . assign ( moduleScope , {
491+ const moduleScope = {
492+ ...baseModuleScope ,
492493 require : _require . bind (
493494 null ,
494495 path . dirname ( p ) ,
@@ -511,7 +512,7 @@ const describeCases = config => {
511512 __dirname : path . dirname ( p ) ,
512513 __filename : p ,
513514 _globalAssign : { expect }
514- } ) ;
515+ } ;
515516 if ( testConfig . moduleScope ) {
516517 testConfig . moduleScope ( moduleScope ) ;
517518 }
@@ -549,14 +550,14 @@ const describeCases = config => {
549550 results . push (
550551 _require (
551552 outputDirectory ,
552- optionsArr [ i ] ,
553+ options ,
553554 "./" + bundlePathItem
554555 )
555556 ) ;
556557 }
557558 } else {
558559 results . push (
559- _require ( outputDirectory , optionsArr [ i ] , bundlePath )
560+ _require ( outputDirectory , options , bundlePath )
560561 ) ;
561562 }
562563 }
0 commit comments