diff --git a/README.md b/README.md index a91ab4e0..2e0975dc 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,31 @@ export default [ ] ``` +### Custom Fixtures + +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. + +```js +import vitest from '@vitest/eslint-plugin' + +export default [ + { + files: ['tests/**'], // or any other pattern + plugins: { + vitest, + }, + rules: { + ...vitest.configs.recommended.rules, + }, + settings: { + vitest: { + vitestImports: ['@/tests/fixtures', /test-extend$/], + }, + }, + }, +] +``` + ### Shareable configurations #### Recommended diff --git a/src/utils/parse-vitest-fn-call.ts b/src/utils/parse-vitest-fn-call.ts index f6c7d7c0..54032619 100644 --- a/src/utils/parse-vitest-fn-call.ts +++ b/src/utils/parse-vitest-fn-call.ts @@ -348,7 +348,18 @@ const resolveVitestFn = ( } if (maybeImport) { - if (maybeImport.source === 'vitest') { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + const vitestImports = context.settings.vitest?.vitestImports ?? [] + const isVitestImport = + maybeImport.source === 'vitest' || + vitestImports.some((importName: unknown) => + importName instanceof RegExp + ? importName.test(maybeImport.source) + : maybeImport.source === importName, + ) + + if (isVitestImport) { return { original: maybeImport.imported, local: maybeImport.local, diff --git a/tests/valid-title.test.ts b/tests/valid-title.test.ts index 9636d730..64db3d9c 100644 --- a/tests/valid-title.test.ts +++ b/tests/valid-title.test.ts @@ -193,6 +193,63 @@ ruleTester.run(RULE_NAME, rule, { }, ], }, + { + code: `import { test } from './test-extend' + test('the correct way to properly handle all things', () => {}) + `, + settings: { + vitest: { + vitestImports: ['./test-extend'], + }, + }, + options: [{ disallowedWords: ['correct'] }], + errors: [ + { + messageId: 'disallowedWord', + data: { word: 'correct' }, + column: 12, + line: 2, + }, + ], + }, + { + code: `import { test } from '@/tests/fixtures' + test('the correct way to properly handle all things', () => {}) + `, + settings: { + vitest: { + vitestImports: ['@/tests/fixtures'], + }, + }, + options: [{ disallowedWords: ['correct'] }], + errors: [ + { + messageId: 'disallowedWord', + data: { word: 'correct' }, + column: 12, + line: 2, + }, + ], + }, + { + code: `import { test } from '@/tests/fixtures' + test('the correct way to properly handle all things', () => {}) + `, + settings: { + vitest: { + vitestImports: [/\/fixtures$/], + }, + }, + options: [{ disallowedWords: ['correct'] }], + errors: [ + { + messageId: 'disallowedWord', + data: { word: 'correct' }, + column: 12, + line: 2, + }, + ], + }, ], })