Rules that enforce test code quality, readability, and maintainability practices in Mocha test suites.
Prevents identical test titles within the same test suite, improving test readability and debugging.
/**
* Disallows identical test titles in the same suite
* Helps identify duplicate or poorly named tests
*/
const noIdenticalTitleRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow identical titles',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-identical-title.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-identical-title": "error"
}
}Ensures all tests have meaningful descriptions, preventing empty or whitespace-only test names.
/**
* Disallows empty test descriptions
* Enforces meaningful test naming for better documentation
*/
const noEmptyDescriptionRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow empty test descriptions',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-empty-description.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-empty-description": "error"
}
}Validates test description format according to configurable patterns and conventions.
/**
* Validates test description format
* Configurable pattern matching for consistent test naming
*/
const validTestDescriptionRule = {
meta: {
type: 'suggestion',
docs: {
description: 'Require valid test descriptions',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/valid-test-description.md'
},
schema: [{
type: 'object',
properties: {
pattern: { type: 'string' },
testNames: {
type: 'array',
items: { type: 'string' }
},
message: { type: 'string' }
},
additionalProperties: false
}]
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/valid-test-description": ["error", {
"pattern": "^should .+$",
"message": "Test description must start with 'should'"
}]
}
}Validates suite (describe block) description format according to configurable patterns.
/**
* Validates suite description format
* Configurable pattern matching for consistent suite naming
*/
const validSuiteDescriptionRule = {
meta: {
type: 'suggestion',
docs: {
description: 'Require valid suite descriptions',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/valid-suite-description.md'
},
schema: [{
type: 'object',
properties: {
pattern: { type: 'string' },
suiteNames: {
type: 'array',
items: { type: 'string' }
},
message: { type: 'string' }
},
additionalProperties: false
}]
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/valid-suite-description": ["error", {
"pattern": "^[A-Z].*$",
"message": "Suite description must start with uppercase letter"
}]
}
}Enforces consistent spacing between test blocks for improved readability.
/**
* Enforces consistent spacing between test blocks
* Improves visual separation and code readability
*/
const consistentSpacingRule = {
meta: {
type: 'layout',
docs: {
description: 'Require consistent spacing between blocks',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/consistent-spacing-between-blocks.md'
},
fixable: 'whitespace',
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/consistent-spacing-between-blocks": "error"
}
}Limits the number of top-level test suites per file to improve test organization.
/**
* Limits the number of top-level test suites
* Encourages better test file organization
*/
const maxTopLevelSuitesRule = {
meta: {
type: 'suggestion',
docs: {
description: 'Limit the number of top-level suites',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/max-top-level-suites.md'
},
schema: [{
type: 'object',
properties: {
limit: {
type: 'integer',
minimum: 1
}
},
additionalProperties: false
}]
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/max-top-level-suites": ["error", { "limit": 1 }]
}
}{
"rules": {
"mocha/no-identical-title": "error",
"mocha/no-empty-description": "error",
"mocha/valid-test-description": ["error", {
"pattern": "^should (have|be|do|return|throw|call|not) .+$",
"message": "Test should describe expected behavior"
}],
"mocha/valid-suite-description": ["error", {
"pattern": "^[A-Z][a-zA-Z0-9\\s]*$",
"message": "Suite should start with uppercase and contain only letters, numbers, and spaces"
}],
"mocha/consistent-spacing-between-blocks": "error",
"mocha/max-top-level-suites": ["error", { "limit": 1 }]
}
}// ✓ Valid test descriptions with pattern "^should .+$"
it('should return correct result', function() {});
it('should throw error for invalid input', function() {});
// ✗ Invalid test descriptions
it('returns result', function() {}); // Missing 'should'
it('', function() {}); // Empty description
// ✓ Valid suite descriptions with uppercase start
describe('UserService', function() {});
describe('Authentication module', function() {});
// ✗ Invalid suite descriptions
describe('userService', function() {}); // Lowercase start
describe('', function() {}); // Empty description