@@ -581,6 +581,8 @@ export default class Runtime {
581
581
async unstable_importModule (
582
582
from : Config . Path ,
583
583
moduleName ?: string ,
584
+ // TODO: implement this
585
+ _isImportActual = false ,
584
586
) : Promise < void > {
585
587
invariant (
586
588
runtimeSupportsVmModules ,
@@ -659,6 +661,7 @@ export default class Runtime {
659
661
this . setExport ( key , value ) ;
660
662
} ) ;
661
663
} ,
664
+ // should identifier be `node://${moduleName}`?
662
665
{ context, identifier : moduleName } ,
663
666
) ;
664
667
@@ -667,7 +670,69 @@ export default class Runtime {
667
670
return evaluateSyntheticModule ( module ) ;
668
671
}
669
672
670
- throw new Error ( 'Attempting to import a mock without a factory' ) ;
673
+ const manualMockOrStub = this . _resolver . getMockModule ( from , moduleName ) ;
674
+
675
+ let modulePath =
676
+ this . _resolver . getMockModule ( from , moduleName ) ||
677
+ this . _resolveModule ( from , moduleName ) ;
678
+
679
+ let isManualMock =
680
+ manualMockOrStub &&
681
+ ! this . _resolver . resolveStubModuleName ( from , moduleName ) ;
682
+ if ( ! isManualMock ) {
683
+ // If the actual module file has a __mocks__ dir sitting immediately next
684
+ // to it, look to see if there is a manual mock for this file.
685
+ //
686
+ // subDir1/my_module.js
687
+ // subDir1/__mocks__/my_module.js
688
+ // subDir2/my_module.js
689
+ // subDir2/__mocks__/my_module.js
690
+ //
691
+ // Where some other module does a relative require into each of the
692
+ // respective subDir{1,2} directories and expects a manual mock
693
+ // corresponding to that particular my_module.js file.
694
+
695
+ const moduleDir = path . dirname ( modulePath ) ;
696
+ const moduleFileName = path . basename ( modulePath ) ;
697
+ const potentialManualMock = path . join (
698
+ moduleDir ,
699
+ '__mocks__' ,
700
+ moduleFileName ,
701
+ ) ;
702
+ if ( fs . existsSync ( potentialManualMock ) ) {
703
+ isManualMock = true ;
704
+ modulePath = potentialManualMock ;
705
+ }
706
+ }
707
+ if ( isManualMock ) {
708
+ const localModule : InitialModule = {
709
+ children : [ ] ,
710
+ exports : { } ,
711
+ filename : modulePath ,
712
+ id : modulePath ,
713
+ loaded : false ,
714
+ path : modulePath ,
715
+ } ;
716
+
717
+ this . _loadModule (
718
+ localModule ,
719
+ from ,
720
+ moduleName ,
721
+ modulePath ,
722
+ undefined ,
723
+ this . _moduleMockRegistry ,
724
+ ) ;
725
+
726
+ this . _moduleMockRegistry . set ( moduleID , localModule . exports ) ;
727
+ } else {
728
+ // Look for a real module to generate an automock from
729
+ this . _moduleMockRegistry . set (
730
+ moduleID ,
731
+ this . _generateMock ( from , moduleName ) ,
732
+ ) ;
733
+ }
734
+
735
+ return this . _moduleMockRegistry . get ( moduleID ) ;
671
736
}
672
737
673
738
private getExportsOfCjs ( modulePath : Config . Path ) {
@@ -1767,16 +1832,22 @@ export default class Runtime {
1767
1832
this . setMock ( from , moduleName , mockFactory , options ) ;
1768
1833
return jestObject ;
1769
1834
} ;
1770
- const mockModule : Jest [ 'unstable_mockModule ' ] = (
1835
+ const mockModule : Jest [ 'mockModule ' ] = (
1771
1836
moduleName ,
1772
1837
mockFactory ,
1773
1838
options ,
1774
1839
) => {
1775
- if ( typeof mockFactory !== 'function' ) {
1776
- throw new Error ( '`unstable_mockModule` must be passed a mock factory' ) ;
1840
+ if ( mockFactory !== undefined ) {
1841
+ this . setModuleMock ( from , moduleName , mockFactory , options ) ;
1842
+ return jestObject ;
1777
1843
}
1778
1844
1779
- this . setModuleMock ( from , moduleName , mockFactory , options ) ;
1845
+ const moduleID = this . _resolver . getModuleID (
1846
+ this . _virtualMocks ,
1847
+ from ,
1848
+ moduleName ,
1849
+ ) ;
1850
+ this . _explicitShouldMockModule . set ( moduleID , true ) ;
1780
1851
return jestObject ;
1781
1852
} ;
1782
1853
const clearAllMocks = ( ) => {
@@ -1877,6 +1948,7 @@ export default class Runtime {
1877
1948
isMockFunction : this . _moduleMocker . isMockFunction ,
1878
1949
isolateModules,
1879
1950
mock,
1951
+ mockModule,
1880
1952
requireActual : this . requireActual . bind ( this , from ) ,
1881
1953
requireMock : this . requireMock . bind ( this , from ) ,
1882
1954
resetAllMocks,
@@ -1913,7 +1985,6 @@ export default class Runtime {
1913
1985
setTimeout,
1914
1986
spyOn,
1915
1987
unmock,
1916
- unstable_mockModule : mockModule ,
1917
1988
useFakeTimers,
1918
1989
useRealTimers,
1919
1990
} ;
0 commit comments