ESLint rules for mocha.
This plugin requires ESLint 9.0.0 or later.
npm install --save-dev eslint-plugin-mochaTo use this plugin with eslint flat configuration format:
import mochaPlugin from "eslint-plugin-mocha";
export default [
mochaPlugin.configs.recommended, // or `mochaPlugin.configs.all` to enable all
// ... Your configurations here
];This plugin supports the following settings, which are used by multiple rules:
-
additionalCustomNames: This allows rules to check additional function names when looking for suites or test cases. This might be used with a custom Mocha extension, such asember-mochaormocha-each.Example:
{ "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" } ] } }The
nameproperty can be in any of the following forms:-
A plain name e.g.
describeModule, which allows:describeModule("example", function() { ... });
-
A dotted name, e.g.
describe.modifier, which allows:describe.modifier("example", function() { ... });
-
A name with parentheses, e.g.
forEach().describe, which allows:forEach([ 1, 2, 3 ]) .describe("example", function(n) { ... });
-
Any combination of the above, e.g.
forEach().describeModule.modifier, which allows:forEach([ 1, 2, 3 ]) .describeModule.modifier("example", function(n) { ... });
-
-
interface: This allows to select eitherTDD,BDD(default) orexports. When usingexportsmocha variables are resolved from namedimportstatements instead of global variables.
💼 Configurations enabled in.
🚫 Configurations disabled in.
✅ Set in the recommended configuration.
🔧 Automatically fixable by the --fix CLI option.
| Name | Description | 💼 | 🚫 | 🔧 | |
|---|---|---|---|---|---|
| consistent-interface | Enforces consistent use of mocha interfaces | ||||
| consistent-spacing-between-blocks | Require consistent spacing between blocks | ✅ | 🔧 | ||
| handle-done-callback | Enforces handling of callbacks for async tests | ✅ | |||
| max-top-level-suites | Enforce the number of top-level suites in a single file | ✅ | |||
| no-async-suite | Disallow async functions passed to a suite | ✅ | 🔧 | ||
| no-empty-title | Disallow empty test descriptions | ✅ | |||
| no-exclusive-tests | Disallow exclusive tests | ✅ | |||
| no-exports | Disallow exports from test files | ✅ | |||
| no-global-tests | Disallow global tests | ✅ | |||
| no-hooks | Disallow hooks | ✅ | |||
| no-hooks-for-single-case | Disallow hooks for a single test or test suite | ✅ | |||
| no-identical-title | Disallow identical titles | ✅ | |||
| no-mocha-arrows | Disallow arrow functions as arguments to mocha functions | ✅ | 🔧 | ||
| no-nested-tests | Disallow tests to be nested within other tests | ✅ | |||
| no-pending-tests | Disallow pending tests | ✅ | |||
| no-return-and-callback | 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-setup-in-describe | Disallow setup in describe blocks | ✅ | |||
| no-sibling-hooks | Disallow duplicate uses of a hook at the same level inside a suite | ✅ | |||
| no-synchronous-tests | Disallow synchronous tests | ✅ | |||
| no-top-level-hooks | Disallow top-level hooks | ✅ | |||
| 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 | ✅ |