Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

ESLint plugin for Jest
https://github.com/jest-community/eslint-plugin-jest

eslint eslint-plugin jest

Last synced: about 1 month ago
JSON representation

ESLint plugin for Jest

Awesome Lists containing this project

README

        








eslint-plugin-jest


ESLint plugin for Jest


[![Actions Status](https://github.com/jest-community/eslint-plugin-jest/actions/workflows/nodejs.yml/badge.svg?branch=main)](https://github.com/jest-community/eslint-plugin-jest/actions)

## Installation

```bash
yarn add --dev eslint eslint-plugin-jest
```

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

## Usage

> [!NOTE]
>
> `eslint.config.js` is supported, though most of the plugin documentation still
> currently uses `.eslintrc` syntax.
>
> Refer to the
> [ESLint documentation on the new configuration file format](https://eslint.org/docs/latest/use/configure/configuration-files-new)
> for more.

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

```json
{
"plugins": ["jest"]
}
```

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

```json
{
"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:

```json
{
"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:

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

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

For `.eslintrc` configs you can use
[overrides](https://eslint.org/docs/user-guide/configuring/configuration-files#how-do-overrides-work)
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 use
[`files` and `ignores`](https://eslint.org/docs/latest/use/configure/configuration-files-new#specifying-files-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' },
},
];
```

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

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

## Shareable configurations

> [!NOTE]
>
> `eslint.config.js` compatible versions of configs are available prefixed with
> `flat/` and may be subject to small breaking changes while ESLint v9 is being
> finalized.

### Recommended

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`, use
`jest.configs['flat/recommended']`:

```js
const jest = require('eslint-plugin-jest');

module.exports = [
{
files: [
/* glob matching your test files */
],
...jest.configs['flat/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:

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

To enable this configuration with `eslint.config.js`, use
`jest.configs['flat/style']`:

```js
const jest = require('eslint-plugin-jest');

module.exports = [
{
files: [
/* glob matching your test files */
],
...jest.configs['flat/style'],
},
];
```

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

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

To enable this configuration with `eslint.config.js`, use
`jest.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 versions
the `all` configuration may change in any release and is thus unsuited for
installations requiring long-term consistency.

## Rules

πŸ’Ό
[Configurations](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations)
enabled in.\
⚠️ [Configurations](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations)
set to warn in.\
βœ… Set in the `recommended`
[configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\
🎨
Set in the `style`
[configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\
πŸ”§
Automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
πŸ’‘
Manually fixable by
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | πŸ”§ | πŸ’‘ |
| :--------------------------------------------------------------------------- | :------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce `test` and `it` usage conventions | | | πŸ”§ | |
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | | βœ… | | |
| [max-expects](docs/rules/max-expects.md) | Enforces a maximum number assertion calls in a test body | | | | |
| [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | | | |
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | βœ… | 🎨 | πŸ”§ | |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | | βœ… | | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally | βœ… | | | |
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | | | |
| [no-confusing-set-timeout](docs/rules/no-confusing-set-timeout.md) | Disallow confusing usages of jest.setTimeout | | | | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | βœ… | | πŸ”§ | |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | | βœ… | | |
| [no-done-callback](docs/rules/no-done-callback.md) | Disallow using a callback in asynchronous tests and hooks | βœ… | | | πŸ’‘ |
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | | | |
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | βœ… | | | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | βœ… | | | πŸ’‘ |
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | | | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | βœ… | | | |
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | βœ… | | | |
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | βœ… | | πŸ”§ | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | | | |
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | βœ… | | | |
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md) | Disallow specific `jest.` methods | | | | |
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | | | |
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | βœ… | | | |
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Require using `.only` and `.skip` over `f` and `x` | βœ… | | πŸ”§ | |
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | | | |
| [no-untyped-mock-factory](docs/rules/no-untyped-mock-factory.md) | Disallow using `jest.mock()` factories without an explicit type parameter | | | πŸ”§ | |
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | | | |
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | | πŸ”§ | |
| [prefer-each](docs/rules/prefer-each.md) | Prefer using `.each` rather than manual loops | | | | |
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | | | πŸ’‘ |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | | | πŸ’‘ |
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | | πŸ”§ | |
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | | | |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | | |
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | πŸ”§ | |
| [prefer-jest-mocked](docs/rules/prefer-jest-mocked.md) | Prefer `jest.mocked()` over `fn as jest.Mock` | | | πŸ”§ | |
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | πŸ”§ | |
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | πŸ”§ | |
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | | πŸ”§ | |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | | | πŸ’‘ |
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | 🎨 | | πŸ”§ | |
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | 🎨 | | πŸ”§ | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | 🎨 | | πŸ”§ | |
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | | πŸ”§ | |
| [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | | | |
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | | | |
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | | | |
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | βœ… | | | |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | βœ… | | πŸ”§ | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | βœ… | | | |
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | βœ… | | πŸ”§ | |

### Requires Type Checking

| NameΒ Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | πŸ”§ | πŸ’‘ |
| :--------------------------------------------- | :----------------------------------------------------------- | :-- | :-- | :-- | :-- |
| [unbound-method](docs/rules/unbound-method.md) | 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](https://typescript-eslint.io/linting/typed-linting).

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

- [eslint-plugin-mocha](https://github.com/lo1tuma/eslint-plugin-mocha)
- [eslint-plugin-jasmine](https://github.com/tlvince/eslint-plugin-jasmine)

## 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/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.

### eslint-plugin-istanbul

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