Skip to content

Commit 3005315

Browse files
authored
fix: workaround for plugin type (#811)
1 parent 977022f commit 3005315

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

README.md

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ npm install @vitest/eslint-plugin --save-dev
2424
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.
2525

2626
```js
27+
import { defineConfig } from 'eslint/config'
2728
import vitest from '@vitest/eslint-plugin'
2829

29-
export default [
30-
{
31-
files: ['tests/**'], // or any other pattern
32-
plugins: {
33-
vitest,
34-
},
35-
rules: {
36-
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
37-
'vitest/max-nested-describe': ['error', { max: 3 }], // you can also modify rules' behavior using option like this
38-
},
30+
export default defineConfig({
31+
files: ['tests/**'], // or any other pattern
32+
plugins: {
33+
vitest,
34+
},
35+
rules: {
36+
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
37+
'vitest/max-nested-describe': ['error', { max: 3 }], // you can also modify rules' behavior using option like this
3938
},
40-
]
39+
})
4140
```
4241

4342
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
@@ -80,9 +79,20 @@ Vitest ships with an optional [type-testing feature](https://vitest.dev/guide/te
8079
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.
8180

8281
```js
82+
import { defineConfig } from 'eslint/config'
83+
import tseslint from 'typescript-eslint'
8384
import vitest from '@vitest/eslint-plugin'
8485

85-
export default [
86+
export default defineConfig(
87+
// see https://typescript-eslint.io
88+
tseslint.configs.recommended,
89+
{
90+
languageOptions: {
91+
parserOptions: {
92+
projectService: true,
93+
},
94+
},
95+
},
8696
{
8797
files: ['tests/**'], // or any other pattern
8898
plugins: {
@@ -102,32 +112,31 @@ export default [
102112
},
103113
},
104114
},
105-
]
115+
)
106116
```
107117

108118
### Custom Fixtures
109119

110120
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.
111121

112122
```js
123+
import { defineConfig } from 'eslint/config'
113124
import vitest from '@vitest/eslint-plugin'
114125

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-
},
126+
export default defineConfig({
127+
files: ['tests/**'], // or any other pattern
128+
plugins: {
129+
vitest,
130+
},
131+
rules: {
132+
...vitest.configs.recommended.rules,
133+
},
134+
settings: {
135+
vitest: {
136+
vitestImports: ['@/tests/fixtures', /test-extend$/],
128137
},
129138
},
130-
]
139+
})
131140
```
132141

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

141150
```js
151+
import { defineConfig } from 'eslint/config'
142152
import vitest from '@vitest/eslint-plugin'
143153

144-
export default [
145-
{
146-
files: ['tests/**'], // or any other pattern
147-
...vitest.configs.recommended,
148-
},
149-
]
154+
export default defineConfig({
155+
files: ['tests/**'], // or any other pattern
156+
...vitest.configs.recommended,
157+
})
150158
```
151159

152160
#### All
153161

154162
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:
155163

156164
```js
165+
import { defineConfig } from 'eslint/config'
157166
import vitest from '@vitest/eslint-plugin'
158167

159-
export default [
160-
{
161-
files: ['tests/**'], // or any other pattern
162-
...vitest.configs.all,
163-
},
164-
]
168+
export default defineConfig({
169+
files: ['tests/**'], // or any other pattern
170+
...vitest.configs.all,
171+
})
165172
```
166173

167174
### Rules

eslint.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default defineConfig(
1010
gitignore(),
1111
eslint.configs.recommended,
1212
tseslint.configs.recommended,
13-
// @ts-expect-error see https://github.com/vitest-dev/eslint-plugin-vitest/issues/771
1413
vitest.configs.recommended,
1514
eslintPlugin.configs.recommended,
1615
{

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Linter } from 'eslint'
1+
import type { ESLint, Linter, Rule } from 'eslint'
22
import { version } from '../package.json'
33
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
44
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
@@ -262,7 +262,7 @@ const rules = {
262262
[warnTodoName]: warnTodo,
263263
[preferImportInMockName]: preferImportInMock,
264264
[preferCalledExactlyOnceWithName]: preferCalledExactlyOnceWith
265-
} as const
265+
} as unknown as Record<string, Rule.RuleModule>
266266

267267
const plugin = {
268268
meta: {
@@ -299,7 +299,7 @@ const plugin = {
299299
recommended: {
300300
name: 'vitest/recommended',
301301
plugins: {
302-
get vitest() {
302+
get vitest(): ESLint.Plugin {
303303
return plugin
304304
},
305305
},
@@ -308,7 +308,7 @@ const plugin = {
308308
all: {
309309
name: 'vitest/all',
310310
plugins: {
311-
get vitest() {
311+
get vitest(): ESLint.Plugin {
312312
return plugin
313313
},
314314
},
@@ -337,8 +337,8 @@ const plugin = {
337337
onTestFinished: 'writable',
338338
},
339339
},
340-
} as const satisfies Linter.Config,
341-
} as const,
342-
} as const
340+
},
341+
},
342+
} as const satisfies ESLint.Plugin
343343

344344
export default plugin

0 commit comments

Comments
 (0)