Config for projects using Vitest for testing.
-
If you haven't already, make sure to install
@code-pushup/eslint-config
and its required peer dependencies. -
Since this plugin requires additional peer dependencies, you have to install them as well:
npm install -D eslint-plugin-{jest-formatting,vitest}
-
Add to your
eslint.config.js
file:import vitest from '@code-pushup/eslint-config/vitest.js'; import tseslint from 'typescript-eslint'; export default tseslint.config( ...vitest, { // customize rules if needed: rules: { // e.g. to customize file naming convention (default pattern is '.*\\.spec\\.[tj]sx?$'): 'vitest/consistent-test-filename': [ 'warn', { 'pattern': '.*\\.(unit|integration|e2e)\\.test\\.ts$' } ], // e.g. to customize `test` or `it` usage (default is `it` in `describe` and `test` at top-level): 'vitest/consistent-test-it': ['warn', { fn: 'test', withinDescribe: 'test' }] } } );
🔧 Automatically fixable by the
--fix
CLI option.
💡 Manually fixable by editor suggestions.
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
expect-expect enforce having expectation in test body |
||||
no-commented-out-tests disallow commented out tests |
||||
no-identical-title disallow identical titles |
🔧 | |||
no-import-node-test disallow importing node:test |
🔧 | |||
require-local-test-context-for-concurrent-snapshots require local Test Context for concurrent snapshot tests |
||||
valid-describe-callback enforce valid describe callback |
||||
valid-expect enforce valid expect() usage |
||||
valid-title enforce valid titles |
🔧 |
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
padding-around-after-all-blocks |
🔧 | |||
padding-around-after-each-blocks |
🔧 | |||
padding-around-before-all-blocks |
🔧 | |||
padding-around-before-each-blocks |
🔧 | |||
padding-around-describe-blocks |
🔧 | |||
padding-around-test-blocks |
🔧 | |||
consistent-test-filename require .spec test file pattern |
pattern: .*\.spec\.[tj]sx?${
"pattern": ".*\\.spec\\.[tj]sx?$"
} |
|||
consistent-test-it enforce using test or it but not both |
🔧 | |||
max-nested-describe require describe block to be less than set max value or default value |
max: 2{
"max": 2
} |
|||
no-alias-methods disallow alias methods |
🔧 | |||
no-conditional-expect disallow conditional expects |
||||
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-mocks-import disallow importing from mocks directory |
||||
no-standalone-expect disallow using expect outside of it or test blocks |
||||
no-test-return-statement disallow return statements in tests |
||||
prefer-comparison-matcher enforce using the built-in comparison matchers |
🔧 | |||
prefer-each enforce using each rather than manual loops |
||||
prefer-equality-matcher enforce using the built-in quality matchers |
💡 | |||
prefer-expect-resolves enforce using expect().resolves over expect(await ...) syntax |
🔧 | |||
prefer-hooks-on-top enforce having hooks before any test cases |
||||
prefer-mock-promise-shorthand enforce mock resolved/rejected shorthands for promises |
🔧 | |||
prefer-spy-on enforce using vi.spyOn |
🔧 | |||
prefer-to-be enforce using toBe() |
🔧 | |||
prefer-to-contain enforce using toContain() |
🔧 | |||
prefer-to-have-length enforce using toHaveLength() |
🔧 | |||
prefer-todo enforce using test.todo |
🔧 | |||
require-hook require setup and teardown to be within a hook |
||||
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 |