@@ -716,6 +716,8 @@ export default class Runtime {
716
716
async unstable_importModule (
717
717
from : string ,
718
718
moduleName ?: string ,
719
+ // TODO: implement this
720
+ _isImportActual = false ,
719
721
) : Promise < void > {
720
722
invariant (
721
723
runtimeSupportsVmModules ,
@@ -793,6 +795,7 @@ export default class Runtime {
793
795
this . setExport ( key , value ) ;
794
796
} ) ;
795
797
} ,
798
+ // should identifier be `node://${moduleName}`?
796
799
{ context, identifier : moduleName } ,
797
800
) ;
798
801
@@ -801,7 +804,69 @@ export default class Runtime {
801
804
return evaluateSyntheticModule ( module ) ;
802
805
}
803
806
804
- throw new Error ( 'Attempting to import a mock without a factory' ) ;
807
+ const manualMockOrStub = this . _resolver . getMockModule ( from , moduleName ) ;
808
+
809
+ let modulePath =
810
+ this . _resolver . getMockModule ( from , moduleName ) ||
811
+ this . _resolveModule ( from , moduleName ) ;
812
+
813
+ let isManualMock =
814
+ manualMockOrStub &&
815
+ ! this . _resolver . resolveStubModuleName ( from , moduleName ) ;
816
+ if ( ! isManualMock ) {
817
+ // If the actual module file has a __mocks__ dir sitting immediately next
818
+ // to it, look to see if there is a manual mock for this file.
819
+ //
820
+ // subDir1/my_module.js
821
+ // subDir1/__mocks__/my_module.js
822
+ // subDir2/my_module.js
823
+ // subDir2/__mocks__/my_module.js
824
+ //
825
+ // Where some other module does a relative require into each of the
826
+ // respective subDir{1,2} directories and expects a manual mock
827
+ // corresponding to that particular my_module.js file.
828
+
829
+ const moduleDir = path . dirname ( modulePath ) ;
830
+ const moduleFileName = path . basename ( modulePath ) ;
831
+ const potentialManualMock = path . join (
832
+ moduleDir ,
833
+ '__mocks__' ,
834
+ moduleFileName ,
835
+ ) ;
836
+ if ( fs . existsSync ( potentialManualMock ) ) {
837
+ isManualMock = true ;
838
+ modulePath = potentialManualMock ;
839
+ }
840
+ }
841
+ if ( isManualMock ) {
842
+ const localModule : InitialModule = {
843
+ children : [ ] ,
844
+ exports : { } ,
845
+ filename : modulePath ,
846
+ id : modulePath ,
847
+ loaded : false ,
848
+ path : modulePath ,
849
+ } ;
850
+
851
+ this . _loadModule (
852
+ localModule ,
853
+ from ,
854
+ moduleName ,
855
+ modulePath ,
856
+ undefined ,
857
+ this . _moduleMockRegistry ,
858
+ ) ;
859
+
860
+ this . _moduleMockRegistry . set ( moduleID , localModule . exports ) ;
861
+ } else {
862
+ // Look for a real module to generate an automock from
863
+ this . _moduleMockRegistry . set (
864
+ moduleID ,
865
+ this . _generateMock ( from , moduleName ) ,
866
+ ) ;
867
+ }
868
+
869
+ return this . _moduleMockRegistry . get ( moduleID ) ;
805
870
}
806
871
807
872
private getExportsOfCjs ( modulePath : string ) {
@@ -2041,16 +2106,22 @@ export default class Runtime {
2041
2106
this . setMock ( from , moduleName , mockFactory , options ) ;
2042
2107
return jestObject ;
2043
2108
} ;
2044
- const mockModule : Jest [ 'unstable_mockModule ' ] = (
2109
+ const mockModule : Jest [ 'mockModule ' ] = (
2045
2110
moduleName ,
2046
2111
mockFactory ,
2047
2112
options ,
2048
2113
) => {
2049
- if ( typeof mockFactory !== 'function' ) {
2050
- throw new Error ( '`unstable_mockModule` must be passed a mock factory' ) ;
2114
+ if ( mockFactory !== undefined ) {
2115
+ this . setModuleMock ( from , moduleName , mockFactory , options ) ;
2116
+ return jestObject ;
2051
2117
}
2052
2118
2053
- this . setModuleMock ( from , moduleName , mockFactory , options ) ;
2119
+ const moduleID = this . _resolver . getModuleID (
2120
+ this . _virtualMocks ,
2121
+ from ,
2122
+ moduleName ,
2123
+ ) ;
2124
+ this . _explicitShouldMockModule . set ( moduleID , true ) ;
2054
2125
return jestObject ;
2055
2126
} ;
2056
2127
const clearAllMocks = ( ) => {
@@ -2161,6 +2232,7 @@ export default class Runtime {
2161
2232
isolateModules,
2162
2233
mock,
2163
2234
mocked,
2235
+ mockModule,
2164
2236
requireActual : this . requireActual . bind ( this , from ) ,
2165
2237
requireMock : this . requireMock . bind ( this , from ) ,
2166
2238
resetAllMocks,
@@ -2197,7 +2269,6 @@ export default class Runtime {
2197
2269
setTimeout,
2198
2270
spyOn,
2199
2271
unmock,
2200
- unstable_mockModule : mockModule ,
2201
2272
useFakeTimers,
2202
2273
useRealTimers,
2203
2274
} ;
0 commit comments