ESLint plugin for Vitest
npm install @vitest/eslint-plugin!npm

ESLint plugin for Vitest
You'll first need to install ESLint:
``sh`
npm i eslint --save-dev
Next, install @vitest/eslint-plugin
`sh`
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 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
Add vitest to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:
`json`
{
"plugins": ["@vitest"]
}
Then configure the rules you want to use under the rules section.
`json`
{
"rules": {
"@vitest/max-nested-describe": [
"error",
{
"max": 3
}
]
}
}
If you're using old ESLint configuration, make sure to use legacy key like the following
`js`
{
"extends": ["plugin:@vitest/legacy-recommended"] // or legacy-all
}
#### Enabling with Type-Testing
Vitest ships with an optional type-testing feature, which is disabled by default.
If you're using this feature, you should also enabled typecheck in the settings for this plugin. This ensures that rules like expect-expect 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 defineConfig(
// see https://typescript-eslint.io
tseslint.configs.recommended,
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
files: ['tests/**'], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules,
},
settings: {
vitest: {
typecheck: true,
},
},
languageOptions: {
globals: {
...vitest.environments.env.globals,
},
},
},
)
`
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 defineConfig({
files: ['tests/**'], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules,
},
settings: {
vitest: {
vitestImports: ['@/tests/fixtures', /test-extend$/],
},
},
})
`
#### Recommended
This plugin exports a recommended configuration that enforces good testing practices.
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 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 defineConfig({
files: ['tests/**'], // or any other pattern
...vitest.configs.all,
})
`
πΌ Configurations enabled in.\
β οΈ Configurations set to warn in.\
π« Configurations disabled in.\
π Set in the all configuration.\recommended
β
Set in the configuration.\--fix
π§ Automatically fixable by the CLI option.\
π‘ Manually fixable by editor suggestions.\
π Requires type information.\
β Deprecated.
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π« | π§ | π‘ | π | β |
| :----------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------- | :-- | :---- | :-- | :-- | :-- | :-- | :-- |
| consistent-each-for | enforce using .each or .for consistently | | π | | | | | |node:test
| consistent-test-filename | require test file pattern | | π | | | | | |
| consistent-test-it | enforce using test or it but not both | | π | | π§ | | | |
| consistent-vitest-vi | enforce using vitest or vi but not both | | π | | π§ | | | |
| expect-expect | enforce having expectation in test body | β
| π | | | | | |
| hoisted-apis-on-top | enforce hoisted APIs to be on top of the file | | π | | | π‘ | | |
| max-expects | enforce a maximum number of expect per test | | π | | | | | |
| max-nested-describe | require describe block to be less than set max value or default value | | π | | | | | |
| no-alias-methods | disallow alias methods | | π | | π§ | | | |
| no-commented-out-tests | disallow commented out tests | β
| π | | | | | |
| no-conditional-expect | disallow conditional expects | β
| π | | | | | |
| no-conditional-in-test | disallow conditional tests | | π | | | | | |
| no-conditional-tests | disallow conditional tests | | π | | | | | |
| no-disabled-tests | disallow disabled tests | | π β
| | | | | |
| no-done-callback | disallow using a callback in asynchronous tests and hooks | | | | | π‘ | | β |
| no-duplicate-hooks | disallow duplicate hooks and teardown hooks | | π | | | | | |
| no-focused-tests | disallow focused tests | β
| π | | π§ | | | |
| no-hooks | disallow setup and teardown hooks | | π | | | | | |
| no-identical-title | disallow identical titles | β
| π | | π§ | | | |
| no-import-node-test | disallow importing | β
| π | | π§ | | | |vi.
| no-importing-vitest-globals | disallow importing Vitest globals | | | π | π§ | | | |
| no-interpolation-in-snapshots | disallow string interpolation in snapshots | β
| π | | π§ | | | |
| no-large-snapshots | disallow large snapshots | | π | | | | | |
| no-mocks-import | disallow importing from mocks directory | β
| π | | | | | |
| no-restricted-matchers | disallow the use of certain matchers | | π | | | | | |
| no-restricted-vi-methods | disallow specific methods | | π | | | | | |expect
| no-standalone-expect | disallow using outside of it or test blocks | β
| π | | | | | |f
| no-test-prefixes | disallow using the and x prefixes in favour of .only and .skip | | π | | π§ | | | |afterAll
| no-test-return-statement | disallow return statements in tests | | π | | | | | |
| no-unneeded-async-expect-function | Disallow unnecessary async function wrapper for expected promises | β
| π | | π§ | | | |
| padding-around-after-all-blocks | Enforce padding around blocks | | π | | π§ | | | |afterEach
| padding-around-after-each-blocks | Enforce padding around blocks | | π | | π§ | | | |beforeAll
| padding-around-all | Enforce padding around vitest functions | | π | | π§ | | | |
| padding-around-before-all-blocks | Enforce padding around blocks | | π | | π§ | | | |beforeEach
| padding-around-before-each-blocks | Enforce padding around blocks | | π | | π§ | | | |describe
| padding-around-describe-blocks | Enforce padding around blocks | | π | | π§ | | | |expect
| padding-around-expect-groups | Enforce padding around groups | | π | | π§ | | | |test
| padding-around-test-blocks | Enforce padding around blocks | | π | | π§ | | | |toHaveBeenCalledExactlyOnceWith
| prefer-called-exactly-once-with | Prefer over toHaveBeenCalledOnce and toHaveBeenCalledWith | β
| π | | π§ | | | |toBeCalledOnce()
| prefer-called-once | enforce using or toHaveBeenCalledOnce() | | | π | π§ | | | |toBeCalledTimes(1)
| prefer-called-times | enforce using or toHaveBeenCalledTimes(1) | | π | | π§ | | | |toBeCalledWith()
| prefer-called-with | enforce using or toHaveBeenCalledWith() | | π | | π§ | | | |each
| prefer-comparison-matcher | enforce using the built-in comparison matchers | | π | | π§ | | | |
| prefer-describe-function-title | enforce using a function as a describe title over an equivalent string | | π | | π§ | | | |
| prefer-each | enforce using rather than manual loops | | π | | | | | |expect().resolves
| prefer-equality-matcher | enforce using the built-in equality matchers | | π | | | π‘ | | |
| prefer-expect-assertions | enforce using expect assertions instead of callbacks | | π | | | π‘ | | |
| prefer-expect-resolves | enforce using over expect(await ...) syntax | | π | | π§ | | | |expectTypeOf
| prefer-expect-type-of | enforce using instead of expect(typeof ...) | | π | | π§ | | | |vi.spyOn
| prefer-hooks-in-order | enforce having hooks in consistent order | | π | | | | | |
| prefer-hooks-on-top | enforce having hooks before any test cases | | π | | | | | |
| prefer-import-in-mock | prefer dynamic import in mock | | π | | π§ | | | |
| prefer-importing-vitest-globals | enforce importing Vitest globals | | π | | π§ | | | |
| prefer-lowercase-title | enforce lowercase titles | | π | | π§ | | | |
| prefer-mock-promise-shorthand | enforce mock resolved/rejected shorthands for promises | | π | | π§ | | | |
| prefer-mock-return-shorthand | Prefer mock return shorthands | | | | π§ | | | |
| prefer-snapshot-hint | enforce including a hint with external snapshots | | π | | | | | |
| prefer-spy-on | enforce using | | π | | π§ | | | |toBe(true)
| prefer-strict-boolean-matchers | enforce using and toBe(false) over matchers that coerce types to boolean | | π | | π§ | | | |toBeTruthy
| prefer-strict-equal | enforce strict equal over equal | | π | | | π‘ | | |
| prefer-to-be | enforce using toBe() | | π | | π§ | | | |
| prefer-to-be-falsy | enforce using toBeFalsy() | | | π | π§ | | | |
| prefer-to-be-object | enforce using toBeObject() | | π | | π§ | | | |
| prefer-to-be-truthy | enforce using | | | π | π§ | | | |toHaveBeenCalledTimes()
| prefer-to-contain | enforce using toContain() | | π | | π§ | | | |
| prefer-to-have-been-called-times | Suggest using | | π | | π§ | | | |test.todo
| prefer-to-have-length | enforce using toHaveLength() | | π | | π§ | | | |
| prefer-todo | enforce using | | π | | π§ | | | |vi.mocked()
| prefer-vi-mocked | require over fn as Mock | | π | | π§ | | π | |expect.poll
| require-awaited-expect-poll | ensure that every call is awaited | | π | | | | | |expect()
| require-hook | require setup and teardown to be within a hook | | π | | | | | |
| require-local-test-context-for-concurrent-snapshots | require local Test Context for concurrent snapshot tests | β
| π | | | | | |
| require-mock-type-parameters | enforce using type parameters with vitest mock functions | | π | | π§ | | | |
| require-test-timeout | require tests to declare a timeout | | | π | | | | |
| require-to-throw-message | require toThrow() to be called with an error message | | π | | | | | |
| require-top-level-describe | enforce that all tests are in a top-level describe | | π | | | | | |
| valid-describe-callback | enforce valid describe callback | β
| π | | | | | |
| valid-expect | enforce valid usage | β
| π | | π§ | | | |.todo` usage | | | | | | | |
| valid-expect-in-promise | require promises that have expectations in their chain to be valid | β
| π | | | | | |
| valid-title | enforce valid titles | β
| π | | π§ | | | |
| warn-todo | disallow
#### Credits
- eslint-plugin-jest:
most of the rules in this plugin are essentially ports of Jest plugin rules with minor modifications