@@ -150,6 +150,7 @@ class Runtime {
150150 | null ;
151151 private _internalModuleRegistry : ModuleRegistry ;
152152 private _isCurrentlyExecutingManualMock : string | null ;
153+ private _mainModule : Module | null ;
153154 private _mockFactories : Map < string , ( ) => unknown > ;
154155 private _mockMetaDataCache : Map <
155156 string ,
@@ -202,6 +203,7 @@ class Runtime {
202203 this . _explicitShouldMock = new Map ( ) ;
203204 this . _internalModuleRegistry = new Map ( ) ;
204205 this . _isCurrentlyExecutingManualMock = null ;
206+ this . _mainModule = null ;
205207 this . _mockFactories = new Map ( ) ;
206208 this . _mockRegistry = new Map ( ) ;
207209 // during setup, this cannot be null (and it's fine to explode if it is)
@@ -891,6 +893,7 @@ class Runtime {
891893 this . resetModules ( ) ;
892894
893895 this . _internalModuleRegistry . clear ( ) ;
896+ this . _mainModule = null ;
894897 this . _mockFactories . clear ( ) ;
895898 this . _mockMetaDataCache . clear ( ) ;
896899 this . _shouldMockModuleCache . clear ( ) ;
@@ -1064,6 +1067,15 @@ class Runtime {
10641067 } ) ,
10651068 ] ;
10661069
1070+ if ( ! this . _mainModule && filename === this . _testPath ) {
1071+ this . _mainModule = module ;
1072+ }
1073+
1074+ Object . defineProperty ( module , 'main' , {
1075+ enumerable : true ,
1076+ value : this . _mainModule ,
1077+ } ) ;
1078+
10671079 try {
10681080 compiledFunction . call (
10691081 module . exports ,
0 commit comments