eslint-plugin-jest
ESLint rules for Jest
Last updated 10 months ago by simenb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install eslint-plugin-jest 
SYNC missed versions from official npm registry.

eslint-plugin-jest

ESLint plugin for Jest

Actions Status

Installation

yarn add --dev eslint eslint-plugin-jest

Note: If you installed ESLint globally then you must also install eslint-plugin-jest globally.

Usage

Add jest to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["jest"]
}

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

{
  "rules": {
    "jest/no-disabled-tests": "warn",
    "jest/no-focused-tests": "error",
    "jest/no-identical-title": "error",
    "jest/prefer-to-have-length": "warn",
    "jest/valid-expect": "error"
  }
}

You can also tell ESLint about the environment variables provided by Jest by doing:

{
  "env": {
    "jest/globals": true
  }
}

This is included in all configs shared by this plugin, so can be omitted if extending them.

Aliased Jest globals

You can tell this plugin about any global Jests you have aliased using the globalAliases setting:

{
  "settings": {
    "jest": {
      "globalAliases": {
        "describe": ["context"],
        "fdescribe": ["fcontext"],
        "xdescribe": ["xcontext"]
      }
    }
  }
}

Running rules only on test-related files

The rules provided by this plugin assume that the files they are checking are test-related. This means it's generally not suitable to include them in your top-level configuration as that applies to all files being linted which can include source files.

You can use overrides to have ESLint apply additional rules to specific files:

{
  "extends": ["eslint:recommended"],
  "overrides": [
    {
      "files": ["test/**"],
      "plugins": ["jest"],
      "extends": ["plugin:jest/recommended"],
      "rules": { "jest/prefer-expect-assertions": "off" }
    }
  ],
  "rules": {
    "indent": ["error", 2]
  }
}

Jest version setting

The behaviour of some rules (specifically no-deprecated-functions) change depending on the version of Jest being used.

By default, this plugin will attempt to determine to locate Jest using require.resolve, meaning it will start looking in the closest node_modules folder to the file being linted and work its way up.

Since we cache the automatically determined version, if you're linting sub-folders that have different versions of Jest, you may find that the wrong version of Jest is considered when linting. You can work around this by providing the Jest version explicitly in nested ESLint configs:

{
  "settings": {
    "jest": {
      "version": 27
    }
  }
}

To avoid hard-coding a number, you can also fetch it from the installed version of Jest if you use a JavaScript config file such as .eslintrc.js:

module.exports = {
  settings: {
    jest: {
      version: require('jest/package.json').version,
    },
  },
};

Shareable configurations

Recommended

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

To enable this configuration use the extends property in your .eslintrc config file:

{
  "extends": ["plugin:jest/recommended"]
}

Style

This plugin also exports a configuration named style, which adds some stylistic rules, such as prefer-to-be-null, which enforces usage of toBeNull over toBe(null).

To enable this configuration use the extends property in your .eslintrc config file:

{
  "extends": ["plugin:jest/style"]
}

See ESLint documentation for more information about extending configuration files.

All

If you want to enable all rules instead of only some you can do so by adding the all configuration to your .eslintrc config file:

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

While the recommended and style configurations only change in major versions the all configuration may change in any release and is thus unsuited for installations requiring long-term consistency.

Rules

💼 Configurations enabled in.
⚠️ Configurations set to warn in.
✅ Set in the recommended configuration.
🎨 Set in the style configuration.
🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.
❌ Deprecated.

Name Description 💼 ⚠️ 🔧 💡
consistent-test-it Enforce test and it usage conventions 🔧
expect-expect Enforce assertion to be made in a test body
max-expects Enforces a maximum number assertion calls in a test body
max-nested-describe Enforces a maximum depth to nested describe calls
no-alias-methods Disallow alias methods 🎨 🔧
no-commented-out-tests Disallow commented out tests
no-conditional-expect Disallow calling expect conditionally
no-conditional-in-test Disallow conditional logic in tests
no-confusing-set-timeout Disallow confusing usages of jest.setTimeout
no-deprecated-functions Disallow use of deprecated functions 🔧
no-disabled-tests Disallow disabled tests
no-done-callback Disallow using a callback in asynchronous tests and hooks 💡
no-duplicate-hooks Disallow duplicate setup and teardown hooks
no-export Disallow using exports in files containing tests
no-focused-tests Disallow focused tests 💡
no-hooks Disallow setup and teardown hooks
no-identical-title Disallow identical titles
no-if Disallow conditional logic
no-interpolation-in-snapshots Disallow string interpolation inside snapshots
no-jasmine-globals Disallow Jasmine globals 🔧
no-large-snapshots Disallow large snapshots
no-mocks-import Disallow manually importing from __mocks__
no-restricted-jest-methods Disallow specific jest. methods
no-restricted-matchers Disallow specific matchers & modifiers
no-standalone-expect Disallow using expect outside of it or test blocks
no-test-prefixes Require using .only and .skip over f and x 🔧
no-test-return-statement Disallow explicitly returning from tests
no-untyped-mock-factory Disallow using jest.mock() factories without an explicit type parameter 🔧
prefer-called-with Suggest using toBeCalledWith() or toHaveBeenCalledWith()
prefer-comparison-matcher Suggest using the built-in comparison matchers 🔧
prefer-each Prefer using .each rather than manual loops
prefer-equality-matcher Suggest using the built-in equality matchers 💡
prefer-expect-assertions Suggest using expect.assertions() OR expect.hasAssertions() 💡
prefer-expect-resolves Prefer await expect(...).resolves over expect(await ...) syntax 🔧
prefer-hooks-in-order Prefer having hooks in a consistent order
prefer-hooks-on-top Suggest having hooks before any test cases
prefer-lowercase-title Enforce lowercase test names 🔧
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 jest.spyOn() 🔧
prefer-strict-equal Suggest using toStrictEqual() 💡
prefer-to-be Suggest using toBe() for primitive literals 🎨 🔧
prefer-to-contain Suggest using toContain() 🎨 🔧
prefer-to-have-length Suggest using toHaveLength() 🎨 🔧
prefer-todo Suggest using test.todo 🔧
require-hook Require setup and teardown code to be within a hook
require-to-throw-message Require a message for toThrow()
require-top-level-describe Require test cases and hooks to be inside a describe block
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 🔧

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.

Credit

Related Projects

eslint-plugin-jest-extended

This is a sister plugin to eslint-plugin-jest that provides support for the matchers provided by jest-extended.

https://github.com/jest-community/eslint-plugin-jest-extended

eslint-plugin-jest-formatting

This project aims to provide formatting rules (auto-fixable where possible) to ensure consistency and readability in jest test suites.

https://github.com/dangreenisrael/eslint-plugin-jest-formatting

eslint-plugin-istanbul

A set of rules to enforce good practices for Istanbul, one of the code coverage tools used by Jest.

https://github.com/istanbuljs/eslint-plugin-istanbul

Current Tags

  • 28.8.3                                ...           latest (4 months ago)
  • 28.0.0-next.7                                ...           next (9 months ago)

325 Versions

  • 28.8.3                                ...           4 months ago
  • 28.8.2                                ...           4 months ago
  • 28.8.1                                ...           4 months ago
  • 28.8.0                                ...           5 months ago
  • 28.7.0                                ...           5 months ago
  • 28.6.0                                ...           7 months ago
  • 28.5.0                                ...           8 months ago
  • 28.4.0                                ...           8 months ago
  • 28.3.0                                ...           8 months ago
  • 28.2.0                                ...           9 months ago
  • 28.1.1                                ...           9 months ago
  • 28.1.0                                ...           9 months ago
  • 28.0.0                                ...           9 months ago
  • 28.0.0-next.7                                ...           9 months ago
  • 28.0.0-next.6                                ...           9 months ago
  • 28.0.0-next.5                                ...           9 months ago
  • 28.0.0-next.4                                ...           9 months ago
  • 28.0.0-next.3                                ...           9 months ago
  • 28.0.0-next.2                                ...           9 months ago
  • 28.0.0-next.1                                ...           9 months ago
  • 27.9.0                                ...           10 months ago
  • 27.8.0-next.1                                ...           10 months ago
  • 27.8.0                                ...           10 months ago
  • 27.7.0                                ...           10 months ago
  • 27.6.3                                ...           a year ago
  • 27.6.2                                ...           a year ago
  • 27.6.1                                ...           a year ago
  • 27.6.0                                ...           a year ago
  • 27.5.0                                ...           a year ago
  • 27.4.3                                ...           a year ago
  • 27.4.2                                ...           a year ago
  • 27.4.1                                ...           a year ago
  • 27.4.0                                ...           a year ago
  • 27.3.0                                ...           a year ago
  • 27.2.3                                ...           a year ago
  • 27.2.2                                ...           2 years ago
  • 27.2.1                                ...           2 years ago
  • 27.2.0                                ...           2 years ago
  • 27.1.7                                ...           2 years ago
  • 27.1.6                                ...           2 years ago
  • 27.1.5                                ...           2 years ago
  • 27.1.4                                ...           2 years ago
  • 27.1.3                                ...           2 years ago
  • 27.1.2                                ...           2 years ago
  • 27.1.1                                ...           2 years ago
  • 27.1.0                                ...           2 years ago
  • 27.0.4                                ...           2 years ago
  • 27.0.3                                ...           2 years ago
  • 27.0.2                                ...           2 years ago
  • 27.0.1                                ...           2 years ago
  • 27.0.0                                ...           2 years ago
  • 27.0.0-next.2                                ...           2 years ago
  • 26.9.0                                ...           2 years ago
  • 27.0.0-next.1                                ...           2 years ago
  • 26.8.7                                ...           2 years ago
  • 26.8.6                                ...           2 years ago
  • 26.8.5                                ...           2 years ago
  • 26.8.4                                ...           2 years ago
  • 26.8.3                                ...           2 years ago
  • 26.8.2                                ...           2 years ago
  • 26.8.1                                ...           2 years ago
  • 26.8.0                                ...           2 years ago
  • 26.7.0                                ...           2 years ago
  • 26.6.0                                ...           2 years ago
  • 26.5.3                                ...           3 years ago
  • 26.5.2                                ...           3 years ago
  • 26.5.1                                ...           3 years ago
  • 26.5.0                                ...           3 years ago
  • 26.4.7                                ...           3 years ago
  • 26.4.6                                ...           3 years ago
  • 26.4.5                                ...           3 years ago
  • 26.4.4                                ...           3 years ago
  • 26.4.3                                ...           3 years ago
  • 26.4.2                                ...           3 years ago
  • 26.4.1                                ...           3 years ago
  • 26.4.0                                ...           3 years ago
  • 26.3.0                                ...           3 years ago
  • 26.2.2                                ...           3 years ago
  • 26.2.1                                ...           3 years ago
  • 26.2.0                                ...           3 years ago
  • 26.1.5                                ...           3 years ago
  • 26.1.4                                ...           3 years ago
  • 26.1.3                                ...           3 years ago
  • 26.1.2                                ...           3 years ago
  • 26.1.1                                ...           3 years ago
  • 26.1.0                                ...           3 years ago
  • 26.1.0-next.2                                ...           3 years ago
  • 26.1.0-next.1                                ...           3 years ago
  • 26.0.0                                ...           3 years ago
  • 25.7.0                                ...           3 years ago
  • 25.6.0                                ...           3 years ago
  • 25.5.0                                ...           3 years ago
  • 25.4.0                                ...           3 years ago
  • 25.3.4                                ...           3 years ago
  • 25.3.3                                ...           3 years ago
  • 25.3.2                                ...           3 years ago
  • 25.3.1                                ...           3 years ago
  • 25.3.0                                ...           3 years ago
  • 25.2.4                                ...           3 years ago
  • 25.2.3                                ...           3 years ago
  • 25.2.2                                ...           3 years ago
  • 25.2.1                                ...           3 years ago
  • 25.2.0                                ...           3 years ago
  • 25.1.0                                ...           3 years ago
  • 25.0.6                                ...           3 years ago
  • 25.0.5                                ...           3 years ago
  • 25.0.4                                ...           3 years ago
  • 25.0.3                                ...           3 years ago
  • 25.0.2                                ...           3 years ago
  • 25.0.1                                ...           3 years ago
  • 25.0.0                                ...           3 years ago
  • 25.0.0-next.7                                ...           3 years ago
  • 25.0.0-next.6                                ...           3 years ago
  • 24.7.0                                ...           3 years ago
  • 24.6.0                                ...           3 years ago
  • 24.5.2                                ...           3 years ago
  • 24.5.1                                ...           3 years ago
  • 25.0.0-next.5                                ...           3 years ago
  • 24.5.0                                ...           3 years ago
  • 24.4.3                                ...           3 years ago
  • 25.0.0-next.4                                ...           3 years ago
  • 24.4.2                                ...           3 years ago
  • 25.0.0-next.3                                ...           3 years ago
  • 24.4.1                                ...           3 years ago
  • 25.0.0-next.2                                ...           3 years ago
  • 25.0.0-next.1                                ...           3 years ago
  • 24.4.0                                ...           3 years ago
  • 24.3.7                                ...           3 years ago
  • 24.3.6                                ...           4 years ago
  • 24.3.5                                ...           4 years ago
  • 24.3.4                                ...           4 years ago
  • 24.3.3                                ...           4 years ago
  • 24.3.2                                ...           4 years ago
  • 24.3.1                                ...           4 years ago
  • 24.3.0                                ...           4 years ago
  • 24.2.1                                ...           4 years ago
  • 24.2.0                                ...           4 years ago
  • 24.1.10                                ...           4 years ago
  • 24.1.9                                ...           4 years ago
  • 24.1.8                                ...           4 years ago
  • 24.1.7                                ...           4 years ago
  • 24.1.6                                ...           4 years ago
  • 24.1.5                                ...           4 years ago
  • 24.1.4 [deprecated]           ...           4 years ago
  • 24.1.3                                ...           4 years ago
  • 24.1.2                                ...           4 years ago
  • 24.1.1                                ...           4 years ago
  • 24.1.0                                ...           4 years ago
  • 24.0.2                                ...           4 years ago
  • 24.0.1                                ...           4 years ago
  • 24.0.0                                ...           4 years ago
  • 23.20.0                                ...           4 years ago
  • 23.19.0                                ...           4 years ago
  • 23.18.2                                ...           4 years ago
  • 23.18.1                                ...           4 years ago
  • 23.18.0                                ...           4 years ago
  • 23.17.1                                ...           5 years ago
  • 23.17.0                                ...           5 years ago
  • 23.16.0                                ...           5 years ago
  • 23.15.0                                ...           5 years ago
  • 23.14.0                                ...           5 years ago
  • 23.13.2                                ...           5 years ago
  • 23.13.1                                ...           5 years ago
  • 23.13.0                                ...           5 years ago
  • 23.12.0                                ...           5 years ago
  • 23.11.0                                ...           5 years ago
  • 23.10.0                                ...           5 years ago
  • 23.9.0                                ...           5 years ago
  • 23.8.2                                ...           5 years ago
  • 23.8.1                                ...           5 years ago
  • 23.8.0                                ...           5 years ago
  • 23.7.0                                ...           5 years ago
  • 23.6.0                                ...           5 years ago
  • 23.5.0                                ...           5 years ago
  • 23.4.0                                ...           5 years ago
  • 23.3.0                                ...           5 years ago
  • 23.2.0                                ...           5 years ago
  • 23.1.1                                ...           5 years ago
  • 23.1.0                                ...           5 years ago
  • 23.0.5                                ...           5 years ago
  • 23.0.4                                ...           5 years ago
  • 23.0.3                                ...           5 years ago
  • 23.0.2                                ...           5 years ago
  • 23.0.1                                ...           5 years ago
  • 23.0.0                                ...           5 years ago
  • 22.21.0                                ...           5 years ago
  • 22.20.1                                ...           5 years ago
  • 22.20.0                                ...           5 years ago
  • 22.19.0                                ...           5 years ago
  • 22.18.0                                ...           5 years ago
  • 22.17.0                                ...           5 years ago
  • 22.16.0                                ...           5 years ago
  • 22.15.2                                ...           5 years ago
  • 22.15.1                                ...           5 years ago
  • 22.15.0                                ...           5 years ago
  • 22.14.1                                ...           5 years ago
  • 22.14.0                                ...           5 years ago
  • 22.13.7                                ...           5 years ago
  • 22.13.6                                ...           5 years ago
  • 22.13.5                                ...           5 years ago
  • 22.13.4                                ...           5 years ago
  • 22.13.3                                ...           5 years ago
  • 22.13.2                                ...           5 years ago
  • 22.13.1                                ...           5 years ago
  • 22.13.0                                ...           5 years ago
  • 22.12.0                                ...           5 years ago
  • 22.11.1                                ...           5 years ago
  • 22.11.0                                ...           5 years ago
  • 22.10.0                                ...           5 years ago
  • 22.9.0                                ...           5 years ago
  • 22.8.0                                ...           5 years ago
  • 22.7.2                                ...           5 years ago
  • 22.7.1                                ...           6 years ago
  • 22.7.0                                ...           6 years ago
  • 22.6.4                                ...           6 years ago
  • 22.6.3                                ...           6 years ago
  • 22.6.2                                ...           6 years ago
  • 22.6.1                                ...           6 years ago
  • 22.6.0                                ...           6 years ago
  • 22.5.1                                ...           6 years ago
  • 22.5.0                                ...           6 years ago
  • 22.4.1                                ...           6 years ago
  • 22.4.0                                ...           6 years ago
  • 22.3.2                                ...           6 years ago
  • 22.3.1                                ...           6 years ago
  • 22.3.0                                ...           6 years ago
  • 22.2.2                                ...           6 years ago
  • 22.2.1                                ...           6 years ago
  • 22.2.0                                ...           6 years ago
  • 22.1.3                                ...           6 years ago
  • 22.1.2                                ...           6 years ago
  • 22.1.1                                ...           6 years ago
  • 22.1.0                                ...           6 years ago
  • 22.0.1                                ...           6 years ago
  • 22.0.0                                ...           6 years ago
  • 21.27.2                                ...           6 years ago
  • 21.27.1                                ...           6 years ago
  • 21.27.0                                ...           6 years ago
  • 21.26.2                                ...           6 years ago
  • 21.26.1                                ...           6 years ago
  • 21.26.0                                ...           6 years ago
  • 21.25.1                                ...           6 years ago
  • 21.25.0                                ...           6 years ago
  • 21.24.2                                ...           6 years ago
  • 21.24.1                                ...           6 years ago
  • 21.24.0                                ...           6 years ago
  • 21.23.0                                ...           6 years ago
  • 21.22.1                                ...           6 years ago
  • 21.22.0                                ...           6 years ago
  • 21.21.0                                ...           6 years ago
  • 21.20.2                                ...           6 years ago
  • 21.20.1                                ...           6 years ago
  • 21.20.0                                ...           6 years ago
  • 21.19.0                                ...           6 years ago
  • 21.18.0                                ...           6 years ago
  • 21.17.0                                ...           7 years ago
  • 21.16.1                                ...           7 years ago
  • 21.16.0                                ...           7 years ago
  • 21.15.3                                ...           7 years ago
  • 21.15.2                                ...           7 years ago
  • 21.15.1                                ...           7 years ago
  • 21.15.0                                ...           7 years ago
  • 21.14.1                                ...           7 years ago
  • 21.14.0                                ...           7 years ago
  • 21.13.0                                ...           7 years ago
  • 21.12.3                                ...           7 years ago
  • 21.12.2                                ...           7 years ago
  • 21.12.1                                ...           7 years ago
  • 21.12.0                                ...           7 years ago
  • 21.11.0                                ...           7 years ago
  • 21.10.0                                ...           7 years ago
  • 21.9.0                                ...           7 years ago
  • 21.8.0                                ...           7 years ago
  • 21.7.1                                ...           7 years ago
  • 21.7.0                                ...           7 years ago
  • 21.6.2                                ...           7 years ago
  • 21.6.1                                ...           7 years ago
  • 21.6.0                                ...           7 years ago
  • 21.5.0                                ...           7 years ago
  • 21.4.3                                ...           7 years ago
  • 21.4.2                                ...           7 years ago
  • 21.4.1                                ...           7 years ago
  • 21.4.0                                ...           7 years ago
  • 21.3.2                                ...           7 years ago
  • 21.3.1                                ...           7 years ago
  • 21.3.0                                ...           7 years ago
  • 21.3.0-beta.8                                ...           7 years ago
  • 21.3.0-beta.7                                ...           7 years ago
  • 21.3.0-beta.6                                ...           7 years ago
  • 21.3.0-beta.5                                ...           7 years ago
  • 21.3.0-beta.4                                ...           7 years ago
  • 21.3.0-beta.3                                ...           7 years ago
  • 21.3.0-alpha.eff7a1cf                                ...           7 years ago
  • 21.3.0-alpha.1e3ee68e                                ...           7 years ago
  • 21.2.0                                ...           7 years ago
  • 21.1.0                                ...           7 years ago
  • 21.0.2                                ...           7 years ago
  • 21.0.0                                ...           7 years ago
  • 21.0.0-beta.1                                ...           7 years ago
  • 21.0.0-alpha.2                                ...           7 years ago
  • 21.0.0-alpha.1                                ...           7 years ago
  • 20.1.0-echo.1                                ...           7 years ago
  • 20.1.0-delta.5                                ...           7 years ago
  • 20.1.0-delta.4                                ...           7 years ago
  • 20.1.0-delta.3                                ...           7 years ago
  • 20.1.0-delta.2                                ...           7 years ago
  • 20.1.0-delta.1                                ...           7 years ago
  • 20.1.0-chi.1                                ...           7 years ago
  • 20.1.0-beta.1                                ...           7 years ago
  • 20.1.0-alpha.3                                ...           7 years ago
  • 20.1.0-alpha.2                                ...           7 years ago
  • 20.1.0-alpha.1                                ...           7 years ago
  • 20.0.3                                ...           8 years ago
  • 20.0.2                                ...           8 years ago
  • 20.0.1                                ...           8 years ago
  • 20.0.0                                ...           8 years ago
  • 19.3.0-alpha.85402254                                ...           8 years ago
  • 19.2.0-alpha.993e64af                                ...           8 years ago
  • 19.1.0-alpha.eed82034                                ...           8 years ago
  • 19.0.1                                ...           8 years ago
  • 19.0.0                                ...           8 years ago
  • 18.5.0-alpha.7da3df39                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago

© 2010 - cnpmjs.org x YWFE | Home | YWFE