Home

Awesome

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 vitest from "@vitest/eslint-plugin";

export default [
  {
    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 vitest from "@vitest/eslint-plugin";

export default [
  {
    files: ["tests/**"], // or any other pattern
    plugins: {
      vitest,
    },
    rules: {
      ...vitest.configs.recommended.rules,
    },
   settings: {
      vitest: {
        typecheck: true
      }
    },
    languageOptions: {
      globals: {
        ...vitest.environments.env.globals,
      },
    },
  },
]

Rules

<!-- begin auto-generated rules list -->

πŸ’Ό Configurations enabled in.
⚠️ Configurations set to warn in.
🌐 Set in the all configuration.
βœ… Set in the recommended configuration.
πŸ”§ Automatically fixable by the --fix CLI option.
πŸ’‘ Manually fixable by editor suggestions.
❌ Deprecated.

NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β DescriptionπŸ’Όβš οΈπŸ”§πŸ’‘βŒ
consistent-test-filenamerequire .spec test file pattern🌐
consistent-test-itenforce using test or it but not bothπŸŒπŸ”§
expect-expectenforce having expectation in test bodyβœ…
max-expectsenforce a maximum number of expect per test🌐
max-nested-describerequire describe block to be less than set max value or default value🌐
no-alias-methodsdisallow alias methodsπŸŒπŸ”§
no-commented-out-testsdisallow commented out testsβœ…
no-conditional-expectdisallow conditional expects🌐
no-conditional-in-testdisallow conditional tests🌐
no-conditional-testsdisallow conditional tests🌐
no-disabled-testsdisallow disabled tests🌐
no-done-callbackdisallow using a callback in asynchronous tests and hooksπŸŒπŸ’‘βŒ
no-duplicate-hooksdisallow duplicate hooks and teardown hooks🌐
no-focused-testsdisallow focused testsπŸŒπŸ”§
no-hooksdisallow setup and teardown hooks🌐
no-identical-titledisallow identical titlesβœ…πŸ”§
no-import-node-testdisallow importing node:testβœ…πŸ”§
no-interpolation-in-snapshotsdisallow string interpolation in snapshotsπŸŒπŸ”§
no-large-snapshotsdisallow large snapshots🌐
no-mocks-importdisallow importing from mocks directory🌐
no-restricted-matchersdisallow the use of certain matchers🌐
no-restricted-vi-methodsdisallow specific vi. methods🌐
no-standalone-expectdisallow using expect outside of it or test blocks🌐
no-test-prefixesdisallow using test as a prefixπŸŒπŸ”§
no-test-return-statementdisallow return statements in tests🌐
prefer-called-withenforce using toBeCalledWith() or toHaveBeenCalledWith()πŸŒπŸ”§
prefer-comparison-matcherenforce using the built-in comparison matchersπŸŒπŸ”§
prefer-eachenforce using each rather than manual loops🌐
prefer-equality-matcherenforce using the built-in quality matchersπŸŒπŸ’‘
prefer-expect-assertionsenforce using expect assertions instead of callbacksπŸŒπŸ’‘
prefer-expect-resolvesenforce using expect().resolves over expect(await ...) syntaxπŸŒπŸ”§
prefer-hooks-in-orderenforce having hooks in consistent order🌐
prefer-hooks-on-topenforce having hooks before any test cases🌐
prefer-lowercase-titleenforce lowercase titlesπŸŒπŸ”§
prefer-mock-promise-shorthandenforce mock resolved/rejected shorthands for promisesπŸŒπŸ”§
prefer-snapshot-hintenforce including a hint with external snapshots🌐
prefer-spy-onenforce using vi.spyOnπŸŒπŸ”§
prefer-strict-equalenforce strict equal over equalπŸŒπŸ’‘
prefer-to-beenforce using toBe()πŸŒπŸ”§
prefer-to-be-falsyenforce using toBeFalsy()πŸŒπŸ”§
prefer-to-be-objectenforce using toBeObject()πŸŒπŸ”§
prefer-to-be-truthyenforce using toBeTruthyπŸŒπŸ”§
prefer-to-containenforce using toContain()πŸŒπŸ”§
prefer-to-have-lengthenforce using toHaveLength()πŸŒπŸ”§
prefer-todoenforce using test.todoπŸŒπŸ”§
prefer-vi-mockedPrefer vi.mocked() over fn as MockπŸŒπŸ”§
require-hookrequire setup and teardown to be within a hook🌐
require-local-test-context-for-concurrent-snapshotsrequire local Test Context for concurrent snapshot testsβœ…
require-to-throw-messagerequire toThrow() to be called with an error message🌐
require-top-level-describeenforce that all tests are in a top-level describe🌐
valid-describe-callbackenforce valid describe callbackβœ…
valid-expectenforce valid expect() usageβœ…
valid-titleenforce valid titlesβœ…πŸ”§
<!-- end auto-generated rules list -->

Credits

Licence

MIT Licence Β© 2022 - present veritem and contributors