@@ -47,7 +47,7 @@ type InternalModuleOptions = {
47
47
48
48
type InitialModule = Partial < Module > &
49
49
Pick < Module , 'children' | 'exports' | 'filename' | 'id' | 'loaded' > ;
50
- type ModuleRegistry = { [ key : string ] : InitialModule | Module } ;
50
+ type ModuleRegistry = Map < string , InitialModule | Module > ;
51
51
type ResolveOptions = Parameters < typeof require . resolve > [ 1 ] ;
52
52
53
53
type BooleanObject = { [ key : string ] : boolean } ;
@@ -93,8 +93,8 @@ class Runtime {
93
93
private _mockMetaDataCache : {
94
94
[ key : string ] : MockFunctionMetadata < unknown , Array < unknown > > ;
95
95
} ;
96
- private _mockRegistry : { [ key : string ] : any } ;
97
- private _isolatedMockRegistry : { [ key : string ] : any } | null ;
96
+ private _mockRegistry : Map < string , any > ;
97
+ private _isolatedMockRegistry : Map < string , any > | null ;
98
98
private _moduleMocker : typeof jestMock ;
99
99
private _isolatedModuleRegistry : ModuleRegistry | null ;
100
100
private _moduleRegistry : ModuleRegistry ;
@@ -127,15 +127,15 @@ class Runtime {
127
127
this . _currentlyExecutingModulePath = '' ;
128
128
this . _environment = environment ;
129
129
this . _explicitShouldMock = Object . create ( null ) ;
130
- this . _internalModuleRegistry = Object . create ( null ) ;
130
+ this . _internalModuleRegistry = new Map ( ) ;
131
131
this . _isCurrentlyExecutingManualMock = null ;
132
132
this . _mockFactories = Object . create ( null ) ;
133
- this . _mockRegistry = Object . create ( null ) ;
133
+ this . _mockRegistry = new Map ( ) ;
134
134
// during setup, this cannot be null (and it's fine to explode if it is)
135
135
this . _moduleMocker = this . _environment . moduleMocker ! ;
136
136
this . _isolatedModuleRegistry = null ;
137
137
this . _isolatedMockRegistry = null ;
138
- this . _moduleRegistry = Object . create ( null ) ;
138
+ this . _moduleRegistry = new Map ( ) ;
139
139
this . _needsCoverageMapped = new Set ( ) ;
140
140
this . _resolver = resolver ;
141
141
this . _scriptTransformer = new ScriptTransformer ( config ) ;
@@ -291,7 +291,7 @@ class Runtime {
291
291
from ,
292
292
moduleName ,
293
293
) ;
294
- let modulePath ;
294
+ let modulePath : string | undefined ;
295
295
296
296
// Some old tests rely on this mocking behavior. Ideally we'll change this
297
297
// to be more explicit.
@@ -320,7 +320,10 @@ class Runtime {
320
320
let moduleRegistry ;
321
321
322
322
if ( ! options || ! options . isInternalModule ) {
323
- if ( this . _moduleRegistry [ modulePath ] || ! this . _isolatedModuleRegistry ) {
323
+ if (
324
+ this . _moduleRegistry . get ( modulePath ) ||
325
+ ! this . _isolatedModuleRegistry
326
+ ) {
324
327
moduleRegistry = this . _moduleRegistry ;
325
328
} else {
326
329
moduleRegistry = this . _isolatedModuleRegistry ;
@@ -329,29 +332,33 @@ class Runtime {
329
332
moduleRegistry = this . _internalModuleRegistry ;
330
333
}
331
334
332
- if ( ! moduleRegistry [ modulePath ] ) {
333
- // We must register the pre-allocated module object first so that any
334
- // circular dependencies that may arise while evaluating the module can
335
- // be satisfied.
336
- const localModule : InitialModule = {
337
- children : [ ] ,
338
- exports : { } ,
339
- filename : modulePath ,
340
- id : modulePath ,
341
- loaded : false ,
342
- } ;
343
- moduleRegistry [ modulePath ] = localModule ;
344
-
345
- this . _loadModule (
346
- localModule ,
347
- from ,
348
- moduleName ,
349
- modulePath ,
350
- options ,
351
- moduleRegistry ,
352
- ) ;
335
+ const module = moduleRegistry . get ( modulePath ) ;
336
+ if ( module ) {
337
+ return module . exports ;
353
338
}
354
- return moduleRegistry [ modulePath ] . exports ;
339
+
340
+ // We must register the pre-allocated module object first so that any
341
+ // circular dependencies that may arise while evaluating the module can
342
+ // be satisfied.
343
+ const localModule : InitialModule = {
344
+ children : [ ] ,
345
+ exports : { } ,
346
+ filename : modulePath ,
347
+ id : modulePath ,
348
+ loaded : false ,
349
+ } ;
350
+ moduleRegistry . set ( modulePath , localModule ) ;
351
+
352
+ this . _loadModule (
353
+ localModule ,
354
+ from ,
355
+ moduleName ,
356
+ modulePath ,
357
+ options ,
358
+ moduleRegistry ,
359
+ ) ;
360
+
361
+ return localModule . exports ;
355
362
}
356
363
357
364
requireInternalModule ( from : Config . Path , to ?: string ) {
@@ -369,16 +376,21 @@ class Runtime {
369
376
moduleName ,
370
377
) ;
371
378
372
- if ( this . _isolatedMockRegistry && this . _isolatedMockRegistry [ moduleID ] ) {
373
- return this . _isolatedMockRegistry [ moduleID ] ;
374
- } else if ( this . _mockRegistry [ moduleID ] ) {
375
- return this . _mockRegistry [ moduleID ] ;
379
+ if (
380
+ this . _isolatedMockRegistry &&
381
+ this . _isolatedMockRegistry . get ( moduleID )
382
+ ) {
383
+ return this . _isolatedMockRegistry . get ( moduleID ) ;
384
+ } else if ( this . _mockRegistry . get ( moduleID ) ) {
385
+ return this . _mockRegistry . get ( moduleID ) ;
376
386
}
377
387
378
388
const mockRegistry = this . _isolatedMockRegistry || this . _mockRegistry ;
379
389
380
390
if ( moduleID in this . _mockFactories ) {
381
- return ( mockRegistry [ moduleID ] = this . _mockFactories [ moduleID ] ( ) ) ;
391
+ const module = this . _mockFactories [ moduleID ] ( ) ;
392
+ mockRegistry . set ( moduleID , module ) ;
393
+ return module ;
382
394
}
383
395
384
396
const manualMockOrStub = this . _resolver . getMockModule ( from , moduleName ) ;
@@ -435,13 +447,13 @@ class Runtime {
435
447
mockRegistry ,
436
448
) ;
437
449
438
- mockRegistry [ moduleID ] = localModule . exports ;
450
+ mockRegistry . set ( moduleID , localModule . exports ) ;
439
451
} else {
440
452
// Look for a real module to generate an automock from
441
- mockRegistry [ moduleID ] = this . _generateMock ( from , moduleName ) ;
453
+ mockRegistry . set ( moduleID , this . _generateMock ( from , moduleName ) ) ;
442
454
}
443
455
444
- return mockRegistry [ moduleID ] ;
456
+ return mockRegistry . get ( moduleID ) ;
445
457
}
446
458
447
459
private _loadModule (
@@ -495,8 +507,8 @@ class Runtime {
495
507
'isolateModules cannot be nested inside another isolateModules.' ,
496
508
) ;
497
509
}
498
- this . _isolatedModuleRegistry = Object . create ( null ) ;
499
- this . _isolatedMockRegistry = Object . create ( null ) ;
510
+ this . _isolatedModuleRegistry = new Map ( ) ;
511
+ this . _isolatedMockRegistry = new Map ( ) ;
500
512
fn ( ) ;
501
513
this . _isolatedModuleRegistry = null ;
502
514
this . _isolatedMockRegistry = null ;
@@ -505,8 +517,8 @@ class Runtime {
505
517
resetModules ( ) {
506
518
this . _isolatedModuleRegistry = null ;
507
519
this . _isolatedMockRegistry = null ;
508
- this . _mockRegistry = Object . create ( null ) ;
509
- this . _moduleRegistry = Object . create ( null ) ;
520
+ this . _mockRegistry = new Map ( ) ;
521
+ this . _moduleRegistry = new Map ( ) ;
510
522
511
523
if ( this . _environment ) {
512
524
if ( this . _environment . global ) {
@@ -678,7 +690,7 @@ class Runtime {
678
690
enumerable : true ,
679
691
get ( ) {
680
692
const key = from || '' ;
681
- return moduleRegistry [ key ] || null ;
693
+ return moduleRegistry . get ( key ) || null ;
682
694
} ,
683
695
} ) ;
684
696
@@ -770,8 +782,8 @@ class Runtime {
770
782
// mocked has calls into side-effectful APIs on another module.
771
783
const origMockRegistry = this . _mockRegistry ;
772
784
const origModuleRegistry = this . _moduleRegistry ;
773
- this . _mockRegistry = Object . create ( null ) ;
774
- this . _moduleRegistry = Object . create ( null ) ;
785
+ this . _mockRegistry = new Map ( ) ;
786
+ this . _moduleRegistry = new Map ( ) ;
775
787
776
788
const moduleExports = this . requireModule ( from , moduleName ) ;
777
789
0 commit comments