UNPKG

5.35 kBTypeScriptView Raw
1import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '../ts-estree';
2import { ParserOptions } from './ParserOptions';
3import { Linter } from './Linter';
4import { RuleCreateFunction, RuleModule, SharedConfigurationSettings } from './Rule';
5interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
6 /**
7 * Code for the test case.
8 */
9 readonly code: string;
10 /**
11 * Environments for the test case.
12 */
13 readonly env?: Readonly<Record<string, boolean>>;
14 /**
15 * The fake filename for the test case. Useful for rules that make assertion about filenames.
16 */
17 readonly filename?: string;
18 /**
19 * The additional global variables.
20 */
21 readonly globals?: Record<string, 'readonly' | 'writable' | 'off' | true>;
22 /**
23 * Options for the test case.
24 */
25 readonly options?: Readonly<TOptions>;
26 /**
27 * The absolute path for the parser.
28 */
29 readonly parser?: string;
30 /**
31 * Options for the parser.
32 */
33 readonly parserOptions?: Readonly<ParserOptions>;
34 /**
35 * Settings for the test case.
36 */
37 readonly settings?: Readonly<SharedConfigurationSettings>;
38 /**
39 * Run this case exclusively for debugging in supported test frameworks.
40 */
41 readonly only?: boolean;
42}
43interface SuggestionOutput<TMessageIds extends string> {
44 /**
45 * Reported message ID.
46 */
47 readonly messageId: TMessageIds;
48 /**
49 * The data used to fill the message template.
50 */
51 readonly data?: Readonly<Record<string, unknown>>;
52 /**
53 * NOTE: Suggestions will be applied as a stand-alone change, without triggering multi-pass fixes.
54 * Each individual error has its own suggestion, so you have to show the correct, _isolated_ output for each suggestion.
55 */
56 readonly output: string;
57}
58interface InvalidTestCase<TMessageIds extends string, TOptions extends Readonly<unknown[]>> extends ValidTestCase<TOptions> {
59 /**
60 * Expected errors.
61 */
62 readonly errors: readonly TestCaseError<TMessageIds>[];
63 /**
64 * The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
65 */
66 readonly output?: string | null;
67}
68interface TestCaseError<TMessageIds extends string> {
69 /**
70 * The 1-based column number of the reported start location.
71 */
72 readonly column?: number;
73 /**
74 * The data used to fill the message template.
75 */
76 readonly data?: Readonly<Record<string, unknown>>;
77 /**
78 * The 1-based column number of the reported end location.
79 */
80 readonly endColumn?: number;
81 /**
82 * The 1-based line number of the reported end location.
83 */
84 readonly endLine?: number;
85 /**
86 * The 1-based line number of the reported start location.
87 */
88 readonly line?: number;
89 /**
90 * Reported message ID.
91 */
92 readonly messageId: TMessageIds;
93 /**
94 * Reported suggestions.
95 */
96 readonly suggestions?: SuggestionOutput<TMessageIds>[] | null;
97 /**
98 * The type of the reported AST node.
99 */
100 readonly type?: AST_NODE_TYPES | AST_TOKEN_TYPES;
101}
102interface RunTests<TMessageIds extends string, TOptions extends Readonly<unknown[]>> {
103 readonly valid: readonly (ValidTestCase<TOptions> | string)[];
104 readonly invalid: readonly InvalidTestCase<TMessageIds, TOptions>[];
105}
106interface RuleTesterConfig extends Linter.Config {
107 readonly parser: string;
108 readonly parserOptions?: Readonly<ParserOptions>;
109}
110declare class RuleTesterBase {
111 /**
112 * Creates a new instance of RuleTester.
113 * @param testerConfig extra configuration for the tester
114 */
115 constructor(testerConfig?: RuleTesterConfig);
116 /**
117 * Adds a new rule test to execute.
118 * @param ruleName The name of the rule to run.
119 * @param rule The rule to test.
120 * @param test The collection of tests to run.
121 */
122 run<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(ruleName: string, rule: RuleModule<TMessageIds, TOptions>, tests: RunTests<TMessageIds, TOptions>): void;
123 /**
124 * If you supply a value to this property, the rule tester will call this instead of using the version defined on
125 * the global namespace.
126 * @param text a string describing the rule
127 * @param callback the test callback
128 */
129 static describe?: (text: string, callback: () => void) => void;
130 /**
131 * If you supply a value to this property, the rule tester will call this instead of using the version defined on
132 * the global namespace.
133 * @param text a string describing the test case
134 * @param callback the test callback
135 */
136 static it?: (text: string, callback: () => void) => void;
137 /**
138 * Define a rule for one particular run of tests.
139 * @param name The name of the rule to define.
140 * @param rule The rule definition.
141 */
142 defineRule<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(name: string, rule: RuleModule<TMessageIds, TOptions> | RuleCreateFunction<TMessageIds, TOptions>): void;
143}
144declare const RuleTester_base: typeof RuleTesterBase;
145declare class RuleTester extends RuleTester_base {
146}
147export { InvalidTestCase, SuggestionOutput, RuleTester, RuleTesterConfig, RunTests, TestCaseError, ValidTestCase, };
148//# sourceMappingURL=RuleTester.d.ts.map
\No newline at end of file