or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dependency-constraints.mdglobal-configuration.mdindex.mdrule-testing.mdtest-cases.mdutilities.md
tile.json

tessl/npm-typescript-eslint--rule-tester

Tooling to test ESLint rules with comprehensive TypeScript support and advanced testing capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@typescript-eslint/rule-tester@8.42.x

To install, run

npx @tessl/cli install tessl/npm-typescript-eslint--rule-tester@8.42.0

index.mddocs/

TypeScript ESLint Rule Tester

The @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.

Package Information

  • Package Name: @typescript-eslint/rule-tester
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @typescript-eslint/rule-tester

Core Imports

import { 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");

Basic Usage

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;",
    },
  ],
});

Architecture

The Rule Tester is built around several key components:

  • RuleTester Class: Central testing engine that manages rule execution and validation
  • Test Framework Integration: Pluggable system supporting Jest, Mocha, and custom test frameworks
  • TypeScript Parser Integration: Built-in @typescript-eslint/parser with type-aware rule testing
  • Configuration System: Extends ESLint's flat config format with TypeScript-specific options
  • Validation Engine: Comprehensive validation using AJV schema validation for test case integrity
  • Dependency Constraints: Conditional test execution based on environment requirements

Capabilities

Rule Testing Engine

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;
}

Rule Testing Engine

Test Case Configuration

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;
}

Test Case Configuration

Dependency Constraints

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;
}

Dependency Constraints

Global Configuration

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>;
}

Global Configuration

Utility Functions

Helper functions for test case formatting and test framework integration.

function noFormat(raw: TemplateStringsArray, ...keys: string[]): string;

Utility Functions

Core Types

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>;