Config for projects using Jest for testing.
-
If you haven't already, make sure to install
@code-pushup/eslint-config
and its required peer dependencies. -
Since this plugin requires additional peer dependencies, you have to install them as well:
npm install -D eslint-plugin-jest
-
Add to your
eslint.config.js
file:import jest from '@code-pushup/eslint-config/jest.js'; import tseslint from 'typescript-eslint'; export default tseslint.config( ...jest, { // customize rules if needed: rules: { // e.g. to customize `test` or `it` usage (default is `it` in `describe` and `test` at top-level): 'jest/consistent-test-it': ['warn', { fn: 'test', withinDescribe: 'test' }] } } );
🔧 Automatically fixable by the
--fix
CLI option.
💡 Manually fixable by editor suggestions.
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
no-identical-title Disallow identical titles |
||||
no-interpolation-in-snapshots Disallow string interpolation inside snapshots |
||||
no-jasmine-globals Disallow Jasmine globals |
🔧 | |||
no-test-prefixes Require using .only and .skip over f and x |
🔧 | |||
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 |
🔧 |
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
consistent-test-it Enforce test and it usage conventions |
🔧 | |||
expect-expect Enforce assertion to be made in a test body |
||||
max-nested-describe Enforces a maximum depth to nested describe calls |
max: 2{
"max": 2
} |
|||
no-alias-methods Disallow alias methods |
🔧 | |||
no-commented-out-tests Disallow commented out tests |
||||
no-conditional-expect Disallow calling expect conditionally |
||||
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-mocks-import Disallow manually importing from __mocks__ |
||||
no-standalone-expect Disallow using expect outside of it or test blocks |
||||
no-test-return-statement Disallow explicitly returning from tests |
||||
padding-around-after-all-blocks Enforce padding around afterAll blocks |
🔧 | |||
padding-around-after-each-blocks Enforce padding around afterEach blocks |
🔧 | |||
padding-around-before-all-blocks Enforce padding around beforeAll blocks |
🔧 | |||
padding-around-before-each-blocks Enforce padding around beforeEach blocks |
🔧 | |||
padding-around-describe-blocks Enforce padding around describe blocks |
🔧 | |||
padding-around-test-blocks Enforce padding around afterAll blocks |
🔧 | |||
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-resolves Prefer await expect(...).resolves over expect(await ...) syntax |
🔧 | |||
prefer-hooks-on-top Suggest having hooks before any test cases |
||||
prefer-mock-promise-shorthand Prefer mock resolved/rejected shorthands for promises |
🔧 | |||
prefer-spy-on Suggest using jest.spyOn() |
🔧 | |||
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 |