ESLint Test Utility
eslint-tester is a utility package designed to facilitate testing within the ESLint project itself, and was initially intended for broader use by authors of custom ESLint rules. However, as of ESLint v1.0.0, this package has been deprecated and is no longer actively developed. Its functionality has been superseded by the `RuleTester` class, which is directly exposed within the main `eslint` package and is the recommended way to test ESLint rules. The package's last significant update (`v0.8.2`) was in June 2015, and its GitHub repository explicitly marks it as 'DEPRECATED'. Users should avoid this package and instead use `RuleTester` from `eslint` for current projects. The release cadence was irregular, with its primary focus being internal ESLint development.
Common errors
-
TypeError: ESLintTester is not a constructor
cause Attempting to import `ESLintTester` using ES Modules syntax (`import`) in an environment that only supports CommonJS for this legacy package, or incorrect capitalization/destructuring.fixEnsure you are using CommonJS `require` syntax: `const ESLintTester = require('eslint-tester');` -
Error: Missing definition for `afterAll` - you must set one using `RuleTester.afterAll` or there must be one defined globally as `afterAll`.
cause This specific error usually comes from `RuleTester` (the replacement for `eslint-tester`) but it signifies that the test environment lacks global hooks (`beforeAll`, `afterAll`, etc.) expected by test utilities. It might surface if `eslint-tester` internally relied on similar patterns, or when migrating tests away from `eslint-tester`.fixEnsure your test runner (e.g., Mocha, Jest, Vitest) is correctly configured to provide global test hooks, or explicitly set `RuleTester.afterAll` (and similar hooks) before running tests: `RuleTester.afterAll = require('mocha').after;` (or equivalent for your test framework).
Warnings
- breaking As of `v0.8.0`, test options are now validated, which may cause existing tests with malformed options to fail. This aligns `eslint-tester` with `eslint`'s more stringent configuration validation.
- deprecated The `eslint-tester` package has been officially deprecated since ESLint v1.0.0 and is no longer under active development. It has been replaced by the `RuleTester` class available directly in the `eslint` package.
- gotcha The package's primary use was internal to the ESLint project development. Its public API, especially for external custom rule authors, was not extensively documented or stable. Attempting to use it for complex external rule testing may lead to unexpected behavior or API inconsistencies.
Install
-
npm install eslint-tester -
yarn add eslint-tester -
pnpm add eslint-tester
Imports
- ESLintTester
import { ESLintTester } from 'eslint-tester';const ESLintTester = require('eslint-tester');
Quickstart
const ESLintTester = require('eslint-tester');
// Mock a simple ESLint rule for demonstration
const myCustomRule = {
meta: {
type: 'suggestion',
schema: [],
messages: {
avoidFoo: 'Avoid using the identifier `foo`.'
}
},
create(context) {
return {
Identifier(node) {
if (node.name === 'foo') {
context.report({ node, messageId: 'avoidFoo' });
}
}
};
}
};
// Instantiate ESLintTester (note: actual usage would involve an ESLint config)
// This is a simplified example based on internal testing patterns.
// In a real scenario, you'd pass a configured ESLint instance or path to rules.
// As this package is deprecated, detailed external API examples are scarce.
const tester = new ESLintTester();
// An example of a valid test case (no errors expected)
const validCase = {
code: 'const bar = 1;',
parserOptions: { ecmaVersion: 6 }
};
// An example of an invalid test case (errors expected)
const invalidCase = {
code: 'const foo = 2;',
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: 'avoidFoo', line: 1, column: 7 }]
};
// Run a test (this API is inferred and might not match direct public usage perfectly for older versions)
// The actual `eslint-tester` API is complex and meant for internal ESLint use.
// For modern rule testing, use `RuleTester` directly from `eslint`.
console.log('Running tests...');
// This part of the quickstart is conceptual as eslint-tester's public API for rule testing is not straightforward.
// Its primary use was internal to ESLint development.
// For actual rule testing, one would register rules and then run tests, similar to RuleTester.
// const results = tester.run('my-custom-rule', myCustomRule, { valid: [validCase], invalid: [invalidCase] });
// console.log(results);
console.log('`eslint-tester` is deprecated. Use `RuleTester` from `eslint` for modern rule testing.');
console.log('A complete runnable example for `eslint-tester` is difficult due to its internal-focused API and deprecation.');