@@ -34,50 +34,64 @@ export default createRule({
3434 const { property } = callee ;
3535
3636 if (
37- node . arguments . length >= 1 &&
38- isTypeOfJestFnCall ( node , context , [ 'jest' ] ) &&
39- isSupportedAccessor ( property ) &&
40- [ 'mock' , 'doMock' ] . includes ( getAccessorValue ( property ) )
37+ ! node . arguments . length ||
38+ ! isTypeOfJestFnCall ( node , context , [ 'jest' ] ) ||
39+ ! (
40+ isSupportedAccessor ( property ) &&
41+ [ 'mock' , 'doMock' ] . includes ( getAccessorValue ( property ) )
42+ )
4143 ) {
42- const [ nameNode ] = node . arguments ;
43- const moduleName = findModuleName ( nameNode ) ;
44+ return ;
45+ }
4446
45- try {
46- if ( moduleName ) {
47- if ( moduleName . value . startsWith ( '.' ) ) {
48- const resolvedModulePath = path . resolve (
49- path . dirname ( context . filename ) ,
50- moduleName . value ,
51- ) ;
47+ const [ nameNode ] = node . arguments ;
48+ const moduleName = findModuleName ( nameNode ) ;
49+
50+ /* istanbul ignore if */
51+ if ( ! moduleName ) {
52+ throw new Error (
53+ 'Cannot parse mocked module name from `jest.mock` - - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`' ,
54+ ) ;
55+ }
5256
53- const hasPossiblyModulePaths = [ '' , '.js' , '.ts' ]
54- . map ( ext => `${ resolvedModulePath } ${ ext } ` )
55- . some ( modPath => {
56- try {
57- statSync ( modPath ) ;
57+ try {
58+ if ( moduleName . value . startsWith ( '.' ) ) {
59+ const resolvedModulePath = path . resolve (
60+ path . dirname ( context . filename ) ,
61+ moduleName . value ,
62+ ) ;
5863
59- return true ;
60- } catch {
61- return false ;
62- }
63- } ) ;
64+ const hasPossiblyModulePaths = [ '' , '.js' , '.ts' ]
65+ . map ( ext => ` ${ resolvedModulePath } ${ ext } ` )
66+ . some ( modPath => {
67+ try {
68+ statSync ( modPath ) ;
6469
65- if ( ! hasPossiblyModulePaths ) {
66- throw { code : 'MODULE_NOT_FOUND' } ;
70+ return true ;
71+ } catch {
72+ return false ;
6773 }
68- } else {
69- require . resolve ( moduleName . value ) ;
70- }
71- }
72- } catch ( err : any ) {
73- if ( err ?. code === 'MODULE_NOT_FOUND' || err ?. code === 'ENOENT' ) {
74- context . report ( {
75- messageId : 'invalidMockModulePath' ,
76- data : { moduleName : moduleName ?. raw ?? './module-name' } ,
77- node,
7874 } ) ;
75+
76+ if ( ! hasPossiblyModulePaths ) {
77+ throw { code : 'MODULE_NOT_FOUND' } ;
7978 }
79+ } else {
80+ require . resolve ( moduleName . value ) ;
8081 }
82+ } catch ( err : any ) {
83+ // Skip over any unexpected issues when attempt to verify mocked module path.
84+ // The list of possible errors is non-exhaustive.
85+ /* istanbul ignore if */
86+ if ( ! [ 'MODULE_NOT_FOUND' , 'ENOENT' ] . includes ( err . code ) ) {
87+ return ;
88+ }
89+
90+ context . report ( {
91+ messageId : 'invalidMockModulePath' ,
92+ data : { moduleName : moduleName . raw } ,
93+ node,
94+ } ) ;
8195 }
8296 } ,
8397 } ;
0 commit comments