What is eslint-plugin-mocha?
eslint-plugin-mocha is an ESLint plugin that provides linting rules specific to Mocha, a popular JavaScript test framework. It helps enforce best practices and coding standards for Mocha tests, ensuring consistency and reducing common errors.
What are eslint-plugin-mocha's main functionalities?
Disallow exclusive tests
This rule disallows the use of exclusive tests (e.g., `describe.only` and `it.only`) to prevent accidentally skipping other tests.
module.exports = { "rules": { "mocha/no-exclusive-tests": "error" } };
Disallow skipped tests
This rule warns against the use of skipped tests (e.g., `describe.skip` and `it.skip`) to ensure that all tests are being run.
module.exports = { "rules": { "mocha/no-skipped-tests": "warn" } };
Enforce test case titles
This rule enforces that test cases have valid descriptions, ensuring that test titles are meaningful and descriptive.
module.exports = { "rules": { "mocha/valid-test-description": "error" } };
Disallow synchronous tests
This rule disallows synchronous tests, encouraging the use of asynchronous tests to avoid blocking the event loop.
module.exports = { "rules": { "mocha/no-synchronous-tests": "error" } };
Limit the number of top-level suites
This rule limits the number of top-level suites in a test file, promoting better organization and structure of tests.
module.exports = { "rules": { "mocha/max-top-level-suites": ["error", { "limit": 1 }] } };
Other packages similar to eslint-plugin-mocha
eslint-plugin-jest
eslint-plugin-jest is an ESLint plugin for Jest, another popular JavaScript testing framework. It provides similar linting rules specific to Jest, helping enforce best practices and coding standards for Jest tests. While eslint-plugin-mocha is tailored for Mocha, eslint-plugin-jest is designed for Jest.
eslint-plugin-jasmine
eslint-plugin-jasmine is an ESLint plugin for Jasmine, a behavior-driven development framework for testing JavaScript code. It offers linting rules specific to Jasmine, ensuring best practices and consistency in Jasmine tests. Like eslint-plugin-mocha, it focuses on a different testing framework.
eslint-plugin-qunit
eslint-plugin-qunit is an ESLint plugin for QUnit, a powerful, easy-to-use JavaScript unit testing framework. It provides linting rules specific to QUnit, helping maintain best practices and coding standards in QUnit tests. It serves a similar purpose to eslint-plugin-mocha but for QUnit.
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
];
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
recommended
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.