6 specialized rules for linting ESLint test files, ensuring test case consistency, proper test structure, and adherence to testing best practices.
Rules that enforce proper test case structure and organization.
/**
* Enforce consistent use of output assertions in rule tests
*/
declare const consistentOutput: Rule.RuleModule;
/**
* Enforce consistent ordering of properties in test cases
*/
declare const testCasePropertyOrdering: Rule.RuleModule;
/**
* Enforce use of shorthand strings in simple test cases
*/
declare const testCaseShorthandStrings: Rule.RuleModule;Usage Examples:
// Good: Consistent test case structure
new RuleTester().run('my-rule', rule, {
valid: [
// Good: shorthand string for simple cases
'const x = 1;',
// Good: object form with proper property ordering
{
code: 'const y = 2;',
options: [{ strict: true }],
parserOptions: { ecmaVersion: 2015 }
}
],
invalid: [
{
code: 'var x = 1;',
output: 'const x = 1;', // Good: consistent output usage
errors: [{ messageId: 'preferConst' }]
}
]
});
// Bad: Inconsistent structure
{
parserOptions: { ecmaVersion: 2015 }, // Bad: wrong property order
code: 'const y = 2;',
options: [{ strict: true }]
}Rules that prevent common testing mistakes and ensure test reliability.
/**
* Disallow identical test cases
*/
declare const noIdenticalTests: Rule.RuleModule;
/**
* Disallow .only() in tests to prevent accidentally committed focused tests
*/
declare const noOnlyTests: Rule.RuleModule;Usage Examples:
// Bad: Identical test cases
new RuleTester().run('my-rule', rule, {
valid: [
'const x = 1;',
'const x = 1;' // Bad: identical to previous test
],
invalid: []
});
// Bad: Committed .only() test
new RuleTester().run.only('my-rule', rule, { // Bad: .only() should not be committed
valid: ['const x = 1;'],
invalid: []
});
// Good: Unique test cases
new RuleTester().run('my-rule', rule, {
valid: [
'const x = 1;',
'const y = 2;' // Good: different test case
],
invalid: []
});Rules specifically for handling test case output and assertions.
/**
* Prefer using null over undefined for test case output
*/
declare const preferOutputNull: Rule.RuleModule;Usage Examples:
// Good: Using null for no output
new RuleTester().run('my-rule', rule, {
valid: [],
invalid: [
{
code: 'debugger;',
output: null, // Good: explicit null when no fix expected
errors: [{ messageId: 'noDebugger' }]
}
]
});
// Bad: Using undefined for no output
{
code: 'debugger;',
output: undefined, // Bad: prefer null over undefined
errors: [{ messageId: 'noDebugger' }]
}Rules that work specifically with ESLint test file patterns and RuleTester usage.
/**
* All test-related rules that apply to ESLint test files
*/
declare const testRules: {
'consistent-output': Rule.RuleModule;
'no-identical-tests': Rule.RuleModule;
'no-only-tests': Rule.RuleModule;
'prefer-output-null': Rule.RuleModule;
'test-case-property-ordering': Rule.RuleModule;
'test-case-shorthand-strings': Rule.RuleModule;
};These rules automatically apply to files that contain RuleTester usage patterns:
// Files with these patterns are detected as test files:
const RuleTester = require('eslint').RuleTester;
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
// RuleTester constructor usage
new RuleTester();
new eslint.RuleTester();
// RuleTester.run() method calls
ruleTester.run('rule-name', rule, tests);Complex test case structures that the rules understand:
// Complex test case structure
new RuleTester({
parserOptions: { ecmaVersion: 2022, sourceType: 'module' }
}).run('complex-rule', rule, {
valid: [
// Simple string case
'const valid = true;',
// Object case with all properties in correct order
{
code: 'const x = getValue();',
filename: 'test.js',
options: [{ allowFunctionCalls: true }],
parserOptions: { ecmaVersion: 2020 },
settings: { complexity: 'low' },
globals: { getValue: 'readonly' }
},
// Conditional test cases
...(process.env.NODE_ENV === 'test' ? [
'const conditionalTest = true;'
] : [])
],
invalid: [
{
code: 'var problematic = getValue();',
output: 'const problematic = getValue();',
options: [{ allowFunctionCalls: true }],
parserOptions: { ecmaVersion: 2020 },
errors: [
{
messageId: 'preferConst',
type: 'VariableDeclaration',
line: 1,
column: 1,
suggestions: [
{
messageId: 'useConst',
output: 'const problematic = getValue();'
}
]
}
]
}
]
});Rules are categorized and can be applied selectively:
// Apply only to test files
export default [
{
files: ['tests/**/*.js', '**/*.test.js', '**/*.spec.js'],
rules: {
'eslint-plugin/consistent-output': 'error',
'eslint-plugin/no-identical-tests': 'error',
'eslint-plugin/no-only-tests': 'error',
'eslint-plugin/test-case-property-ordering': 'warn',
'eslint-plugin/test-case-shorthand-strings': 'warn',
'eslint-plugin/prefer-output-null': 'warn'
}
}
];