- Notifications
You must be signed in to change notification settings - Fork243
jest-community/eslint-plugin-jest
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
yarn add --dev eslint eslint-plugin-jest
Note: If you installed ESLint globally then you must also installeslint-plugin-jest globally.
If you're using flat configuration:
Withflat configuration,just import the plugin and away you go:
constpluginJest=require('eslint-plugin-jest');module.exports=[{// update this to match your test filesfiles:['**/*.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, addjest to the plugins section of your.eslintrcconfiguration file. You can omit theeslint-plugin- prefix:
{"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 ourshared configs
You can tell this plugin about any global Jests you have aliased using theglobalAliases setting:
{"settings": {"jest": {"globalAliases": {"describe": ["context"],"fdescribe": ["fcontext"],"xdescribe": ["xcontext"] } } }}You can tell this plugin to treat a different package as the source of Jestglobals using theglobalPackage setting:
{"settings": {"jest": {"globalPackage":"bun:test" } }}Warning
While this can be used to apply rules when using alternative testing librariesand frameworks likebun,vitest andnode, there's no guarantee thesemantics this plugin assumes will hold outside of Jest
The rules provided by this plugin assume that the files they are checking aretest-related. This means it's generally not suitable to include them in yourtop-level configuration as that applies to all files being linted which caninclude source files.
For.eslintrc configs you can useoverridesto 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] }}Foreslint.config.js you can usefiles andignores:
constjest=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 (specificallyno-deprecated-functions) changedepending on the version of Jest being used.
By default, this plugin will attempt to determine to locate Jest usingrequire.resolve, meaning it will start looking in the closestnode_modulesfolder to the file being linted and work its way up.
Since we cache the automatically determined version, if you're lintingsub-folders that have different versions of Jest, you may find that the wrongversion of Jest is considered when linting. You can work around this byproviding 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 versionof Jest if you use a JavaScript config file such as.eslintrc.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 beingfinalized.
This plugin exports a recommended configuration that enforces good testingpractices.
To enable this configuration with.eslintrc, use theextends property:
{"extends": ["plugin:jest/recommended"]}To enable this configuration witheslint.config.js, usejest.configs['flat/recommended']:
constjest=require('eslint-plugin-jest');module.exports=[{files:[/* glob matching your test files */], ...jest.configs['flat/recommended'],},];
This plugin also exports a configuration namedstyle, which adds somestylistic rules, such asprefer-to-be-null, which enforces usage oftoBeNullovertoBe(null).
To enable this configuration use theextends property in your.eslintrcconfig file:
{"extends": ["plugin:jest/style"]}To enable this configuration witheslint.config.js, usejest.configs['flat/style']:
constjest=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 theall configuration to your.eslintrc config file:
{"extends": ["plugin:jest/all"]}To enable this configuration witheslint.config.js, usejest.configs['flat/all']:
constjest=require('eslint-plugin-jest');module.exports=[{files:[/* glob matching your test files */], ...jest.configs['flat/all'],},];
While therecommended andstyle configurations only change in major versionstheall configuration may change in any release and is thus unsuited forinstallations requiring long-term consistency.
💼Configurationsenabled in.
✅ Set in therecommendedconfiguration.
🎨Set in thestyleconfiguration.
🔧Automatically fixable by the--fix CLI option.
💡Manually fixable byeditor suggestions.
| Name | Description | 💼 | 🔧 | 💡 | |
|---|---|---|---|---|---|
| consistent-test-it | Enforcetest andit 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 callingexpect 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 usingexports in files containing tests | ✅ | |||
| 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__mocks__ | ✅ | |||
| no-restricted-jest-methods | Disallow specificjest. methods | ||||
| no-restricted-matchers | Disallow specific matchers & modifiers | ||||
| no-standalone-expect | Disallow usingexpect outside ofit ortest blocks | ✅ | |||
| no-test-prefixes | Require using.only and.skip overf andx | ✅ | 🔧 | ||
| no-test-return-statement | Disallow explicitly returning from tests | ||||
| no-untyped-mock-factory | Disallow usingjest.mock() factories without an explicit type parameter | 🔧 | |||
| padding-around-after-all-blocks | Enforce padding aroundafterAll blocks | 🔧 | |||
| padding-around-after-each-blocks | Enforce padding aroundafterEach blocks | 🔧 | |||
| padding-around-all | Enforce padding around Jest functions | 🔧 | |||
| padding-around-before-all-blocks | Enforce padding aroundbeforeAll blocks | 🔧 | |||
| padding-around-before-each-blocks | Enforce padding aroundbeforeEach blocks | 🔧 | |||
| padding-around-describe-blocks | Enforce padding arounddescribe blocks | 🔧 | |||
| padding-around-expect-groups | Enforce padding aroundexpect groups | 🔧 | |||
| padding-around-test-blocks | Enforce padding aroundtest andit blocks | 🔧 | |||
| prefer-called-with | Suggest usingtoBeCalledWith() ortoHaveBeenCalledWith() | ||||
| prefer-comparison-matcher | Suggest using the built-in comparison matchers | 🔧 | |||
| prefer-each | Prefer using.each rather than manual loops | ||||
| 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 usingexpect.assertions() ORexpect.hasAssertions() | 💡 | |||
| prefer-expect-resolves | Preferawait expect(...).resolves overexpect(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-importing-jest-globals | Prefer importing Jest globals | 🔧 | |||
| prefer-jest-mocked | Preferjest.mocked() overfn as jest.Mock | 🔧 | |||
| 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 usingjest.spyOn() | 🔧 | |||
| prefer-strict-equal | Suggest usingtoStrictEqual() | 💡 | |||
| prefer-to-be | Suggest usingtoBe() for primitive literals | 🎨 | 🔧 | ||
| prefer-to-contain | Suggest usingtoContain() | 🎨 | 🔧 | ||
| prefer-to-have-length | Suggest usingtoHaveLength() | 🎨 | 🔧 | ||
| prefer-todo | Suggest usingtest.todo | 🔧 | |||
| require-hook | Require setup and teardown code to be within a hook | ||||
| require-to-throw-message | Require a message fortoThrow() | ||||
| require-top-level-describe | Require test cases and hooks to be inside adescribe block | ||||
| valid-describe-callback | Enforce validdescribe() callback | ✅ | |||
| valid-expect | Enforce validexpect() usage | ✅ | 🔧 | ||
| valid-expect-in-promise | Require promises that have expectations in their chain to be valid | ✅ | |||
| valid-title | Enforce valid titles | ✅ | 🔧 |
| 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 outlinedhere.
Note that unlike the type-checking rules in@typescript-eslint/eslint-plugin,the rules here will fallback to doing nothing if type information is notavailable, meaning it's safe to include them in shared configs that could beused on JavaScript and TypeScript projects.
Also note thatunbound-method depends on@typescript-eslint/eslint-plugin,as it extends the originalunbound-method rule from that plugin.
This is a sister plugin toeslint-plugin-jest that provides support for thematchers provided byjest-extended.
https://github.com/jest-community/eslint-plugin-jest-extended
This project aims to provide formatting rules (auto-fixable where possible) toensure consistency and readability in jest test suites.
https://github.com/dangreenisrael/eslint-plugin-jest-formatting
A set of rules to enforce good practices for Istanbul, one of the code coveragetools used by Jest.
About
ESLint plugin for Jest
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
