Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-mocha

lo1tuma3.4mMIT10.5.0TypeScript support: definitely-typed

Eslint rules for mocha.

eslint, eslintplugin, eslint-plugin, mocha

readme

NPM Version GitHub Actions status Coverage Status NPM Downloads

eslint-plugin-mocha

ESLint rules for mocha.

Install and configure

This plugin requires ESLint 4.0.0 or later.

npm install --save-dev eslint-plugin-mocha

.eslintrc.json

Then add a reference to this plugin and selected rules in your eslint config:

{
    "plugins": [
        "mocha"
    ]
}

eslint.config.js (requires eslint >= 8.23.0)

To use this plugin with the new eslint configuration format (flat config):

import mochaPlugin from 'eslint-plugin-mocha';

export default [
    mochaPlugin.configs.flat.recommended // or `mochaPlugin.configs.flat.all` to enable all
    // ... Your configurations here
];

Plugin Settings

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 as ember-mocha or mocha-each.

    Example:

      {
          "rules": {
              "mocha/no-skipped-tests": "error",
              "mocha/no-exclusive-tests": "error"
          },
          "settings": {
              "mocha/additionalCustomNames": [
                  { "name": "describeModule", "type": "suite", "interfaces": [ "BDD" ] },
                  { "name": "testModule", "type": "testCase", "interfaces": [ "TDD" ] }
              ]
          }
      }

    The name property 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) { ... });

Configs

This plugin exports a recommended config that enforces good practices.

Enable it with the extends option:

{
    "extends": [
        "plugin:mocha/recommended"
    ]
}

all

There's also a configuration that enables all of our rules.

See Configuring Eslint on eslint.org for more info.

Rules

💼 Configurations enabled in.\ ⚠️ Configurations set to warn in.\ 🚫 Configurations disabled in.\ ✅ Set in the recommended configuration.\ 🔧 Automatically fixable by the --fix CLI option.

Name Description 💼 ⚠️ 🚫 🔧
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-describe Disallow async functions passed to describe | 🔧
no-empty-description 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 describe |
no-skipped-tests Disallow skipped tests | ✅ |
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-description Require suite descriptions to match a pre-configured regular expression |
valid-test-description Require test descriptions to match a pre-configured regular expression |

changelog

10.5.0 (July 29, 2024)

Enhancements

  • Add name to flat configs (#356)

10.4.3 (April 19, 2024)

Bug Fixes

  • Fix support for ESLint versions < 8.40.0

10.4.2 (April 10, 2024)

Bug Fixes

  • Stop using deprecated ESLint context methods

10.4.1 (March 8, 2024)

Bug Fixes

  • Downgrade globals package to support node versions < v18

10.4.0 (March 7, 2024)

Features

  • Add support for eslint flat config format (#349)

10.3.0 (February 16, 2024)

Features

  • Add consistent-spacing-between-blocks rule (#340)

Documentation

  • Add screendriver to contributors (#339)

10.2.0 (September 25, 2023)

Features

  • Support function calls as custom names (to stop false warnings) (#336)

Documentation

  • Automate docs with eslint-doc-generator (#330)

Dependency Upgrades

  • Update all dependencies (#334)
  • Include Node.js v20 in continuous integration pipeline (#338)
  • Introduce "release-it" to make releases more comfortable (#337)

10.1.0 (July 20, 2022)

Enhancements

  • Support this for additionalCustomNames (#329)

10.0.5 (May 27, 2022)

Bug Fixes

  • Support Node 18 + switch from ramda to rambda (#326)

Code Refactoring

  • Add unit tests to verify the correct reference is analyzed for shadowing (#328)

10.0.4 (April 11, 2022)

Documentation

  • Document recommended error level (#317)

Dependency Upgrades

  • Bump package versions due to vulnerability issues (#320)

10.0.3 (December 14, 2021)

Bug Fixes

  • Allow dynamic expressions as description argument in no-empty-description (#316)

10.0.2 (December 13, 2021)

Bug Fixes

  • Fix no-empty-description rule to not report on dynamic values (#315)

Code Refactoring

  • chore: switch yarn command to npm (#309)

10.0.1 (December 8, 2021)

Bug Fixes

  • Add mocha prefix to "no-empty-description" rule name in rec config (#310)
  • chore: use npm-run-all to run lint/test scripts (#308)

10.0.0 (December 8, 2021)

Breaking Changes

  • Turn off no-hooks-for-single-case in recommended (#307)
  • Drop support for node v12 (#299)

Features

  • Add no-empty-description rule. closes #302 (#304)

Enhancements

  • Add rule documentation URLs (and eslint-plugin-eslint-plugin) (#294)

Documentation

  • Remove unavailable badge from README (#305)

Dependency Upgrades

  • Update devDependencies (#301)

Code Refactoring

  • Remove eslint-config-holidaycheck dev dependency (#300)
  • Add markdownlint and fix linting issues (#306)

9.0.0 (May 26, 2021)

Breaking Changes

  • Drop support for node v10 (#285)

Bug Fixes

  • Fix false positive in no-setup-in-describe (#293)

Features

  • Add "all" config preset which enables all rules (#281)

Enhancements

  • Improve no-skipped performance (#292)
  • Improve no-hooks-for-single-case performance (#291)
  • Improve no-nested-tests performance (#290)
  • Improve performance of no-identical-title (#289)
  • Improve no-setup-in-describe performance (#287)
  • Improve no-mocha-arrows performance (#288)
  • Improve performance of no-exports rule (#286)
  • Enable all rules during runtime benchmark (#282)

Dependency Upgrades

  • Update dependencies (#284)
  • Add node v16 to CI environments (#283)

8.2.0 (May 25, 2021)

Enhancements

  • Memoize names (increases speed by 10x) (#280)

Code Refactoring

    • Avoid passing superfluous argument (#278)

8.1.0 (March 5, 2021)

Bug Fixes

  • Fix prefer-arrow-callback to not fail when using import.meta (#266)

Enhancements

  • Speed up no-exclusive-tests and no-pending-tests (#276)
  • set env.mocha on recommended config (#273)
  • Add benchmarks for runtime with many files (#269)

Documentation

  • Remove stray linebreak at top of file (#272)
  • Add recommended tick to no-exports (#271)
  • Improve benchmark test reliability (#277)

8.0.0 (August 6, 2020)

Breaking Changes

  • Consistently use shared settings in all rules (#262)
  • remove autofix from no-skipped-tests rule fix (#258)

Features

  • New rule no-exports (#263)

Enhancements

  • New option ignoreSkipped for handle-done-callback rule (#260)

Documentation

  • Add meta.docs.description to all rules (#257)

Dependency Upgrades

  • Update dependencies (#259)

Code Refactoring

  • Use includes instead of indexOf (#261)

7.0.1 (May 30, 2020)

Bug Fixes

  • Fix no-setup-in-describe to not flag describe.skip() (#256)
  • Fix max-top-level-suites to work with ES modules (#255)
  • Support comments in arrow functions when fixing (#253)

7.0.0 (May 13, 2020)

Breaking Changes

  • Drop support for ESLint < v7 and Node.js < v10 (#247)

Enhancements

  • Add meta.type and missing fixable: 'code' (#245)

Dependency Upgrades

  • Updates of devDependencies (#246)

6.3.0 (February 19, 2020)

Bug Fixes

  • no-hooks-for-single-case: fix false postive in nested suites (#238)
  • Fix max-top-level-suites to ignore generated suites (#239)
  • Check static template strings in valid-test-description and valid-suite-description (#237)

Enhancements

  • no-hooks: add option to allow certain kind of hooks (#236)
  • Add schemas for options (and remove for files which are using settings) (#234)
  • Add u flag in RegExp for valid-test-description and valid-suite-description (#232)
  • Add fixable property to fixable rules (and mention in docs) (#228)
  • add plugin to recommended config (#226)

Documentation

  • Indicate whether rule is recommended (#229)
  • "Options" heading in doc files (and consistent level 2 heading) (#233)
  • Doc syntax issue (#231)

Code Refactoring

  • Nondeprecated rule format (#235)
  • Alphabetize rule lists (#227)
    • npm: Add package-lock.json (#230)
  • ✉️ Send a webhook to Coveralls when the build completes (#222)

6.2.2 (November 22, 2019)

Bug Fixes

  • Support TDD interface in no-setup-in-describe (#220)
  • 🚀 Use GitHub Actions instead of Travis (#221)

6.2.1 (October 28, 2019)

Bug Fixes

  • Fix no-setup-in-describe to allow mocha config calls (#215)
  • Relax no-synchronous-tests to allow non literals from concise arrows (#216)

Documentation

  • Minor documentation tweaks (#217)

Dependency Upgrades

  • Update devDependencies (#218)

6.2.0 (October 14, 2019)

Enhancements

  • Startup Performance Optimization (#214)

6.1.1 (September 11, 2019)

Bug Fixes

  • Fix no-setup-in-describe to allow Mocha suite config (#209)

6.1.0 (August 22, 2019)

Enhancements

  • Add custom message for valid-suite-description (#207)
  • Add custom message for valid-test-description rule (#206)

6.0.0 (July 17, 2019)

Breaking Changes

  • Revamped recommended ruleset (#200)
  • Drop nodejs 6 support (#197)

Bug Fixes

  • Fix no-setup-in-describe to correctly detect describe calls (#196)
  • Fix no-setup-in-describe to work with arrow functions (#195)

Features

  • Implement no-return-from-async rule (#190)

Dependency Upgrades

  • Update dev dependencies (#199)
  • Add nodejs to travis build environments (#198)

5.3.0 (February 13, 2019)

Features

  • Implement no-async-describe rule (#188)

5.2.1 (January 8, 2019)

Bug Fixes

  • Remove invalid test-cases and unreachable code from prefer-arrow-callback (#186)
  • Fix invalid syntax in test case (#182)

Documentation

  • Fixing typo (#184)
  • Replace warning with warn (#181)

Dependency Upgrades

  • Update dependencies (#187)
  • Update eslint-plugin-node to the latest version 🚀 (#173)

5.2.0 (August 13, 2018)

Enhancements

  • Prohibit tests in beforeEach etc. hook calls (#174)

5.1.0 (July 6, 2018)

Bug Fixes

  • Issue #166: No setup in describe hooks (#167)

Features

  • Adds mocha-aware prefer-arrow-callback rule (#163)

Dependency Upgrades

  • Update eslint-config-holidaycheck to version 0.13.1 (#170)
  • Update eslint to version 5.0.1 (#169)
  • Update nyc to version 12.0.2 (#168)
  • Add node 10 build environment (#171)

5.0.0 (March 24, 2018)

Breaking Changes

  • Remove support for ESLint versions < 4.0.0 (#155)
  • Remove support for nodejs 4, 5 and 7 (#154)

Dependency Upgrades

  • Update pr-log to the latest version 🚀 (#159)
  • Update chai to version 4.1.2 (#151)

Code Refactoring

  • Use new language features (#156)
  • Use nyc instead of istanbul (#153)
  • Whitelist files instead of using .npmignore (#152)

4.12.1 (March 3, 2018)

Bug Fixes

  • Fix: skip template strings in valid-test-description and valid-suite-description

4.12.0 (March 2, 2018)

Features

  • Adds rule "no setup in describe" (#147)

Dependency Upgrades

  • Update coveralls to the latest version 🚀 (#142)
  • chore(package): update mocha to version 5.0.1 (#150)
  • Update ramda to the latest version 🚀 (#144)

Bug Fixes

  • Fix complexity problems (#149)

4.11.0 (June 19, 2017)

Enhancements

  • Added support for async functions in no-synchronous-tests (#138)

4.10.1 (June 12, 2017)

Bug Fixes

  • don't drop support for eslint 3.x (#137)

4.10.0 (June 12, 2017)

  • Add node 8 to build environments (#135)

Enhancements

  • Support ESLint 4.x (#134)

Dependency Upgrades

  • Update ramda to the latest version 🚀 (#130)
  • Update pr-log to version 2.0.0 🚀 (#127)

4.9.0 (March 17, 2017)

Dependency Upgrades

  • Update ramda to version 0.23.0 🚀 (#121)

Enhancements

  • Add settings to support additional suite function names (#126)

Documentation

  • Organize alphabetically (#123)

4.8.0 (December 23, 2016)

Enhancements

  • Support MemberExpression for additionalTestFunctions (#114)
  • Make no-mocha-arrows rule fixable (#112)

Bug Fixes

  • Fix no-mocha-arrow fixer (#118)
  • Add node 7 as travis build environment (#115)

Documentation

  • Fix rule name in CHANGELOG to match actual rule (#111)

4.7.0 (October 12, 2016)

Features

  • Add no-nested-tests rule (#109)

4.6.0 (October 3, 2016)

Documentation

  • Adds rule name to title for valid-suite-description documentation. (#107)
  • Adds rule name to title for valid-test-description documentation. (#106)

Features

  • Add 'max-top-level-suites' rule (#103) (#105)

4.5.1 (August 30, 2016)

Bug Fixes

  • Fix crash in no-identical-title (fixes #98) (#99)

4.5.0 (August 29, 2016)

Features

  • Add no-identical-title rule (fixes #33) (#97)

4.4.0 (August 24, 2016)

Features

  • Add no-hooks-for-single-case rule (fixes #44) (#95)
  • Add rule no-return-and-callback (fixes #88) (#94)
  • Add no-top-level-hooks rule (fixes #37) (#87)

Documentation

  • Fix title in no-sibling-hooks documentation file (#92)

Dependency Upgrades

  • Update ramda to version 0.22.1 🚀 (#93)
  • Add editorconfig file (#91)

4.3.0 (August 1, 2016)

Dependency Upgrades

  • Update mocha to version 3.0.0 🚀 (#86)

Features

  • Add rule no-sibling-hooks (fixes #82) (#85)
  • Add rule no-hooks (fixes #39) (#84)

4.2.0 (July 26, 2016)

Features

  • Allow custom test functions (#81)

4.1.0 (July 22, 2016)

Features

  • no-mocha-arrows: New rule (#78)

4.0.0 (July 4, 2016)

Features

  • feat(rules): add 'valid-suite-description' rule (#74)
  • feat(rules): add 'valid-test-description' rule (#68)

Enhancements

  • Add recommended config (#72)

Dependency Upgrades

  • Update eslint to version 3.0.0 🚀 (#70)

Breaking Changes

  • Drop support old node versions (#71)

Documentation

  • Remove fixable from no-exclusive on README (#73)
  • [README] Use a more explicit config (#65)
  • update to docs to match removed autofix (#64)

3.0.0 (June 2, 2016)

Breaking Changes

  • Remove autofix on no-exclusive-tests rule. (#63)

2.2.0 (April 14, 2016)

Features

  • Add rule no-pending-tests (#59)

2.1.0 (April 11, 2016)

Bug Fixes

  • Support specify alias (#58)

Dependency Upgrades

  • Update ramda to version 0.21.0 🚀 (#56)
  • Update ramda to version 0.20.0 🚀 (#53)

Features

  • Add rule no-skipped-tests (#55)

2.0.0 (February 13, 2016)

Breaking Changes

  • Update to eslint 2.0.0 (#49)

1.1.0 (November 13, 2015)

Features

  • Implement new rule no-global-tests (#46)

Enhancements

  • Replace lodash with ramda (#45)

1.0.0 (September 17, 2015)

Enhancements

  • Implement autofix for no-exclusive-tests (#34)
  • Improve detection if done callback is handled (#23)
  • Add integration tests (#30)
  • Instrumment all sources for coverage (#29)
  • Add node 4 to travis-ci build (#42)

Dependency Upgrades

  • Update devDependencies (#43)
  • Update eslint (#31)

Documentation

  • Add NPM Downloads badge (#41)
  • Badges in README.md should only show master status (#40)

0.5.1 (August 20, 2015)

Bug Fixes

  • add new rule to index.js and change tests to keep that from happening (#28)

0.5.0 (August 19, 2015)

Features

  • Add no-synchronous-tests rule (#26)

Dependency Upgrades

  • ESLint 1.x compatibility (#25)
  • Update dependencies (#22)

0.4.0 (June 26, 2015)

Enhancements

  • add context.only to no-exclusive-tests rule (#21)

0.3.0 (June 23, 2015)

Features

  • Add new rule handle-done-callback (#15)
  • Refactor package.json scripts (#17)
  • Disable sudo on travis-ci (#10)
  • Run travis build on node 0.12 and iojs (#11)
  • Ignore log files and .idea folder (#9)
  • Add changelog (#8)

Documentation

  • Fix links to mocha website (#16)
  • Add install documentation to README (#14)

Dependency Upgrades

  • Update dependencies (#18)
  • Update pr-log (#13)
  • Update eslint (#12)
  • Update dev dependencies (#7)

0.2.2 (October 25, 2014)

Bug Fixes

  • Allow all ESLint versions >= 0.8.0

0.2.1 (October 18, 2014)

  • Add recommended keywords to package.json

0.2.0 (September 20, 2014)

Enhancements

  • Support mochas tdd interface (fixes #4)
  • Allow minor version updates for eslint

Documentation

  • Docs: remove unnecessary backtick

Dependency Upgrades

  • Update devDependencies.

0.1.1 (September 6, 2014)

  • Add .npmignore

0.1.0 (September 6, 2014)

Initial release