ESLint Plugin Mocha is a comprehensive ESLint plugin that provides 24 rules specifically designed for projects using the Mocha testing framework. It enforces best practices, catches common mistakes, prevents testing anti-patterns, and improves test code quality and maintainability.
npm install --save-dev eslint-plugin-mocha// ESM import
import mochaPlugin from 'eslint-plugin-mocha';
// CommonJS require
const mochaPlugin = require('eslint-plugin-mocha');{
"plugins": ["mocha"],
"extends": ["plugin:mocha/recommended"],
"env": {
"mocha": true
}
}import mochaPlugin from 'eslint-plugin-mocha';
export default [
mochaPlugin.configs.flat.recommended,
// ... your other configurations
];{
"plugins": ["mocha"],
"rules": {
"mocha/no-exclusive-tests": "error",
"mocha/no-skipped-tests": "warn",
"mocha/max-top-level-suites": ["error", { "limit": 1 }]
}
}ESLint Plugin Mocha is structured around several key components:
recommended and all) for quick setupPre-configured rule sets for different use cases, supporting both legacy and flat ESLint configurations.
interface PluginExport {
rules: { [ruleName: string]: ESLintRule };
configs: {
recommended: LegacyConfig;
all: LegacyConfig;
flat: {
recommended: FlatConfig;
all: FlatConfig;
};
};
}
interface LegacyConfig {
env: { mocha: boolean };
plugins: string[];
rules: { [ruleKey: string]: string | [string, any] };
}
interface FlatConfig {
name: string;
plugins: { mocha: PluginExport };
languageOptions: { globals: object };
rules: { [ruleKey: string]: string | [string, any] };
}Rules that enforce test code quality, readability, and maintainability practices.
// Key quality rules
const qualityRules = {
'mocha/no-identical-title': ESLintRule;
'mocha/no-empty-description': ESLintRule;
'mocha/valid-test-description': ESLintRule;
'mocha/valid-suite-description': ESLintRule;
'mocha/consistent-spacing-between-blocks': ESLintRule;
'mocha/max-top-level-suites': ESLintRule;
};Rules for proper handling of asynchronous tests, callbacks, and promises.
// Key async rules
const asyncRules = {
'mocha/handle-done-callback': ESLintRule;
'mocha/no-return-and-callback': ESLintRule;
'mocha/no-return-from-async': ESLintRule;
'mocha/no-synchronous-tests': ESLintRule;
};Rules that enforce proper test organization, structure, and prevent anti-patterns.
// Key structure rules
const structureRules = {
'mocha/no-nested-tests': ESLintRule;
'mocha/no-setup-in-describe': ESLintRule;
'mocha/no-hooks-for-single-case': ESLintRule;
'mocha/no-sibling-hooks': ESLintRule;
'mocha/no-top-level-hooks': ESLintRule;
'mocha/no-hooks': ESLintRule;
};Rules that prevent problematic test execution patterns and enforce development workflow practices.
// Key execution rules
const executionRules = {
'mocha/no-exclusive-tests': ESLintRule;
'mocha/no-skipped-tests': ESLintRule;
'mocha/no-pending-tests': ESLintRule;
'mocha/no-global-tests': ESLintRule;
'mocha/no-exports': ESLintRule;
};Rules for consistent function style and arrow function usage in tests.
// Key style rules
const styleRules = {
'mocha/no-mocha-arrows': ESLintRule;
'mocha/prefer-arrow-callback': ESLintRule;
'mocha/no-async-describe': ESLintRule;
};Configuration options for extending the plugin's detection capabilities with custom test function names.
interface PluginSettings {
'mocha/additionalCustomNames'?: CustomName[];
}
interface CustomName {
name: string;
type: 'suite' | 'testCase' | 'hook';
interfaces: Array<'BDD' | 'TDD' | 'QUnit'>;
}interface ESLintRule {
meta: {
type: 'problem' | 'suggestion' | 'layout';
docs: {
description: string;
url: string;
};
schema: any[];
fixable?: 'code' | 'whitespace';
};
create: (context: ESLintContext) => ESLintVisitor;
}
interface ESLintContext {
options: any[];
settings: { [key: string]: any };
getSourceCode(): SourceCode;
report(descriptor: ReportDescriptor): void;
}
interface ESLintVisitor {
[nodeType: string]: (node: any) => void;
}