Skip to content

Commit 8330763

Browse files
authored
feat: add support for custom vitest imports (#793)
* feat: add support for custom vitest imports * style: format code
1 parent ad8174b commit 8330763

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ export default [
105105
]
106106
```
107107

108+
### Custom Fixtures
109+
110+
If you're using custom fixtures in a separate file and importing them in your tests, you can let the plugin know about them by adding them to the `vitestImports` setting. The property accepts an array of strings or regular expressions that match the module names where your custom fixtures are defined.
111+
112+
```js
113+
import vitest from '@vitest/eslint-plugin'
114+
115+
export default [
116+
{
117+
files: ['tests/**'], // or any other pattern
118+
plugins: {
119+
vitest,
120+
},
121+
rules: {
122+
...vitest.configs.recommended.rules,
123+
},
124+
settings: {
125+
vitest: {
126+
vitestImports: ['@/tests/fixtures', /test-extend$/],
127+
},
128+
},
129+
},
130+
]
131+
```
132+
108133
### Shareable configurations
109134

110135
#### Recommended

src/utils/parse-vitest-fn-call.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,18 @@ const resolveVitestFn = (
348348
}
349349

350350
if (maybeImport) {
351-
if (maybeImport.source === 'vitest') {
351+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
352+
// @ts-expect-error
353+
const vitestImports = context.settings.vitest?.vitestImports ?? []
354+
const isVitestImport =
355+
maybeImport.source === 'vitest' ||
356+
vitestImports.some((importName: unknown) =>
357+
importName instanceof RegExp
358+
? importName.test(maybeImport.source)
359+
: maybeImport.source === importName,
360+
)
361+
362+
if (isVitestImport) {
352363
return {
353364
original: maybeImport.imported,
354365
local: maybeImport.local,

tests/valid-title.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,63 @@ ruleTester.run(RULE_NAME, rule, {
193193
},
194194
],
195195
},
196+
{
197+
code: `import { test } from './test-extend'
198+
test('the correct way to properly handle all things', () => {})
199+
`,
200+
settings: {
201+
vitest: {
202+
vitestImports: ['./test-extend'],
203+
},
204+
},
205+
options: [{ disallowedWords: ['correct'] }],
206+
errors: [
207+
{
208+
messageId: 'disallowedWord',
209+
data: { word: 'correct' },
210+
column: 12,
211+
line: 2,
212+
},
213+
],
214+
},
215+
{
216+
code: `import { test } from '@/tests/fixtures'
217+
test('the correct way to properly handle all things', () => {})
218+
`,
219+
settings: {
220+
vitest: {
221+
vitestImports: ['@/tests/fixtures'],
222+
},
223+
},
224+
options: [{ disallowedWords: ['correct'] }],
225+
errors: [
226+
{
227+
messageId: 'disallowedWord',
228+
data: { word: 'correct' },
229+
column: 12,
230+
line: 2,
231+
},
232+
],
233+
},
234+
{
235+
code: `import { test } from '@/tests/fixtures'
236+
test('the correct way to properly handle all things', () => {})
237+
`,
238+
settings: {
239+
vitest: {
240+
vitestImports: [/\/fixtures$/],
241+
},
242+
},
243+
options: [{ disallowedWords: ['correct'] }],
244+
errors: [
245+
{
246+
messageId: 'disallowedWord',
247+
data: { word: 'correct' },
248+
column: 12,
249+
line: 2,
250+
},
251+
],
252+
},
196253
],
197254
})
198255

0 commit comments

Comments
 (0)