@@ -325,6 +325,8 @@ const describeCases = config => {
325
325
} ;
326
326
327
327
const requireCache = Object . create ( null ) ;
328
+ const esmCache = new Map ( ) ;
329
+ const esmIdentifier = `${ category . name } -${ testName } -${ i } ` ;
328
330
// eslint-disable-next-line no-loop-func
329
331
const _require = (
330
332
currentDirectory ,
@@ -335,7 +337,7 @@ const describeCases = config => {
335
337
) => {
336
338
if ( testConfig === undefined ) {
337
339
throw new Error (
338
- `_require(${ module } ) called after all tests have completed`
340
+ `_require(${ module } ) called after all tests from ${ category . name } ${ testName } have completed`
339
341
) ;
340
342
}
341
343
if ( Array . isArray ( module ) || / ^ \. \. ? \/ / . test ( module ) ) {
@@ -373,16 +375,15 @@ const describeCases = config => {
373
375
) ;
374
376
}
375
377
}
376
- if ( p in requireCache ) {
377
- return requireCache [ p ] . exports ;
378
- }
379
- const m = {
380
- exports : { }
381
- } ;
382
- requireCache [ p ] = m ;
378
+ const isModule =
379
+ p . endsWith ( ".mjs" ) &&
380
+ options . experiments &&
381
+ options . experiments . outputModule ;
382
+
383
383
let runInNewContext = false ;
384
384
385
385
const moduleScope = {
386
+ console : console ,
386
387
it : _it ,
387
388
beforeEach : _beforeEach ,
388
389
afterEach : _afterEach ,
@@ -396,36 +397,7 @@ const describeCases = config => {
396
397
return m ;
397
398
}
398
399
} ;
399
- const isModule =
400
- p . endsWith ( ".mjs" ) &&
401
- options . experiments &&
402
- options . experiments . outputModule ;
403
- if ( ! isModule ) {
404
- Object . assign ( moduleScope , {
405
- require : _require . bind (
406
- null ,
407
- path . dirname ( p ) ,
408
- options
409
- ) ,
410
- importScripts : url => {
411
- expect ( url ) . toMatch (
412
- / ^ h t t p s : \/ \/ t e s t \. c a s e s \/ p a t h \/ /
413
- ) ;
414
- _require (
415
- outputDirectory ,
416
- options ,
417
- `.${ url . slice (
418
- "https://test.cases/path" . length
419
- ) } `
420
- ) ;
421
- } ,
422
- module : m ,
423
- exports : m . exports ,
424
- __dirname : path . dirname ( p ) ,
425
- __filename : p ,
426
- _globalAssign : { expect }
427
- } ) ;
428
- }
400
+
429
401
if (
430
402
options . target === "web" ||
431
403
options . target === "webworker"
@@ -439,48 +411,55 @@ const describeCases = config => {
439
411
} ) ;
440
412
runInNewContext = true ;
441
413
}
442
- if ( testConfig . moduleScope ) {
443
- testConfig . moduleScope ( moduleScope ) ;
444
- }
445
414
if ( isModule ) {
415
+ if ( testConfig . moduleScope ) {
416
+ testConfig . moduleScope ( moduleScope ) ;
417
+ }
446
418
if ( ! vm . SourceTextModule )
447
419
throw new Error (
448
420
"Running this test requires '--experimental-vm-modules'.\nRun with 'node --experimental-vm-modules node_modules/jest-cli/bin/jest'."
449
421
) ;
450
- const esm = new vm . SourceTextModule ( content , {
451
- identifier : p ,
452
- url : pathToFileURL ( p ) . href ,
453
- context :
454
- ( parentModule && parentModule . context ) ||
455
- vm . createContext ( moduleScope , {
456
- name : `context for ${ p } `
457
- } ) ,
458
- initializeImportMeta : ( meta , module ) => {
459
- meta . url = pathToFileURL ( p ) . href ;
460
- } ,
461
- importModuleDynamically : async (
462
- specifier ,
463
- module
464
- ) => {
465
- const result = await _require (
466
- path . dirname ( p ) ,
467
- options ,
422
+ let esm = esmCache . get ( p ) ;
423
+ if ( ! esm ) {
424
+ esm = new vm . SourceTextModule ( content , {
425
+ identifier : esmIdentifier + "-" + p ,
426
+ url : pathToFileURL ( p ) . href + "?" + esmIdentifier ,
427
+ context :
428
+ ( parentModule && parentModule . context ) ||
429
+ vm . createContext ( moduleScope , {
430
+ name : `context for ${ p } `
431
+ } ) ,
432
+ initializeImportMeta : ( meta , module ) => {
433
+ meta . url = pathToFileURL ( p ) . href ;
434
+ } ,
435
+ importModuleDynamically : async (
468
436
specifier ,
469
- "evaluated" ,
470
437
module
471
- ) ;
472
- return await asModule ( result , module . context ) ;
473
- }
474
- } ) ;
438
+ ) => {
439
+ const result = await _require (
440
+ path . dirname ( p ) ,
441
+ options ,
442
+ specifier ,
443
+ "evaluated" ,
444
+ module
445
+ ) ;
446
+ return await asModule ( result , module . context ) ;
447
+ }
448
+ } ) ;
449
+ esmCache . set ( p , esm ) ;
450
+ }
475
451
if ( esmMode === "unlinked" ) return esm ;
476
452
return ( async ( ) => {
477
453
await esm . link (
478
454
async ( specifier , referencingModule ) => {
479
455
return await asModule (
480
456
await _require (
481
457
path . dirname (
482
- referencingModule . identifier ||
483
- fileURLToPath ( referencingModule . url )
458
+ referencingModule . identifier
459
+ ? referencingModule . identifier . slice (
460
+ esmIdentifier . length + 1
461
+ )
462
+ : fileURLToPath ( referencingModule . url )
484
463
) ,
485
464
options ,
486
465
specifier ,
@@ -502,6 +481,40 @@ const describeCases = config => {
502
481
: ns ;
503
482
} ) ( ) ;
504
483
} else {
484
+ if ( p in requireCache ) {
485
+ return requireCache [ p ] . exports ;
486
+ }
487
+ const m = {
488
+ exports : { }
489
+ } ;
490
+ requireCache [ p ] = m ;
491
+ Object . assign ( moduleScope , {
492
+ require : _require . bind (
493
+ null ,
494
+ path . dirname ( p ) ,
495
+ options
496
+ ) ,
497
+ importScripts : url => {
498
+ expect ( url ) . toMatch (
499
+ / ^ h t t p s : \/ \/ t e s t \. c a s e s \/ p a t h \/ /
500
+ ) ;
501
+ _require (
502
+ outputDirectory ,
503
+ options ,
504
+ `.${ url . slice (
505
+ "https://test.cases/path" . length
506
+ ) } `
507
+ ) ;
508
+ } ,
509
+ module : m ,
510
+ exports : m . exports ,
511
+ __dirname : path . dirname ( p ) ,
512
+ __filename : p ,
513
+ _globalAssign : { expect }
514
+ } ) ;
515
+ if ( testConfig . moduleScope ) {
516
+ testConfig . moduleScope ( moduleScope ) ;
517
+ }
505
518
if ( ! runInNewContext )
506
519
content = `Object.assign(global, _globalAssign); ${ content } ` ;
507
520
const args = Object . keys ( moduleScope ) ;
@@ -517,8 +530,8 @@ const describeCases = config => {
517
530
: vm . runInThisContext ( code , p ) ;
518
531
fn . call ( m . exports , ...argValues ) ;
519
532
document . currentScript = oldCurrentScript ;
533
+ return m . exports ;
520
534
}
521
- return m . exports ;
522
535
} else if (
523
536
testConfig . modules &&
524
537
module in testConfig . modules
0 commit comments