ESLint rules for Mocha.
This plugin targets Mocha's current JavaScript interfaces documented for Mocha 11. It does not execute Mocha or require Mocha at runtime.
This plugin requires ESLint 10.2.0 or later.
npm install --save-dev eslint-plugin-mocha
Use the plugin with ESLint's flat config. Apply it only to Mocha test files, and adjust files to match your project:
import mochaPlugin from 'eslint-plugin-mocha';
export default [
{
files: [ 'test/**/*.js' ],
...mochaPlugin.configs.recommended
}
];
mochaPlugin.configs.recommended: Practical defaults for most projects.mochaPlugin.configs.all: Enables every rule. This config may change when new
rules are added, so it is better suited for trying the full rule set than
for stable long-term lint output.import mochaPlugin from 'eslint-plugin-mocha';
export default [
{
files: [ 'test/**/*.js' ],
...mochaPlugin.configs.all
}
];
These settings are shared by multiple rules.
additionalCustomNames: Adds custom suite, test, or hook function names. This is useful for Mocha wrappers such as ember-mocha, mocha-each, or project-specific helpers that wrap setup and teardown. Use interface: "require" for wrappers that expose named imports from a helper module instead of importing directly from mocha.{
"rules": {
"mocha/no-pending-tests": "error",
"mocha/no-exclusive-tests": "error"
},
"settings": {
"mocha/additionalCustomNames": [
{
"name": "describeModule",
"type": "suite",
"interface": "BDD"
},
{
"name": "testModule",
"type": "testCase",
"interface": "TDD"
},
{
"name": "prepareTestContexts",
"type": "hook",
"interface": "BDD"
}
]
}
}
The name field supports these forms:
describeModule:describeModule('example', function () {});
describe.modifier:describe.modifier('example', function () {});
forEach().describe:forEach([ 1, 2, 3 ]).describe('example', function (n) {});
forEach().describeModule.modifier:forEach([ 1, 2, 3 ]).describeModule.modifier('example', function (n) {});
type: Selects suite, testCase, or hook.interface: Selects BDD, TDD, or require. The default is BDD. With require, rule resolution uses named import statements instead of globals. mocha/consistent-interface also reports named imports of Mocha interface methods when this setting is BDD or TDD, which helps catch accidental require-style usage and interface misconfiguration earlier.The plugin supports Mocha's BDD, TDD, and Require interfaces. It does not support Mocha's Exports or QUnit interfaces, or third-party UIs with different syntax. Many rules depend on suite, test, and hook calls being represented as nested call expressions, which those interfaces do not provide consistently.
For wrapper APIs that still expose suite, test, or hook call functions, use additionalCustomNames.
For maintainers: the rules table below is generated, and the headers in documentation/rules/*.md are partly generated. Refresh them with npx just update-eslint-docs. Run mutation testing with npx just test-mutation.
πΌ Configurations enabled in.
β οΈ Configurations set to warn in.
π« Configurations disabled in.
β
Set in the recommended configuration.
π§ Automatically fixable by the --fix CLI option.
π‘ Manually fixable by editor suggestions.
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π« | π§ | π‘ |
|---|---|---|---|---|---|---|
| consistent-interface | Enforces consistent use of mocha interfaces | β | π§ | |||
| consistent-spacing-between-blocks | Require consistent spacing between blocks | β | π§ | |||
| consistent-structure | Require consistent structure for Mocha test entities | β | ||||
| handle-done-callback | Enforces handling of callbacks for async tests in every branch | β | ||||
| limit-retries | Enforce limits for Mocha retries | β | ||||
| limit-slow | Enforce limits for Mocha slow thresholds | β | ||||
| limit-timeout | Enforce limits for Mocha timeouts | β | ||||
| max-top-level-suites | Enforce the number of top-level suites in a single file | β | ||||
| no-async-and-done | Disallow async functions that also use a Mocha callback | β | ||||
| no-async-in-sync-tests | Disallow async operations in synchronous tests or hooks | β | ||||
| no-async-suite | Disallow async functions passed to a suite | β | π§ | |||
| no-code-after-done | Disallow executing code after calling a Mocha callback | β | ||||
| no-conditional-tests | Disallow conditional suite and test declarations | β | ||||
| no-done-twice | Disallow calling a Mocha callback more than once | β | ||||
| no-empty-title | Disallow empty suite and test descriptions | β | ||||
| no-exclusive-tests | Disallow exclusive tests | β | π‘ | |||
| no-exports | Disallow exports from test files | β | π‘ | |||
| no-hooks | Disallow hooks | β | ||||
| no-hooks-for-single-child | Disallow hooks with a single direct child | β | ||||
| no-identical-title | Disallow identical titles | β | ||||
| no-mocha-arrows | Disallow arrow functions as arguments to mocha functions | β | π§ | |||
| no-nested-suites | Disallow suites to be nested within other suites | β | ||||
| no-nested-tests | Disallow tests to be nested within other tests | β | ||||
| no-pending-tests | Disallow pending tests | β | π‘ | |||
| no-return-and-done | Disallow returning in a test or hook function that uses a callback | β | ||||
| no-return-from-async | Disallow returning from an async test or hook | β | ||||
| no-root-hooks | Disallow root hooks | β | ||||
| no-setup-in-suite | Disallow setup in suite blocks | β | ||||
| no-synchronous-tests | Disallow synchronous tests | β | ||||
| no-top-level-tests | Disallow top-level tests | β | ||||
| prefer-arrow-callback | Require using arrow functions for callbacks | β | π§ | |||
| valid-suite-title | Require suite descriptions to match a pre-configured regular expression | β | ||||
| valid-test-title | Require test descriptions to match a pre-configured regular expression | β |
(top 30 of 53)
TypeScript
98.6%
JavaScript
1.2%
ESLint rules for Mocha.
This plugin targets Mocha's current JavaScript interfaces documented for Mocha 11. It does not execute Mocha or require Mocha at runtime.
This plugin requires ESLint 10.2.0 or later.
npm install --save-dev eslint-plugin-mocha
Use the plugin with ESLint's flat config. Apply it only to Mocha test files, and adjust files to match your project:
import mochaPlugin from 'eslint-plugin-mocha';
export default [
{
files: [ 'test/**/*.js' ],
...mochaPlugin.configs.recommended
}
];
mochaPlugin.configs.recommended: Practical defaults for most projects.mochaPlugin.configs.all: Enables every rule. This config may change when new
rules are added, so it is better suited for trying the full rule set than
for stable long-term lint output.import mochaPlugin from 'eslint-plugin-mocha';
export default [
{
files: [ 'test/**/*.js' ],
...mochaPlugin.configs.all
}
];
These settings are shared by multiple rules.
additionalCustomNames: Adds custom suite, test, or hook function names. This is useful for Mocha wrappers such as ember-mocha, mocha-each, or project-specific helpers that wrap setup and teardown. Use interface: "require" for wrappers that expose named imports from a helper module instead of importing directly from mocha.{
"rules": {
"mocha/no-pending-tests": "error",
"mocha/no-exclusive-tests": "error"
},
"settings": {
"mocha/additionalCustomNames": [
{
"name": "describeModule",
"type": "suite",
"interface": "BDD"
},
{
"name": "testModule",
"type": "testCase",
"interface": "TDD"
},
{
"name": "prepareTestContexts",
"type": "hook",
"interface": "BDD"
}
]
}
}
The name field supports these forms:
describeModule:describeModule('example', function () {});
describe.modifier:describe.modifier('example', function () {});
forEach().describe:forEach([ 1, 2, 3 ]).describe('example', function (n) {});
forEach().describeModule.modifier:forEach([ 1, 2, 3 ]).describeModule.modifier('example', function (n) {});
type: Selects suite, testCase, or hook.interface: Selects BDD, TDD, or require. The default is BDD. With require, rule resolution uses named import statements instead of globals. mocha/consistent-interface also reports named imports of Mocha interface methods when this setting is BDD or TDD, which helps catch accidental require-style usage and interface misconfiguration earlier.The plugin supports Mocha's BDD, TDD, and Require interfaces. It does not support Mocha's Exports or QUnit interfaces, or third-party UIs with different syntax. Many rules depend on suite, test, and hook calls being represented as nested call expressions, which those interfaces do not provide consistently.
For wrapper APIs that still expose suite, test, or hook call functions, use additionalCustomNames.
For maintainers: the rules table below is generated, and the headers in documentation/rules/*.md are partly generated. Refresh them with npx just update-eslint-docs. Run mutation testing with npx just test-mutation.
πΌ Configurations enabled in.
β οΈ Configurations set to warn in.
π« Configurations disabled in.
β
Set in the recommended configuration.
π§ Automatically fixable by the --fix CLI option.
π‘ Manually fixable by editor suggestions.
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π« | π§ | π‘ |
|---|---|---|---|---|---|---|
| consistent-interface | Enforces consistent use of mocha interfaces | β | π§ | |||
| consistent-spacing-between-blocks | Require consistent spacing between blocks | β | π§ | |||
| consistent-structure | Require consistent structure for Mocha test entities | β | ||||
| handle-done-callback | Enforces handling of callbacks for async tests in every branch | β | ||||
| limit-retries | Enforce limits for Mocha retries | β | ||||
| limit-slow | Enforce limits for Mocha slow thresholds | β | ||||
| limit-timeout | Enforce limits for Mocha timeouts | β | ||||
| max-top-level-suites | Enforce the number of top-level suites in a single file | β | ||||
| no-async-and-done | Disallow async functions that also use a Mocha callback | β | ||||
| no-async-in-sync-tests | Disallow async operations in synchronous tests or hooks | β | ||||
| no-async-suite | Disallow async functions passed to a suite | β | π§ | |||
| no-code-after-done | Disallow executing code after calling a Mocha callback | β | ||||
| no-conditional-tests | Disallow conditional suite and test declarations | β | ||||
| no-done-twice | Disallow calling a Mocha callback more than once | β | ||||
| no-empty-title | Disallow empty suite and test descriptions | β | ||||
| no-exclusive-tests | Disallow exclusive tests | β | π‘ | |||
| no-exports | Disallow exports from test files | β | π‘ | |||
| no-hooks | Disallow hooks | β | ||||
| no-hooks-for-single-child | Disallow hooks with a single direct child | β | ||||
| no-identical-title | Disallow identical titles | β | ||||
| no-mocha-arrows | Disallow arrow functions as arguments to mocha functions | β | π§ | |||
| no-nested-suites | Disallow suites to be nested within other suites | β | ||||
| no-nested-tests | Disallow tests to be nested within other tests | β | ||||
| no-pending-tests | Disallow pending tests | β | π‘ | |||
| no-return-and-done | Disallow returning in a test or hook function that uses a callback | β | ||||
| no-return-from-async | Disallow returning from an async test or hook | β | ||||
| no-root-hooks | Disallow root hooks | β | ||||
| no-setup-in-suite | Disallow setup in suite blocks | β | ||||
| no-synchronous-tests | Disallow synchronous tests | β | ||||
| no-top-level-tests | Disallow top-level tests | β | ||||
| prefer-arrow-callback | Require using arrow functions for callbacks | β | π§ | |||
| valid-suite-title | Require suite descriptions to match a pre-configured regular expression | β | ||||
| valid-test-title | Require test descriptions to match a pre-configured regular expression | β |
(top 30 of 53)
TypeScript
98.6%
JavaScript
1.2%