Rules that prevent problematic test execution patterns and enforce development workflow practices in Mocha test suites.
Prevents exclusive tests (.only) from being committed, which would cause other tests to be skipped in CI/CD.
/**
* Disallows exclusive tests (.only)
* Prevents accidental commits of focused tests that skip other tests
*/
const noExclusiveTestsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow exclusive tests',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-exclusive-tests.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-exclusive-tests": "error"
}
}Examples:
// ✓ Good - normal tests
describe('Calculator', function() {
it('should add numbers', function() {
assert.equal(add(1, 2), 3);
});
it('should subtract numbers', function() {
assert.equal(subtract(5, 2), 3);
});
});
// ✗ Bad - exclusive tests
describe('Calculator', function() {
it.only('should add numbers', function() { // Not allowed
assert.equal(add(1, 2), 3);
});
it('should subtract numbers', function() {
assert.equal(subtract(5, 2), 3);
});
});
describe.only('Calculator', function() { // Not allowed
// Tests
});Prevents skipped tests (.skip) from being committed, ensuring all tests are active and maintained.
/**
* Disallows skipped tests (.skip)
* Prevents accumulation of disabled tests in codebase
*/
const noSkippedTestsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow skipped tests',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-skipped-tests.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-skipped-tests": "warn"
}
}Examples:
// ✓ Good - active tests
it('should process data', function() {
// Test implementation
});
describe('DataProcessor', function() {
// Tests
});
// ✗ Bad - skipped tests
it.skip('should process data', function() { // Not allowed
// Test implementation
});
describe.skip('DataProcessor', function() { // Not allowed
// Tests
});
xit('should process data', function() { // Not allowed (BDD interface)
// Test implementation
});Disallows pending/incomplete tests that have no implementation body.
/**
* Disallows pending tests without implementation
* Prevents incomplete test stubs from being committed
*/
const noPendingTestsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow pending tests',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-pending-tests.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-pending-tests": "warn"
}
}Examples:
// ✓ Good - implemented test
it('should validate input', function() {
assert.equal(validate('test'), true);
});
// ✗ Bad - pending test (no implementation)
it('should validate input'); // Not allowed
// ✗ Bad - empty test body
it('should validate input', function() {
// No implementation
});Prevents tests from being defined at the global scope outside of describe blocks.
/**
* Disallows global tests outside describe blocks
* Ensures tests are properly organized in suites
*/
const noGlobalTestsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow global tests',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-global-tests.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-global-tests": "error"
}
}Examples:
// ✓ Good - tests within describe blocks
describe('Calculator', function() {
it('should add numbers', function() {
assert.equal(add(1, 2), 3);
});
});
// ✗ Bad - global test
it('should add numbers', function() { // Not allowed at global scope
assert.equal(add(1, 2), 3);
});Prevents test files from exporting functions or variables, maintaining test file purity.
/**
* Disallows exports from test files
* Ensures test files remain focused on testing only
*/
const noExportsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow exports from test files',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-exports.md'
},
schema: []
},
create: (context) => ESLintVisitor
};Usage:
{
"rules": {
"mocha/no-exports": "error"
}
}Examples:
// ✓ Good - no exports in test file
describe('Calculator', function() {
const helper = function() {
return 'helper';
};
it('should work', function() {
assert.equal(helper(), 'helper');
});
});
// ✗ Bad - exports in test file
function testHelper() { // Not allowed
return 'helper';
}
module.exports = { // Not allowed
testHelper
};
export function helper() { // Not allowed
return 'helper';
}{
"rules": {
"mocha/no-exclusive-tests": "error",
"mocha/no-skipped-tests": "warn",
"mocha/no-pending-tests": "warn",
"mocha/no-global-tests": "error",
"mocha/no-exports": "error"
}
}{
"rules": {
"mocha/no-exclusive-tests": "error",
"mocha/no-skipped-tests": "error",
"mocha/no-pending-tests": "error",
"mocha/no-global-tests": "error",
"mocha/no-exports": "error"
}
}// Development configuration - allow some flexibility
const devConfig = {
rules: {
"mocha/no-exclusive-tests": "warn", // Allow during development
"mocha/no-skipped-tests": "off", // Allow skipping temporarily
"mocha/no-pending-tests": "warn", // Allow test stubs
"mocha/no-global-tests": "error",
"mocha/no-exports": "error"
}
};
// Production/CI configuration - strict enforcement
const prodConfig = {
rules: {
"mocha/no-exclusive-tests": "error", // Block exclusive tests
"mocha/no-skipped-tests": "error", // Block skipped tests
"mocha/no-pending-tests": "error", // Block incomplete tests
"mocha/no-global-tests": "error",
"mocha/no-exports": "error"
}
};// ✓ Good - well-organized test file
describe('UserService', function() {
describe('#create', function() {
it('should create user with valid data', function() {
const user = userService.create('John', 'john@example.com');
assert.equal(user.name, 'John');
});
it('should throw error for invalid email', function() {
assert.throws(() => {
userService.create('John', 'invalid');
});
});
});
describe('#update', function() {
it('should update existing user', function() {
// Test implementation
});
});
});