@@ -7,12 +7,18 @@ use oxc_span::Span;
77
88use crate :: { context:: LintContext , rule:: Rule } ;
99
10- fn no_mocks_import_diagnostic ( span : Span ) -> OxcDiagnostic {
10+ fn no_mocks_jest_import_diagnostic ( span : Span ) -> OxcDiagnostic {
1111 OxcDiagnostic :: warn ( "Mocks should not be manually imported from a `__mocks__` directory." )
1212 . with_help ( "Instead use `jest.mock` and import from the original module path." )
1313 . with_label ( span)
1414}
1515
16+ fn no_mocks_vitest_import_diagnostic ( span : Span ) -> OxcDiagnostic {
17+ OxcDiagnostic :: warn ( "Mocks should not be manually imported from a `__mocks__` directory." )
18+ . with_help ( "Instead use `vi.mock` and import from the original module path." )
19+ . with_label ( span)
20+ }
21+
1622// <https://github.com/jest-community/eslint-plugin-jest/blob/v28.9.0/docs/rules/no-mocks-import.md>
1723#[ derive( Debug , Default , Clone ) ]
1824pub struct NoMocksImport ;
@@ -66,7 +72,17 @@ impl Rule for NoMocksImport {
6672 for import_entry in & module_records. import_entries {
6773 let module_specifier = import_entry. module_request . name ( ) ;
6874 if contains_mocks_dir ( module_specifier) {
69- ctx. diagnostic ( no_mocks_import_diagnostic ( import_entry. module_request . span ) ) ;
75+ if ctx. frameworks ( ) . is_vitest ( ) {
76+ ctx. diagnostic ( no_mocks_vitest_import_diagnostic (
77+ import_entry. module_request . span ,
78+ ) ) ;
79+ }
80+
81+ if ctx. frameworks ( ) . is_jest ( ) {
82+ ctx. diagnostic ( no_mocks_jest_import_diagnostic (
83+ import_entry. module_request . span ,
84+ ) ) ;
85+ }
7086 }
7187 }
7288
@@ -87,7 +103,13 @@ impl Rule for NoMocksImport {
87103 } ;
88104
89105 if contains_mocks_dir ( & string_literal. value ) {
90- ctx. diagnostic ( no_mocks_import_diagnostic ( string_literal. span ) ) ;
106+ if ctx. frameworks ( ) . is_vitest ( ) {
107+ ctx. diagnostic ( no_mocks_vitest_import_diagnostic ( string_literal. span ) ) ;
108+ }
109+
110+ if ctx. frameworks ( ) . is_jest ( ) {
111+ ctx. diagnostic ( no_mocks_jest_import_diagnostic ( string_literal. span ) ) ;
112+ }
91113 }
92114 }
93115 }
@@ -104,7 +126,7 @@ fn contains_mocks_dir(value: &str) -> bool {
104126fn test ( ) {
105127 use crate :: tester:: Tester ;
106128
107- let pass = vec ! [
129+ let mut pass = vec ! [
108130 ( "import something from 'something'" , None ) ,
109131 ( "require('somethingElse')" , None ) ,
110132 ( "require('./__mocks__.js')" , None ) ,
@@ -117,7 +139,7 @@ fn test() {
117139 ( "entirelyDifferent(fn)" , None ) ,
118140 ] ;
119141
120- let fail = vec ! [
142+ let mut fail = vec ! [
121143 ( "require('./__mocks__')" , None ) ,
122144 ( "require('./__mocks__/')" , None ) ,
123145 ( "require('./__mocks__/index')" , None ) ,
@@ -127,6 +149,32 @@ fn test() {
127149 ( "import thing from './__mocks__/index'" , None ) ,
128150 ] ;
129151
152+ let pass_vitest = vec ! [
153+ ( "import something from 'something'" , None ) ,
154+ ( "require('somethingElse')" , None ) ,
155+ ( "require('./__mocks__.js')" , None ) ,
156+ ( "require('./__mocks__x')" , None ) ,
157+ ( "require('./__mocks__x/x')" , None ) ,
158+ ( "require('./x__mocks__')" , None ) ,
159+ ( "require('./x__mocks__/x')" , None ) ,
160+ ( "require()" , None ) ,
161+ ( "var path = './__mocks__.js'; require(path)" , None ) ,
162+ ( "entirelyDifferent(fn)" , None ) ,
163+ ] ;
164+
165+ let fail_vitest = vec ! [
166+ ( "require('./__mocks__')" , None ) ,
167+ ( "require('./__mocks__/')" , None ) ,
168+ ( "require('./__mocks__/index')" , None ) ,
169+ ( "require('__mocks__')" , None ) ,
170+ ( "require('__mocks__/')" , None ) ,
171+ ( "require('__mocks__/index')" , None ) ,
172+ ( "import thing from './__mocks__/index'" , None ) ,
173+ ] ;
174+
175+ pass. extend ( pass_vitest) ;
176+ fail. extend ( fail_vitest) ;
177+
130178 Tester :: new ( NoMocksImport :: NAME , NoMocksImport :: PLUGIN , pass, fail)
131179 . with_jest_plugin ( true )
132180 . with_vitest_plugin ( true )
0 commit comments