Tooling to test ESLint rules with comprehensive TypeScript support and advanced testing capabilities
npx @tessl/cli install tessl/npm-typescript-eslint--rule-tester@8.42.0The @typescript-eslint/rule-tester package provides comprehensive tooling for testing ESLint rules, specifically designed for TypeScript-ESLint rules. It offers a RuleTester class that extends ESLint's native rule testing capabilities with enhanced TypeScript support, including type-aware rule testing, complex configuration validation, and sophisticated test case management with support for both valid and invalid test scenarios.
npm install @typescript-eslint/rule-testerimport { RuleTester, noFormat } from "@typescript-eslint/rule-tester";
import type {
RuleTesterConfig,
ValidTestCase,
InvalidTestCase,
RunTests
} from "@typescript-eslint/rule-tester";For CommonJS:
const { RuleTester, noFormat } = require("@typescript-eslint/rule-tester");import { RuleTester } from "@typescript-eslint/rule-tester";
import { myCustomRule } from "./my-custom-rule";
// Create a rule tester instance
const ruleTester = new RuleTester({
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
},
},
});
// Test your rule
ruleTester.run("my-custom-rule", myCustomRule, {
valid: [
// Valid test cases
"const x = 1;",
{
code: "function foo(): number { return 1; }",
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
},
},
},
],
invalid: [
// Invalid test cases
{
code: "var x = 1;",
errors: [{ messageId: "noVar" }],
output: "const x = 1;",
},
],
});The Rule Tester is built around several key components:
Core rule testing functionality providing comprehensive test execution, validation, and reporting for ESLint rules.
class RuleTester {
constructor(testerConfig?: RuleTesterConfig);
run<MessageIds extends string, Options extends readonly unknown[]>(
ruleName: string,
rule: RuleModule<MessageIds, Options>,
test: RunTests<MessageIds, Options>
): void;
defineRule(name: string, rule: AnyRuleModule): void;
}Comprehensive test case definition system with support for valid and invalid scenarios, autofix testing, and suggestion validation.
interface RunTests<MessageIds extends string, Options extends readonly unknown[]> {
readonly valid: readonly (string | ValidTestCase<Options>)[];
readonly invalid: readonly InvalidTestCase<MessageIds, Options>[];
}
interface ValidTestCase<Options extends readonly unknown[]> {
readonly after?: () => void;
readonly before?: () => void;
readonly code: string;
readonly dependencyConstraints?: DependencyConstraint;
readonly filename?: string;
readonly languageOptions?: TestLanguageOptions;
readonly name?: string;
readonly only?: boolean;
readonly options?: Readonly<Options>;
readonly settings?: Readonly<SharedConfigurationSettings>;
readonly skip?: boolean;
}
interface InvalidTestCase<MessageIds extends string, Options extends readonly unknown[]>
extends ValidTestCase<Options> {
readonly errors: readonly TestCaseError<MessageIds>[];
readonly output?: string | string[] | null;
}Environment dependency validation system for conditional test execution based on package versions and availability.
type DependencyConstraint = Readonly<Record<string, VersionConstraint>>;
type VersionConstraint = AtLeastVersionConstraint | SemverVersionConstraint;
type AtLeastVersionConstraint =
| `${number}.${number}.${number}-${string}`
| `${number}.${number}.${number}`
| `${number}.${number}`
| `${number}`;
interface SemverVersionConstraint {
readonly range: string;
readonly options?: boolean | RangeOptions;
}Static configuration methods for managing default settings across all RuleTester instances.
class RuleTester {
static setDefaultConfig(config: RuleTesterConfig): void;
static getDefaultConfig(): Readonly<RuleTesterConfig>;
static resetDefaultConfig(): void;
static only<Options>(item: string | ValidTestCase<Options>): ValidTestCase<Options>;
}Helper functions for test case formatting and test framework integration.
function noFormat(raw: TemplateStringsArray, ...keys: string[]): string;interface RuleTesterConfig extends FlatConfig.Config {
readonly defaultFilenames?: Readonly<{
ts: string;
tsx: string;
}>;
readonly dependencyConstraints?: DependencyConstraint;
}
interface TestLanguageOptions {
readonly parser?: Readonly<Parser.LooseParserModule>;
readonly parserOptions?: Readonly<ParserOptions>;
readonly globals?: Readonly<Linter.GlobalsConfig>;
readonly env?: Readonly<Linter.EnvironmentConfig>;
}
interface TestCaseError<MessageIds extends string> {
readonly messageId: MessageIds;
readonly line?: number;
readonly column?: number;
readonly endLine?: number;
readonly endColumn?: number;
readonly type?: AST_NODE_TYPES | AST_TOKEN_TYPES;
readonly data?: ReportDescriptorMessageData;
readonly suggestions?: readonly SuggestionOutput<MessageIds>[] | null;
}
interface SuggestionOutput<MessageIds extends string> {
readonly messageId: MessageIds;
readonly output: string;
readonly data?: ReportDescriptorMessageData;
}
type SharedConfigurationSettings = Record<string, unknown>;