Skip to content

Latest commit

 

History

History
201 lines (160 loc) · 12.8 KB

README.md

File metadata and controls

201 lines (160 loc) · 12.8 KB

eslint-plugin-vitest

npm ci

Eslint plugin for vitest

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install eslint-plugin-vitest:

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

Usage

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
      }
    ]
  }
}

Recommended

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.

all

To use the all configuration, extend it in your .eslintrc file:

{
  "extends": ["plugin:vitest/all"]
}

Running on test files only

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,
      },
    },
  },
];

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.

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-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-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

Requires Type Checking

Name Description 💼 ⚠️ 🔧 💡
unbound-method Enforce unbound methods are called with their expected scope

In order to use the rules powered by TypeScript type-checking, you must be using @typescript-eslint/parser & adjust your eslint config as outlined here.

Note that unlike the type-checking rules in @typescript-eslint/eslint-plugin, the rules here will fallback to doing nothing if type information is not available, meaning it's safe to include them in shared configs that could be used on JavaScript and TypeScript projects.

Also note that unbound-method depends on @typescript-eslint/eslint-plugin, as it extends the original unbound-method rule from that plugin.

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