or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async-rules.mdconfigurations.mdexecution-rules.mdindex.mdquality-rules.mdsettings.mdstructure-rules.mdstyle-rules.md
tile.json

tessl/npm-eslint-plugin-mocha

ESLint plugin providing 24 rules for Mocha testing framework best practices and error detection.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/eslint-plugin-mocha@10.5.x

To install, run

npx @tessl/cli install tessl/npm-eslint-plugin-mocha@10.5.0

index.mddocs/

ESLint Plugin Mocha

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.

Package Information

  • Package Name: eslint-plugin-mocha
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev eslint-plugin-mocha

Core Imports

// ESM import
import mochaPlugin from 'eslint-plugin-mocha';

// CommonJS require
const mochaPlugin = require('eslint-plugin-mocha');

Basic Usage

Legacy ESLint Configuration (.eslintrc.json)

{
  "plugins": ["mocha"],
  "extends": ["plugin:mocha/recommended"],
  "env": {
    "mocha": true
  }
}

Flat Configuration (eslint.config.js)

import mochaPlugin from 'eslint-plugin-mocha';

export default [
  mochaPlugin.configs.flat.recommended,
  // ... your other configurations
];

Custom Rule Configuration

{
  "plugins": ["mocha"],
  "rules": {
    "mocha/no-exclusive-tests": "error",
    "mocha/no-skipped-tests": "warn",
    "mocha/max-top-level-suites": ["error", { "limit": 1 }]
  }
}

Architecture

ESLint Plugin Mocha is structured around several key components:

  • Rules System: 24 individual ESLint rules targeting specific Mocha patterns and anti-patterns
  • Configuration Presets: Pre-configured rule sets (recommended and all) for quick setup
  • AST Utilities: Sophisticated AST analysis tools for detecting Mocha function calls and patterns
  • Name Resolution: Dynamic detection of test functions, suites, and hooks across different Mocha interfaces (BDD, TDD, QUnit)
  • Settings Support: Extensible configuration allowing custom test function names and patterns

Capabilities

Rule Configurations

Pre-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] };
}

Rule Configurations

Test Quality Rules

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;
};

Test Quality Rules

Async Testing Rules

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;
};

Async Testing Rules

Test Structure Rules

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;
};

Test Structure Rules

Test Execution Rules

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;
};

Test Execution Rules

Function Style Rules

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;
};

Function Style Rules

Plugin Settings

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'>;
}

Plugin Settings

Types

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;
}