ESLint rules for Jest
npm install eslint-plugin-jest
``bash`
yarn add --dev eslint eslint-plugin-jest
Note: If you installed ESLint globally then you must also install
eslint-plugin-jest globally.
If you're using flat configuration:
With
flat configuration,
just import the plugin and away you go:
`js
const pluginJest = require('eslint-plugin-jest');
module.exports = [
{
// update this to match your test files
files: ['/.spec.js', '/.test.js'],
plugins: { jest: pluginJest },
languageOptions: {
globals: pluginJest.environments.globals.globals,
},
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',
},
},
];
`
With legacy configuration, add jest to the plugins section of your .eslintrceslint-plugin-
configuration file. You can omit the prefix:
`json`
{
"plugins": ["jest"],
"env": {
"jest/globals": true
},
"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"
}
}
> [!NOTE]
>
> You only need to explicitly include our globals if you're not using one of our
> shared configs
#### Aliased Jest globals
You can tell this plugin about any global Jests you have aliased using the
globalAliases setting:
`json`
{
"settings": {
"jest": {
"globalAliases": {
"describe": ["context"],
"fdescribe": ["fcontext"],
"xdescribe": ["xcontext"]
}
}
}
}
#### Aliased @jest/globals
You can tell this plugin to treat a different package as the source of Jest
globals using the globalPackage setting:
`json`
{
"settings": {
"jest": {
"globalPackage": "bun:test"
}
}
}
> [!WARNING]
>
> While this can be used to apply rules when using alternative testing libraries
> and frameworks like bun, vitest and node, there's no guarantee the
> semantics this plugin assumes will hold outside of Jest
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.
For .eslintrc configs you can use
overrides
to have ESLint apply additional rules to specific files:
`json`
{
"extends": ["eslint:recommended"],
"overrides": [
{
"files": ["test/**"],
"plugins": ["jest"],
"extends": ["plugin:jest/recommended"],
"rules": { "jest/prefer-expect-assertions": "off" }
}
],
"rules": {
"indent": ["error", 2]
}
}
For eslint.config.js you can usefiles and ignores:
`js
const jest = require('eslint-plugin-jest');
module.exports = [
...require('@eslint/js').configs.recommended,
{
files: ['test/**'],
...jest.configs['flat/recommended'],
rules: {
...jest.configs['flat/recommended'].rules,
'jest/prefer-expect-assertions': 'off',
},
},
// you can also configure jest rules in other objects, so long as some of the files match`
{
files: ['test/**'],
rules: { 'jest/prefer-expect-assertions': 'off' },
},
];
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:
`json`
{
"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:
`js`
module.exports = {
settings: {
jest: {
version: require('jest/package.json').version,
},
},
};
> [!NOTE]
>
> eslint.config.js compatible versions of configs are available prefixed withflat/
> and may be subject to small breaking changes while ESLint v9 is being
> finalized.
This plugin exports a recommended configuration that enforces good testing
practices.
To enable this configuration with .eslintrc, use the extends property:
`json`
{
"extends": ["plugin:jest/recommended"]
}
To enable this configuration with eslint.config.js, usejest.configs['flat/recommended']:
`js
const jest = require('eslint-plugin-jest');
module.exports = [
{
files: [
/ glob matching your test files /
],
...jest.configs['flat/recommended'],
},
];
`
This plugin also exports a configuration named style, which adds someprefer-to-be-null
stylistic rules, such as , which enforces usage of toBeNulltoBe(null)
over .
To enable this configuration use the extends property in your .eslintrc
config file:
`json`
{
"extends": ["plugin:jest/style"]
}
To enable this configuration with eslint.config.js, usejest.configs['flat/style']:
`js
const jest = require('eslint-plugin-jest');
module.exports = [
{
files: [
/ glob matching your test files /
],
...jest.configs['flat/style'],
},
];
`
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:
`json`
{
"extends": ["plugin:jest/all"]
}
To enable this configuration with eslint.config.js, usejest.configs['flat/all']:
`js
const jest = require('eslint-plugin-jest');
module.exports = [
{
files: [
/ glob matching your test files /
],
...jest.configs['flat/all'],
},
];
`
While the recommended and style configurations only change in major versionsall
the configuration may change in any release and is thus unsuited for
installations requiring long-term consistency.
πΌ
Configurations
enabled in.\
β οΈ Configurations
set to warn in.\
β
Set in the recommendedstyle
configuration.\
π¨
Set in the --fix
configuration.\
π§
Automatically fixable by the CLI option.\
π‘
Manually fixable by
editor suggestions.
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π§ | π‘ |
| :----------------------------------------------------------------------------------- | :------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
| consistent-test-it | Enforce test and it usage conventions | | | π§ | |expect
| 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 conditionally | β
| | | |exports
| 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 in files containing tests | β
| | | |__mocks__
| 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 inside snapshots | β
| | | |
| no-jasmine-globals | Disallow Jasmine globals | β
| | π§ | |
| no-large-snapshots | Disallow large snapshots | | | | |
| no-mocks-import | Disallow manually importing from | β
| | | |jest.
| no-restricted-jest-methods | Disallow specific methods | | | | |expect
| no-restricted-matchers | Disallow specific matchers & modifiers | | | | |
| no-standalone-expect | Disallow using outside of it or test blocks | β
| | | |.only
| no-test-prefixes | Require using and .skip over f and x | β
| | π§ | |jest.mock()
| no-test-return-statement | Disallow explicitly returning from tests | | | | |
| no-unneeded-async-expect-function | Disallow unnecessary async function wrapper for expected promises | | | π§ | |
| no-untyped-mock-factory | Disallow using factories without an explicit type parameter | | | π§ | |afterAll
| padding-around-after-all-blocks | Enforce padding around blocks | | | π§ | |afterEach
| padding-around-after-each-blocks | Enforce padding around blocks | | | π§ | |beforeAll
| padding-around-all | Enforce padding around Jest functions | | | π§ | |
| padding-around-before-all-blocks | Enforce padding around blocks | | | π§ | |beforeEach
| padding-around-before-each-blocks | Enforce padding around blocks | | | π§ | |describe
| padding-around-describe-blocks | Enforce padding around blocks | | | π§ | |expect
| padding-around-expect-groups | Enforce padding around groups | | | π§ | |test
| padding-around-test-blocks | Enforce padding around and it blocks | | | π§ | |toHaveBeenCalledWith()
| prefer-called-with | Suggest using | | | | |.each
| prefer-comparison-matcher | Suggest using the built-in comparison matchers | | | π§ | |
| prefer-each | Prefer using rather than manual loops | | | | |expect.assertions()
| prefer-ending-with-an-expect | Prefer having the last statement in a test be an assertion | | | | |
| prefer-equality-matcher | Suggest using the built-in equality matchers | | | | π‘ |
| prefer-expect-assertions | Suggest using OR expect.hasAssertions() | | | | π‘ |await expect(...).resolves
| prefer-expect-resolves | Prefer over expect(await ...) syntax | | | π§ | |jest.mocked()
| prefer-hooks-in-order | Prefer having hooks in a consistent order | | | | |
| prefer-hooks-on-top | Suggest having hooks before any test cases | | | | |
| prefer-importing-jest-globals | Prefer importing Jest globals | | | π§ | |
| prefer-jest-mocked | Prefer over fn as jest.Mock | | | π§ | |jest.spyOn()
| prefer-lowercase-title | Enforce lowercase test names | | | π§ | |
| prefer-mock-promise-shorthand | Prefer mock resolved/rejected shorthands for promises | | | π§ | |
| prefer-mock-return-shorthand | Prefer mock return shorthands | | | π§ | |
| prefer-snapshot-hint | Prefer including a hint with external snapshots | | | | |
| prefer-spy-on | Suggest using | | | π§ | |toStrictEqual()
| prefer-strict-equal | Suggest using | | | | π‘ |toBe()
| prefer-to-be | Suggest using for primitive literals | π¨ | | π§ | |toContain()
| prefer-to-contain | Suggest using | π¨ | | π§ | |toHaveBeenCalled
| prefer-to-have-been-called | Suggest using | | | π§ | |toHaveBeenCalledTimes()
| prefer-to-have-been-called-times | Suggest using | | | π§ | |toHaveLength()
| prefer-to-have-length | Suggest using | π¨ | | π§ | |test.todo
| prefer-todo | Suggest using | | | π§ | |toThrow()
| require-hook | Require setup and teardown code to be within a hook | | | | |
| require-to-throw-message | Require a message for | | | | |describe
| require-top-level-describe | Require test cases and hooks to be inside a block | | | | |describe()
| valid-describe-callback | Enforce valid callback | β
| | | |expect()
| valid-expect | Enforce valid usage | β
| | π§ | |
| valid-expect-in-promise | Require promises that have expectations in their chain to be valid | β
| | | |
| valid-mock-module-path | Disallow mocking of non-existing module paths | | | | |
| valid-title | Enforce valid titles | β
| | π§ | |
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π§ | π‘ |
| :------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
| no-error-equal | Disallow using equality matchers on error types | | | | |
| no-unnecessary-assertion | Disallow unnecessary assertions based on types | | | | |
| unbound-method | Enforce unbound methods are called with their expected scope | | | | |
| valid-expect-with-promise | Require that resolve and reject modifiers are present (and only) for promise-like types | | | | |
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,unbound-method
as it extends the original rule from that plugin.
- eslint-plugin-mocha
- eslint-plugin-jasmine
This is a sister plugin to eslint-plugin-jest that provides support for thejest-extended
matchers provided by.
This project aims to provide formatting rules (auto-fixable where possible) to
ensure consistency and readability in jest test suites.
A set of rules to enforce good practices for Istanbul, one of the code coverage
tools used by Jest.
[no-deprecated-functions`]: docs/rules/no-deprecated-functions.md