Skip to content

vitest-dev/eslint-plugin-vitest

Repository files navigation

eslint-plugin-vitest

npm ci

ESLint plugin for Vitest

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install @vitest/eslint-plugin

npm install @vitest/eslint-plugin --save-dev

Usage

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.

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:

{
  "plugins": ["@vitest"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "@vitest/max-nested-describe": [
      "error",
      {
        "max": 3
      }
    ]
  }
}

If you're using old ESLint configuration, make sure to use legacy key like the following

{
  "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.

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,
      },
    },
  },
)

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.

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$/],
    },
  },
})

Shareable Configurations

Recommended

This plugin exports a recommended configuration that enforces good testing practices.

To enable this configuration with eslint.config.js, use vitest.configs.recommended:

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:

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

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

Rules

๐Ÿ’ผ Configurations enabled in.
โš ๏ธ Configurations set to warn in.
๐Ÿšซ Configurations disabled in.
๐ŸŒ Set in the all configuration.
โœ… Set in the recommended configuration.
๐Ÿ”ง Automatically fixable by the --fix CLI option.
๐Ÿ’ก Manually fixable by editor suggestions.
๐Ÿ’ญ Requires type information.
โŒ Deprecated.

Nameย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Description ๐Ÿ’ผ โš ๏ธ ๐Ÿšซ ๐Ÿ”ง ๐Ÿ’ก ๐Ÿ’ญ โŒ
consistent-each-for enforce using .each or .for consistently ๐ŸŒ
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 node:test โœ… ๐ŸŒ ๐Ÿ”ง
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 vi. methods ๐ŸŒ
no-standalone-expect disallow using expect outside of it or test blocks โœ… ๐ŸŒ
no-test-prefixes disallow using the f and x prefixes in favour of .only and .skip ๐ŸŒ ๐Ÿ”ง
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 afterAll blocks ๐ŸŒ ๐Ÿ”ง
padding-around-after-each-blocks Enforce padding around afterEach blocks ๐ŸŒ ๐Ÿ”ง
padding-around-all Enforce padding around vitest functions ๐ŸŒ ๐Ÿ”ง
padding-around-before-all-blocks Enforce padding around beforeAll blocks ๐ŸŒ ๐Ÿ”ง
padding-around-before-each-blocks Enforce padding around beforeEach blocks ๐ŸŒ ๐Ÿ”ง
padding-around-describe-blocks Enforce padding around describe blocks ๐ŸŒ ๐Ÿ”ง
padding-around-expect-groups Enforce padding around expect groups ๐ŸŒ ๐Ÿ”ง
padding-around-test-blocks Enforce padding around test blocks ๐ŸŒ ๐Ÿ”ง
prefer-called-exactly-once-with Prefer toHaveBeenCalledExactlyOnceWith over toHaveBeenCalledOnce and toHaveBeenCalledWith โœ… ๐ŸŒ ๐Ÿ”ง
prefer-called-once enforce using toBeCalledOnce() or toHaveBeenCalledOnce() ๐ŸŒ ๐Ÿ”ง
prefer-called-times enforce using toBeCalledTimes(1) or toHaveBeenCalledTimes(1) ๐ŸŒ ๐Ÿ”ง
prefer-called-with enforce using toBeCalledWith() or toHaveBeenCalledWith() ๐ŸŒ ๐Ÿ”ง
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 each rather than manual loops ๐ŸŒ
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 expect().resolves over expect(await ...) syntax ๐ŸŒ ๐Ÿ”ง
prefer-expect-type-of enforce using expectTypeOf instead of expect(typeof ...) ๐ŸŒ ๐Ÿ”ง
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 vi.spyOn ๐ŸŒ ๐Ÿ”ง
prefer-strict-boolean-matchers enforce using toBe(true) and toBe(false) over matchers that coerce types to boolean ๐ŸŒ ๐Ÿ”ง
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 toBeTruthy ๐ŸŒ ๐Ÿ”ง
prefer-to-contain enforce using toContain() ๐ŸŒ ๐Ÿ”ง
prefer-to-have-been-called-times Suggest using toHaveBeenCalledTimes() ๐ŸŒ ๐Ÿ”ง
prefer-to-have-length enforce using toHaveLength() ๐ŸŒ ๐Ÿ”ง
prefer-todo enforce using test.todo ๐ŸŒ ๐Ÿ”ง
prefer-vi-mocked require vi.mocked() over fn as Mock ๐ŸŒ ๐Ÿ”ง ๐Ÿ’ญ
require-awaited-expect-poll ensure that every expect.poll call is awaited ๐ŸŒ
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 ๐ŸŒ
unbound-method enforce unbound methods are called with their expected scope ๐ŸŒ ๐Ÿ’ญ
valid-describe-callback enforce valid describe callback โœ… ๐ŸŒ
valid-expect enforce valid expect() usage โœ… ๐ŸŒ ๐Ÿ”ง
valid-expect-in-promise require promises that have expectations in their chain to be valid โœ… ๐ŸŒ
valid-title enforce valid titles โœ… ๐ŸŒ ๐Ÿ”ง
warn-todo disallow .todo usage

Credits

  • eslint-plugin-jest: most of the rules in this plugin are essentially ports of Jest plugin rules with minor modifications

Licence

MIT Licence ยฉ 2022 - present by veritem and contributors

About

eslint plugin for vitest

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors