Skip to content

⬅️ Back to Table of Contents

📄 ts-eslint/RuleTester

📊 Analysis Summary

Metric Count
🔧 Functions 2
🧱 Classes 2
📦 Imports 10
📐 Interfaces 6
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/utils/src/ts-eslint/RuleTester.ts

📦 Imports

Name Source
ESLintRuleTester eslint
AST_NODE_TYPES ../ts-estree
AST_TOKEN_TYPES ../ts-estree
ClassicConfig ./Config
Linter ./Linter
ParserOptions ./ParserOptions
ReportDescriptorMessageData ./Rule
RuleCreateFunction ./Rule
RuleModule ./Rule
SharedConfigurationSettings ./Rule

Functions

RuleTesterBase.run(ruleName: string, rule: RuleModule<MessageIds, Options>, tests: RunTests<MessageIds, Options>): void

Adds a new rule test to execute.

Parameters:

  • ruleName any: The name of the rule to run.
  • rule any: The rule to test.
  • tests any: The collection of tests to run.
Raw JSDoc
/**
   * Adds a new rule test to execute.
   * @param ruleName The name of the rule to run.
   * @param rule The rule to test.
   * @param tests The collection of tests to run.
   */
Code
run<MessageIds extends string, Options extends readonly unknown[]>(
    ruleName: string,
    rule: RuleModule<MessageIds, Options>,
    tests: RunTests<MessageIds, Options>,
  ): void;

RuleTesterBase.defineRule(name: string, rule: RuleCreateFunction<MessageIds, Options>…): void

Define a rule for one particular run of tests.

Raw JSDoc
/**
   * Define a rule for one particular run of tests.
   */
Code
defineRule<MessageIds extends string, Options extends readonly unknown[]>(
    name: string,
    rule:
      RuleCreateFunction<MessageIds, Options> | RuleModule<MessageIds, Options>,
  ): void;

Classes

RuleTesterBase

Class Code
declare class RuleTesterBase {
  /**
   * Creates a new instance of RuleTester.
   * @param testerConfig extra configuration for the tester
   */
  constructor(testerConfig?: RuleTesterConfig);

  /**
   * Adds a new rule test to execute.
   * @param ruleName The name of the rule to run.
   * @param rule The rule to test.
   * @param tests The collection of tests to run.
   */
  run<MessageIds extends string, Options extends readonly unknown[]>(
    ruleName: string,
    rule: RuleModule<MessageIds, Options>,
    tests: RunTests<MessageIds, Options>,
  ): void;

  /**
   * If you supply a value to this property, the rule tester will call this instead of using the version defined on
   * the global namespace.
   */
  static get describe(): RuleTesterTestFrameworkFunction;
  static set describe(value: RuleTesterTestFrameworkFunction | undefined);

  /**
   * If you supply a value to this property, the rule tester will call this instead of using the version defined on
   * the global namespace.
   */
  static get it(): RuleTesterTestFrameworkFunction;
  static set it(value: RuleTesterTestFrameworkFunction | undefined);

  /**
   * If you supply a value to this property, the rule tester will call this instead of using the version defined on
   * the global namespace.
   */
  static get itOnly(): RuleTesterTestFrameworkFunction;
  static set itOnly(value: RuleTesterTestFrameworkFunction | undefined);

  /**
   * Define a rule for one particular run of tests.
   */
  defineRule<MessageIds extends string, Options extends readonly unknown[]>(
    name: string,
    rule:
      RuleCreateFunction<MessageIds, Options> | RuleModule<MessageIds, Options>,
  ): void;
}

Methods (2) — full entries under Functions

Method Signature
run (ruleName: string, rule: RuleModule<MessageIds, Options>, tests: RunTests<MessageIds, Options>): ...
defineRule (name: string, rule: RuleCreateFunction<MessageIds, Options> \| RuleModule<MessageIds, Options>):...

RuleTester

Extends: (ESLintRuleTester as typeof RuleTesterBase)

Class Code
export class RuleTester extends (ESLintRuleTester as typeof RuleTesterBase) {}

Interfaces

ValidTestCase<Options extends readonly unknown[]>

Interface Code
export interface ValidTestCase<Options extends readonly unknown[]> {
  /**
   * Code for the test case.
   */
  readonly code: string;
  /**
   * Environments for the test case.
   */
  readonly env?: Readonly<Linter.EnvironmentConfig>;
  /**
   * The fake filename for the test case. Useful for rules that make assertion about filenames.
   */
  readonly filename?: string;
  /**
   * The additional global variables.
   */
  readonly globals?: Readonly<Linter.GlobalsConfig>;
  /**
   * Name for the test case.
   */
  readonly name?: string;
  /**
   * Run this case exclusively for debugging in supported test frameworks.
   */
  readonly only?: boolean;
  /**
   * Options for the test case.
   */
  readonly options?: Readonly<Options>;
  /**
   * The absolute path for the parser.
   */
  readonly parser?: string;
  /**
   * Options for the parser.
   */
  readonly parserOptions?: Readonly<ParserOptions>;
  /**
   * Settings for the test case.
   */
  readonly settings?: Readonly<SharedConfigurationSettings>;
}

Properties

Name Type Optional Description
code string not shown
env Readonly<Linter.EnvironmentConfig> not shown
filename string not shown
globals Readonly<Linter.GlobalsConfig> not shown
name string not shown
only boolean not shown
options Readonly<Options> not shown
parser string not shown
parserOptions Readonly<ParserOptions> not shown
settings Readonly<SharedConfigurationSettings> not shown

SuggestionOutput<MessageIds extends string>

Interface Code
export interface SuggestionOutput<MessageIds extends string> {
  /**
   * The data used to fill the message template.
   */
  readonly data?: ReportDescriptorMessageData;
  /**
   * Reported message ID.
   */
  readonly messageId: MessageIds;
  /**
   * NOTE: Suggestions will be applied as a stand-alone change, without triggering multi-pass fixes.
   * Each individual error has its own suggestion, so you have to show the correct, _isolated_ output for each suggestion.
   */
  readonly output: string;

  // we disallow this because it's much better to use messageIds for reusable errors that are easily testable
  // readonly desc?: string;
}

Properties

Name Type Optional Description
data ReportDescriptorMessageData not shown
messageId MessageIds not shown
output string not shown

InvalidTestCase<MessageIds extends string, Options extends readonly unknown[]>

Interface Code
export interface InvalidTestCase<
  MessageIds extends string,
  Options extends readonly unknown[],
> extends ValidTestCase<Options> {
  /**
   * Expected errors.
   */
  readonly errors: readonly TestCaseError<MessageIds>[];
  /**
   * The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
   */
  readonly output?: string | string[] | null;
}

Properties

Name Type Optional Description
errors readonly TestCaseError<MessageIds>[] not shown
output string \| string[] \| null not shown

TestCaseError<MessageIds extends string>

Interface Code
export interface TestCaseError<MessageIds extends string> {
  /**
   * The 1-based column number of the reported start location.
   */
  readonly column?: number;
  /**
   * The data used to fill the message template.
   */
  readonly data?: ReportDescriptorMessageData;
  /**
   * The 1-based column number of the reported end location.
   */
  readonly endColumn?: number;
  /**
   * The 1-based line number of the reported end location.
   */
  readonly endLine?: number;
  /**
   * The 1-based line number of the reported start location.
   */
  readonly line?: number;
  /**
   * Reported message ID.
   */
  readonly messageId: MessageIds;
  /**
   * Reported suggestions.
   */
  readonly suggestions?: readonly SuggestionOutput<MessageIds>[] | null;
  /**
   * The type of the reported AST node.
   */
  readonly type?: AST_NODE_TYPES | AST_TOKEN_TYPES;

  // we disallow this because it's much better to use messageIds for reusable errors that are easily testable
  // readonly message?: string | RegExp;
}

Properties

Name Type Optional Description
column number not shown
data ReportDescriptorMessageData not shown
endColumn number not shown
endLine number not shown
line number not shown
messageId MessageIds not shown
suggestions readonly SuggestionOutput<MessageIds>[] \| null not shown
type AST_NODE_TYPES \| AST_TOKEN_TYPES not shown

RunTests<MessageIds extends string, Options extends readonly unknown[]>

Interface Code
export interface RunTests<
  MessageIds extends string,
  Options extends readonly unknown[],
> {
  // RuleTester.run also accepts strings for valid cases
  readonly invalid: readonly InvalidTestCase<MessageIds, Options>[];
  readonly valid: readonly (string | ValidTestCase<Options>)[];
}

Properties

Name Type Optional Description
invalid readonly InvalidTestCase<MessageIds, Options>[] not shown
valid readonly (string \| ValidTestCase<Options>)[] not shown

RuleTesterConfig

Interface Code
export interface RuleTesterConfig extends ClassicConfig.Config {
  // should be require.resolve(parserPackageName)
  readonly parser: string;
  readonly parserOptions?: Readonly<ParserOptions>;
}

Properties

Name Type Optional Description
parser string not shown
parserOptions Readonly<ParserOptions> not shown

Type Aliases

RuleTesterTestFrameworkFunction

/* * @param text a string describing the rule * @deprecated Use @typescript-eslint/rule-tester instead. /

type RuleTesterTestFrameworkFunction = (
  text: string,
  callback: () => void,
) => void;

Generated by Syntax Scribe