eslint-vitest-rule-tester
Version:
ESLint rule tester with Vitest
149 lines (142 loc) • 5.44 kB
TypeScript
import { Linter } from 'eslint';
export { unindent as $, unindent } from '@antfu/utils';
interface ValidTestCaseBase extends CompatConfigOptions, RuleTesterBehaviorOptions {
name?: string;
description?: string;
code: string;
options?: any;
filename?: string;
only?: boolean;
skip?: boolean;
onResult?: (result: Linter.FixReport) => void;
}
interface TestCaseError extends Partial<Linter.LintMessage> {
/**
* Data for interpolate the error message
*/
data?: Record<string, any>;
/**
* Alias to `nodeType`
*/
type?: string;
}
interface InvalidTestCaseBase extends ValidTestCaseBase {
/**
* Expected errors.
* If a number is provided, it asserts that the number of errors is equal to the number provided.
* If an array of strings is provided, it asserts that the error messageIds are equal to the array provided.
* If an array of objects is provided, it asserts that the errors are partially equal to the objects provided.
*/
errors?: number | (string | TestCaseError)[] | ((errors: Linter.LintMessage[]) => void);
/**
* Assert if output is expected.
* Pass `null` to assert that the output is the same as the input.
*/
output?: string | null | ((output: string, input: string) => void);
}
interface NormalizedTestCase extends InvalidTestCaseBase {
type: 'valid' | 'invalid';
code: string;
}
type InvalidTestCase = InvalidTestCaseBase | string;
type ValidTestCase = ValidTestCaseBase | string;
type TestCase = ValidTestCase | InvalidTestCase;
interface TestExecutionResult extends Linter.FixReport {
/**
* If the rule fixes in multiple steps, each step will be present here
*/
steps: Linter.FixReport[];
}
interface CompatConfigOptions {
parserOptions?: Linter.ParserOptions;
parser?: Linter.Parser;
languageOptions?: Linter.Config['languageOptions'];
linterOptions?: Linter.Config['linterOptions'];
settings?: Linter.Config['settings'];
processor?: Linter.Config['processor'];
files?: Linter.Config['files'];
}
type RuleModule = any;
interface RuleTester {
/**
* Run a single test case
*/
each: (arg: TestCase) => TestExecutionResult;
/**
* Run a single valid test case
*/
valid: (arg: ValidTestCase) => TestExecutionResult;
/**
* Run a single invalid test case
*/
invalid: (arg: InvalidTestCase) => TestExecutionResult;
/**
* ESLint's RuleTester style test runner, that runs multiple test cases
*/
run: (options: TestCasesOptions) => void;
}
interface RuleTesterBehaviorOptions {
/**
* The number of times to recursively apply the rule
* @default 10
*/
recursive?: number | false;
/**
* Run verification after applying the fix
* @default true
*/
verifyAfterFix?: boolean;
/**
* Verify that fix allways changes the code
* @default true
*/
verifyFixChanges?: boolean;
}
interface DefaultFilenames {
js: string;
jsx: string;
ts: string;
tsx: string;
}
interface RuleTesterInitOptions extends CompatConfigOptions, RuleTesterBehaviorOptions {
/**
* The rule to test
*/
rule?: RuleModule;
/**
* The name of the rule to test
*/
name?: string;
/**
* Additional flat configs to be merged with the rule config
*/
configs?: Linter.Config | Linter.Config[];
/**
* The default filenames to use for type-aware tests.
* @default { js: 'file.js', jsx: 'file.jsx', ts: 'file.ts', tsx: 'file.tsx' }
*/
defaultFilenames?: Partial<DefaultFilenames>;
}
interface TestCasesOptions {
valid?: (ValidTestCase | string)[];
invalid?: (InvalidTestCase | string)[];
/**
* Callback to be called after each test case
*/
onResult?: (_case: NormalizedTestCase, result: Linter.FixReport) => void | Promise<void>;
}
declare function pickFlatConfigFromOptions(options: CompatConfigOptions): Linter.Config | undefined;
declare function createRuleTester(options: RuleTesterInitOptions): RuleTester;
/**
* Shortcut to run test cases for a rule
*/
declare function run(options: TestCasesOptions & RuleTesterInitOptions): void;
/**
* Shortcut to run test cases for a rule in classic style
*/
declare function runClassic(ruleName: string, rule: RuleModule, cases: TestCasesOptions, options?: RuleTesterInitOptions): void;
declare function normalizeTestCase(c: TestCase, languageOptions: Linter.Config['languageOptions'], defaultFilenames: DefaultFilenames, type?: 'valid' | 'invalid'): NormalizedTestCase;
declare function normalizeCaseError(error: TestCaseError | string, rule?: RuleModule): Partial<Linter.LintMessage>;
declare function isUsingTypeScriptParser(languageOptions: Linter.Config['languageOptions']): boolean;
declare function isUsingTypeScriptTypings(languageOptions: Linter.Config['languageOptions']): boolean;
export { type CompatConfigOptions, type DefaultFilenames, type InvalidTestCase, type InvalidTestCaseBase, type NormalizedTestCase, type RuleModule, type RuleTester, type RuleTesterBehaviorOptions, type RuleTesterInitOptions, type TestCase, type TestCaseError, type TestCasesOptions, type TestExecutionResult, type ValidTestCase, type ValidTestCaseBase, createRuleTester, isUsingTypeScriptParser, isUsingTypeScriptTypings, normalizeCaseError, normalizeTestCase, pickFlatConfigFromOptions, run, runClassic };