Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 46 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ npm install @vitest/eslint-plugin --save-dev
Make sure you're running ESLint `v9.0.0` or higher for the latest version of this plugin to work. The following example is how your `eslint.config.js` should be setup for this plugin to work for you.

```js
import { defineConfig } from 'eslint/config'
import vitest from '@vitest/eslint-plugin'

export default [
{
files: ['tests/**'], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
'vitest/max-nested-describe': ['error', { max: 3 }], // you can also modify rules' behavior using option like this
},
export default defineConfig({
files: ['tests/**'], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
'vitest/max-nested-describe': ['error', { max: 3 }], // you can also modify rules' behavior using option like this
},
]
})
```

If you're not using the latest version of ESLint (version `v8.57.0` or lower) you can setup this plugin using the following configuration
Expand Down Expand Up @@ -80,9 +79,20 @@ Vitest ships with an optional [type-testing feature](https://vitest.dev/guide/te
If you're using this feature, you should also enabled `typecheck` in the settings for this plugin. This ensures that rules like [expect-expect](docs/rules/expect-expect.md) account for type-related assertions in tests.

```js
import { defineConfig } from 'eslint/config'
import tseslint from 'typescript-eslint'
import vitest from '@vitest/eslint-plugin'

export default [
export default defineConfig(
// see https://typescript-eslint.io
tseslint.configs.recommended,
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
files: ['tests/**'], // or any other pattern
plugins: {
Expand All @@ -102,32 +112,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 { defineConfig } from 'eslint/config'
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$/],
},
export default defineConfig({
files: ['tests/**'], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules,
},
settings: {
vitest: {
vitestImports: ['@/tests/fixtures', /test-extend$/],
},
},
]
})
```

### Shareable Configurations
Expand All @@ -139,29 +148,27 @@ This plugin exports a recommended configuration that enforces good testing pract
To enable this configuration with `eslint.config.js`, use `vitest.configs.recommended`:

```js
import { defineConfig } from 'eslint/config'
import vitest from '@vitest/eslint-plugin'

export default [
{
files: ['tests/**'], // or any other pattern
...vitest.configs.recommended,
},
]
export default defineConfig({
files: ['tests/**'], // or any other pattern
...vitest.configs.recommended,
})
```

#### All

If you want to enable all rules instead of only some you can do so by adding the all configuration to your `eslint.config.js` config file:

```js
import { defineConfig } from 'eslint/config'
import vitest from '@vitest/eslint-plugin'

export default [
{
files: ['tests/**'], // or any other pattern
...vitest.configs.all,
},
]
export default defineConfig({
files: ['tests/**'], // or any other pattern
...vitest.configs.all,
})
```

### Rules
Expand Down
1 change: 0 additions & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default defineConfig(
gitignore(),
eslint.configs.recommended,
tseslint.configs.recommended,
// @ts-expect-error see https://github.com/vitest-dev/eslint-plugin-vitest/issues/771
vitest.configs.recommended,
eslintPlugin.configs.recommended,
{
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Linter } from 'eslint'
import type { ESLint, Linter, Rule } from 'eslint'
import { version } from '../package.json'
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
Expand Down Expand Up @@ -262,7 +262,7 @@ const rules = {
[warnTodoName]: warnTodo,
[preferImportInMockName]: preferImportInMock,
[preferCalledExactlyOnceWithName]: preferCalledExactlyOnceWith
} as const
} as unknown as Record<string, Rule.RuleModule>

const plugin = {
meta: {
Expand Down Expand Up @@ -299,7 +299,7 @@ const plugin = {
recommended: {
name: 'vitest/recommended',
plugins: {
get vitest() {
get vitest(): ESLint.Plugin {
return plugin
},
},
Expand All @@ -308,7 +308,7 @@ const plugin = {
all: {
name: 'vitest/all',
plugins: {
get vitest() {
get vitest(): ESLint.Plugin {
return plugin
},
},
Expand Down Expand Up @@ -337,8 +337,8 @@ const plugin = {
onTestFinished: 'writable',
},
},
} as const satisfies Linter.Config,
} as const,
} as const
},
},
} as const satisfies ESLint.Plugin

export default plugin
Loading