Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vitest/eslint-plugin

Package Overview
Dependencies
Maintainers
5
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vitest/eslint-plugin

Eslint plugin for vitest

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
354K
increased by11.96%
Maintainers
5
Weekly downloads
 
Created
Source

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

💼 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🌐🔧
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🔧
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 veritem and contributors

Keywords

FAQs

Package last updated on 13 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc