28.2.0 β€’ Published 12 days ago

eslint-plugin-jest v28.2.0

Weekly downloads
4,091,444
License
MIT
Repository
github
Last release
12 days ago

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

!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 for more.

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

Aliased @jest/globals

You can tell this plugin to treat a different package as the source of Jest globals using the globalPackage setting:

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

For eslint.config.js you can use files and ignores:

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:

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

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

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

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

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:

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

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

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:

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

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

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

NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β DescriptionπŸ’Όβš οΈπŸ”§πŸ’‘
consistent-test-itEnforce test and it usage conventionsπŸ”§
expect-expectEnforce assertion to be made in a test bodyβœ…
max-expectsEnforces a maximum number assertion calls in a test body
max-nested-describeEnforces a maximum depth to nested describe calls
no-alias-methodsDisallow alias methodsβœ…πŸŽ¨πŸ”§
no-commented-out-testsDisallow commented out testsβœ…
no-conditional-expectDisallow calling expect conditionallyβœ…
no-conditional-in-testDisallow conditional logic in tests
no-confusing-set-timeoutDisallow confusing usages of jest.setTimeout
no-deprecated-functionsDisallow use of deprecated functionsβœ…πŸ”§
no-disabled-testsDisallow disabled testsβœ…
no-done-callbackDisallow using a callback in asynchronous tests and hooksβœ…πŸ’‘
no-duplicate-hooksDisallow duplicate setup and teardown hooks
no-exportDisallow using exports in files containing testsβœ…
no-focused-testsDisallow focused testsβœ…πŸ’‘
no-hooksDisallow setup and teardown hooks
no-identical-titleDisallow identical titlesβœ…
no-interpolation-in-snapshotsDisallow string interpolation inside snapshotsβœ…
no-jasmine-globalsDisallow Jasmine globalsβœ…πŸ”§
no-large-snapshotsDisallow large snapshots
no-mocks-importDisallow manually importing from __mocks__βœ…
no-restricted-jest-methodsDisallow specific jest. methods
no-restricted-matchersDisallow specific matchers & modifiers
no-standalone-expectDisallow using expect outside of it or test blocksβœ…
no-test-prefixesRequire using .only and .skip over f and xβœ…πŸ”§
no-test-return-statementDisallow explicitly returning from tests
no-untyped-mock-factoryDisallow using jest.mock() factories without an explicit type parameterπŸ”§
prefer-called-withSuggest using toBeCalledWith() or toHaveBeenCalledWith()
prefer-comparison-matcherSuggest using the built-in comparison matchersπŸ”§
prefer-eachPrefer using .each rather than manual loops
prefer-equality-matcherSuggest using the built-in equality matchersπŸ’‘
prefer-expect-assertionsSuggest using expect.assertions() OR expect.hasAssertions()πŸ’‘
prefer-expect-resolvesPrefer await expect(...).resolves over expect(await ...) syntaxπŸ”§
prefer-hooks-in-orderPrefer having hooks in a consistent order
prefer-hooks-on-topSuggest having hooks before any test cases
prefer-importing-jest-globalsPrefer importing Jest globalsπŸ”§
prefer-lowercase-titleEnforce lowercase test namesπŸ”§
prefer-mock-promise-shorthandPrefer mock resolved/rejected shorthands for promisesπŸ”§
prefer-snapshot-hintPrefer including a hint with external snapshots
prefer-spy-onSuggest using jest.spyOn()πŸ”§
prefer-strict-equalSuggest using toStrictEqual()πŸ’‘
prefer-to-beSuggest using toBe() for primitive literalsπŸŽ¨πŸ”§
prefer-to-containSuggest using toContain()πŸŽ¨πŸ”§
prefer-to-have-lengthSuggest using toHaveLength()πŸŽ¨πŸ”§
prefer-todoSuggest using test.todoπŸ”§
require-hookRequire setup and teardown code to be within a hook
require-to-throw-messageRequire a message for toThrow()
require-top-level-describeRequire test cases and hooks to be inside a describe block
valid-describe-callbackEnforce valid describe() callbackβœ…
valid-expectEnforce valid expect() usageβœ…
valid-expect-in-promiseRequire promises that have expectations in their chain to be validβœ…
valid-titleEnforce valid titlesβœ…πŸ”§

Requires Type Checking

NameΒ Β Β Β Β Β Β Β Β Β DescriptionπŸ’Όβš οΈπŸ”§πŸ’‘
unbound-methodEnforce 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

eslint-config-react-appvitacontracts@brunormoreira/eslint-config@procore/eslint-config@jiralite/eslint-config-neon@huyhpham/rn-line@kaivanwong/eslint-config-ts@rain-star/eslint-config-ts@polkadot/dev@giveerr/rollpkg@backstage/cli@teambit/react@knapsack-cloud/msk-design-system@ridedott/eslint-configeasy-select-rn@powerlink/designsystem@rschristian/foobarbyteplayerkstick-react-scripts@wongbejoonline/eslint-plugin-wbotables-viewtable-maquantable-viewsstylexy@oliveiraswell/eslint-config-oliveiraswell-js@qhnu/ts-lib-template@zattoo/test@asep.setiawan/react-kitreact-native-bluetooth2noodles-normmobilelivedesignsystemreact@nuuji/js.config@osstotalsoft/pure-validations@tetondev/lint-configtrippkit@kolesa-group/eslint-config@moxy/test@doordash/eslint-config-doordashlint-configs@analogyxbi-ui/build-config@devesharp/typescriptopinion-pollkolesa-group-eslint-configeslint-config-dseslint-config-devesharpkg-stylelint-configeslint-config-hubsidedne-react-starteractive-react-scriptsephemeral-oauth2@ownzones/eslint-config@ffbutton/eslint-config-base@geekality/eslint-confignextf@outcome-co/eslint-config@zebra-c/eslintrceslint-config-slickpoc-shared-servereslint-config-angertitanws-scriptseslint-config-quartzt-kvlnk-configsmultisig-server@defencedigital/r2d2-lint-config@pohodnik/eslintreact-scripts-dkoujeleveslint-config-clicktimegusgard-eslint-configanjan-custom-react-scriptscra-must-have-librariescra-template-have-librarieseslint-plugin-zjlab@neat-preset/eslintreact-native-template-rfbaseeslint-config-demingdemingcustom-react-scripts-anjanluohe-react-scriptsluohe-react-tplreact-scripts-changedreact-scripts-oss@bookmate/eslint-bookmate-react@ctrine/eslint-config-sharedreact-scripts-os2dadachart@oliveiraswell/eslint-config-dasa-health-jsreact-scripts-anjanvp-desktop@bpoehland/eslint-configaboutbits-react-scriptscustom-cracra-anjandps-scriptsdps2cules-crademing-linterbadabam-react-scripts-spectacle@conten2/eslint-configform-viewsairscan
28.2.0

12 days ago

28.0.0-next.7

12 days ago

28.1.0

12 days ago

28.1.1

12 days ago

28.0.0

12 days ago

28.0.0-next.6

20 days ago

28.0.0-next.5

22 days ago

28.0.0-next.4

26 days ago

28.0.0-next.1

28 days ago

28.0.0-next.3

27 days ago

28.0.0-next.2

28 days ago

27.9.0

2 months ago

27.7.0

2 months ago

27.8.0

2 months ago

27.8.0-next.1

2 months ago

27.6.3

3 months ago

27.6.2

3 months ago

27.6.1

4 months ago

27.5.0

6 months ago

27.4.3

6 months ago

27.6.0

6 months ago

27.4.0

7 months ago

27.4.1

7 months ago

27.4.2

7 months ago

27.3.0

7 months ago

27.2.2

10 months ago

27.2.3

9 months ago

27.1.6

1 year ago

27.1.7

1 year ago

27.1.1

2 years ago

27.1.2

2 years ago

27.1.3

2 years ago

27.1.4

1 year ago

27.1.5

1 year ago

27.2.0

1 year ago

27.2.1

1 year ago

26.9.0

2 years ago

27.0.0-next.2

2 years ago

27.0.0-next.1

2 years ago

26.8.6

2 years ago

26.8.7

2 years ago

26.8.5

2 years ago

27.1.0

2 years ago

27.0.0

2 years ago

27.0.2

2 years ago

27.0.1

2 years ago

27.0.4

2 years ago

27.0.3

2 years ago

26.8.4

2 years ago

26.7.0

2 years ago

26.6.0

2 years ago

26.8.2

2 years ago

26.8.3

2 years ago

26.8.0

2 years ago

26.8.1

2 years ago

26.3.0

2 years ago

26.2.2

2 years ago

26.5.3

2 years ago

26.5.2

2 years ago

26.5.1

2 years ago

26.5.0

2 years ago

26.4.7

2 years ago

26.4.6

2 years ago

26.4.5

2 years ago

26.4.4

2 years ago

26.4.3

2 years ago

26.4.2

2 years ago

26.4.1

2 years ago

26.4.0

2 years ago

26.2.1

2 years ago

26.2.0

2 years ago

26.1.5

2 years ago

26.1.4

2 years ago

26.1.3

2 years ago

26.1.2

2 years ago

26.1.0-next.2

2 years ago

26.1.0-next.1

2 years ago

26.1.1

2 years ago

26.1.0

2 years ago

25.3.3

2 years ago

25.3.2

2 years ago

25.3.4

2 years ago

25.3.1

2 years ago

26.0.0

2 years ago

25.5.0

2 years ago

25.4.0

2 years ago

25.7.0

2 years ago

25.6.0

2 years ago

25.3.0

2 years ago

25.2.4

2 years ago

25.2.3

2 years ago

25.1.0

3 years ago

25.0.6

3 years ago

25.2.0

3 years ago

25.2.2

3 years ago

25.2.1

3 years ago

25.0.5

3 years ago

25.0.2

3 years ago

25.0.1

3 years ago

25.0.4

3 years ago

25.0.3

3 years ago

25.0.0

3 years ago

24.7.0

3 years ago

24.6.0

3 years ago

25.0.0-next.7

3 years ago

25.0.0-next.6

3 years ago

24.5.2

3 years ago

24.5.1

3 years ago

24.5.0

3 years ago

25.0.0-next.5

3 years ago

24.4.3

3 years ago

25.0.0-next.4

3 years ago

24.4.1

3 years ago

24.4.2

3 years ago

25.0.0-next.3

3 years ago

25.0.0-next.1

3 years ago

25.0.0-next.2

3 years ago

24.4.0

3 years ago

24.3.7

3 years ago

24.3.6

3 years ago

24.3.5

3 years ago

24.3.4

3 years ago

24.3.3

3 years ago

24.3.2

3 years ago

24.3.1

3 years ago

24.3.0

3 years ago

24.2.1

3 years ago

24.2.0

3 years ago

24.1.10

3 years ago

24.1.9

3 years ago

24.1.8

3 years ago

24.1.7

3 years ago

24.1.6

3 years ago

24.1.4

3 years ago

24.1.5

3 years ago

24.1.3

3 years ago

24.1.2

3 years ago

24.1.1

3 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

4 years ago

23.17.0

4 years ago

23.16.0

4 years ago

23.15.0

4 years ago

23.14.0

4 years ago

23.13.2

4 years ago

23.13.1

4 years ago

23.12.0

4 years ago

23.13.0

4 years ago

23.11.0

4 years ago

23.10.0

4 years ago

23.9.0

4 years ago

23.8.2

4 years ago

23.8.1

4 years ago

23.8.0

4 years ago

23.7.0

4 years ago

23.5.0

4 years ago

23.6.0

4 years ago

23.4.0

4 years ago

23.3.0

4 years ago

23.2.0

4 years ago

23.1.1

4 years ago

23.1.0

4 years ago

23.0.5

4 years ago

23.0.4

4 years ago

23.0.3

4 years ago

23.0.2

4 years ago

23.0.1

4 years ago

23.0.0

4 years ago

22.21.0

4 years ago

22.20.1

4 years ago

22.20.0

4 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

5 years ago

22.7.0

5 years ago

22.6.4

5 years ago

22.6.3

5 years ago

22.6.2

5 years ago

22.6.1

5 years ago

22.6.0

5 years ago

22.5.1

5 years ago

22.5.0

5 years ago

22.4.1

5 years ago

22.4.0

5 years ago

22.3.2

5 years ago

22.3.1

5 years ago

22.3.0

5 years ago

22.2.2

5 years ago

22.2.1

5 years ago

22.2.0

5 years ago

22.1.3

5 years ago

22.1.2

5 years ago

22.1.1

5 years ago

22.1.0

5 years ago

22.0.1

5 years ago

22.0.0

5 years ago

21.27.2

5 years ago

21.27.1

5 years ago

21.27.0

5 years ago

21.26.2

5 years ago

21.26.1

5 years ago

21.26.0

5 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

6 years ago

21.16.1

6 years ago

21.16.0

6 years ago

21.15.3

6 years ago

21.15.2

6 years ago

21.15.1

6 years ago

21.15.0

6 years ago

21.14.1

6 years ago

21.14.0

6 years ago

21.13.0

6 years ago

21.12.3

6 years ago

21.12.2

6 years ago

21.12.1

6 years ago

21.12.0

6 years ago

21.11.0

6 years ago

21.10.0

6 years ago

21.9.0

6 years ago

21.8.0

6 years ago

21.7.1

6 years ago

21.7.0

6 years ago

21.6.2

6 years ago

21.6.1

6 years ago

21.6.0

6 years ago

21.5.0

6 years ago

21.4.3

6 years ago

21.4.2

6 years ago

21.4.1

6 years ago

21.4.0

6 years ago

21.3.2

6 years ago

21.3.1

6 years ago

21.3.0

6 years ago

21.3.0-beta.8

6 years ago

21.3.0-beta.7

6 years ago

21.3.0-beta.6

6 years ago

21.3.0-beta.5

6 years ago

21.3.0-beta.4

6 years ago

21.3.0-beta.3

6 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

7 years ago

20.0.2

7 years ago

20.0.1

7 years ago

20.0.0

7 years ago

19.0.1

7 years ago

19.0.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago