Eslint plugin for vitest
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-vitest
:
npm install eslint-plugin-vitest --save-dev
Add vitest
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": ["vitest"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"vitest/max-nested-describe": [
"error",
{
"max": 3
}
]
}
}
To use the recommended configuration, extend it in your .eslintrc
file:
{
"extends": ["plugin:vitest/recommended"]
}
All recommend rules will be set to error by default. You can however disable some rules by setting turning them off
in your .eslintrc
file or by setting them to warn
in your .eslintrc
.
To use the all configuration, extend it in your .eslintrc
file:
{
"extends": ["plugin:vitest/all"]
}
This plugin assumes that you're running it on tests files only by default which sometimes is not the case. If you can to run it on test files only. Your configuration will look like this:
If you're using .eslintrc
{
"extends": ["eslint:recommended"],
"overrides": [
{
"files": ["tests/**"], // or any other pattern
"plugins": ["vitest"],
"extends": ["plugin:vitest/recommended"]
}
]
}
If you're using .eslintrc.js
import vitest from "eslint-plugin-vitest";
export default [
{
files: ["tests/**"], // or any other pattern
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules,
},
languageOptions: {
globals: {
...vitest.environments.env.globals,
},
},
},
];
💼 Configurations enabled in.
🌐 Set in the all
configuration.
✅ Set in the recommended
configuration.
🔧 Automatically fixable by the --fix
CLI option.
💡 Manually fixable by editor suggestions.
Name | Description | 💼 | 🔧 | 💡 | |
---|---|---|---|---|---|
consistent-test-filename | forbidden .spec test file pattern | 🌐 | |||
consistent-test-it | Prefer test or it but not both | 🌐 | 🔧 | ||
expect-expect | Enforce having expectation in test body | ✅ | |||
max-expects | Enforce a maximum number of expect per test | 🌐 | |||
max-nested-describe | Nested describe block should 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 node:test |
✅ | 🌐 | 🔧 | |
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 vi. methods |
🌐 | |||
no-standalone-expect | Disallow using expect outside of it or test blocks |
🌐 | |||
no-test-prefixes | Disallow using test as a prefix |
🌐 | 🔧 | ||
no-test-return-statement | Disallow return statements in tests | 🌐 | |||
prefer-called-with | Suggest using toBeCalledWith() or toHaveBeenCalledWith() |
🌐 | 🔧 | ||
prefer-comparison-matcher | Suggest using the built-in comparison matchers | 🌐 | 🔧 | ||
prefer-each | Prefer each rather than manual loops |
🌐 | |||
prefer-equality-matcher | Suggest using the built-in quality matchers | 🌐 | 💡 | ||
prefer-expect-resolves | Suggest using expect().resolves over expect(await ...) syntax |
🌐 | 🔧 | ||
prefer-hooks-in-order | Prefer having hooks in consistent order | 🌐 | |||
prefer-hooks-on-top | Suggest having hooks before any test cases | 🌐 | |||
prefer-lowercase-title | Enforce lowercase titles | 🌐 | 🔧 | ||
prefer-mock-promise-shorthand | Prefer mock resolved/rejected shorthands for promises | 🌐 | 🔧 | ||
prefer-snapshot-hint | Prefer including a hint with external snapshots | 🌐 | |||
prefer-spy-on | Suggest using vi.spyOn |
🌐 | 🔧 | ||
prefer-strict-equal | Prefer strict equal over equal | 🌐 | 💡 | ||
prefer-to-be | Suggest using toBe() | ✅ | 🔧 | ||
prefer-to-be-falsy | Suggest using toBeFalsy() | 🌐 | 🔧 | ||
prefer-to-be-object | Prefer toBeObject() | 🌐 | 🔧 | ||
prefer-to-be-truthy | Suggest using toBeTruthy |
🌐 | 🔧 | ||
prefer-to-contain | Prefer using toContain() | 🌐 | 🔧 | ||
prefer-to-have-length | Suggest using toHaveLength() | 🌐 | 🔧 | ||
prefer-todo | Suggest using test.todo |
🌐 | 🔧 | ||
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-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 expect() usage |
✅ | |||
valid-title | Enforce valid titles | ✅ | 🔧 |
- eslint-plugin-jest Most of the rules in this plugin are essentially ports of Jest plugin rules with minor modifications