-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
RuleTester.ts
199 lines (185 loc) · 5.55 KB
/
RuleTester.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { RuleTester as ESLintRuleTester } from 'eslint';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '../ts-estree';
import { Linter } from './Linter';
import { ParserOptions } from './ParserOptions';
import {
RuleCreateFunction,
RuleModule,
SharedConfigurationSettings,
} from './Rule';
interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
/**
* Name for the test case.
* @since 8.1.0
*/
readonly name?: string;
/**
* Code for the test case.
*/
readonly code: string;
/**
* Environments for the test case.
*/
readonly env?: Readonly<Record<string, boolean>>;
/**
* The fake filename for the test case. Useful for rules that make assertion about filenames.
*/
readonly filename?: string;
/**
* The additional global variables.
*/
readonly globals?: Record<string, 'readonly' | 'writable' | 'off' | true>;
/**
* Options for the test case.
*/
readonly options?: Readonly<TOptions>;
/**
* 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>;
/**
* Run this case exclusively for debugging in supported test frameworks.
* @since 7.29.0
*/
readonly only?: boolean;
}
interface SuggestionOutput<TMessageIds extends string> {
/**
* Reported message ID.
*/
readonly messageId: TMessageIds;
/**
* The data used to fill the message template.
*/
readonly data?: Readonly<Record<string, unknown>>;
/**
* 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;
}
interface InvalidTestCase<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
> extends ValidTestCase<TOptions> {
/**
* Expected errors.
*/
readonly errors: readonly TestCaseError<TMessageIds>[];
/**
* The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
*/
readonly output?: string | null;
}
interface TestCaseError<TMessageIds 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?: Readonly<Record<string, unknown>>;
/**
* 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: TMessageIds;
/**
* Reported suggestions.
*/
readonly suggestions?: readonly SuggestionOutput<TMessageIds>[] | 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;
}
interface RunTests<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
> {
// RuleTester.run also accepts strings for valid cases
readonly valid: readonly (ValidTestCase<TOptions> | string)[];
readonly invalid: readonly InvalidTestCase<TMessageIds, TOptions>[];
}
interface RuleTesterConfig extends Linter.Config {
// should be require.resolve(parserPackageName)
readonly parser: string;
readonly parserOptions?: Readonly<ParserOptions>;
}
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 test The collection of tests to run.
*/
run<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
ruleName: string,
rule: RuleModule<TMessageIds, TOptions>,
tests: RunTests<TMessageIds, TOptions>,
): 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.
* @param text a string describing the rule
* @param callback the test callback
*/
static describe?: (text: string, callback: () => void) => 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.
* @param text a string describing the test case
* @param callback the test callback
*/
static it?: (text: string, callback: () => void) => void;
/**
* Define a rule for one particular run of tests.
* @param name The name of the rule to define.
* @param rule The rule definition.
*/
defineRule<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
name: string,
rule:
| RuleModule<TMessageIds, TOptions>
| RuleCreateFunction<TMessageIds, TOptions>,
): void;
}
class RuleTester extends (ESLintRuleTester as typeof RuleTesterBase) {}
export {
InvalidTestCase,
SuggestionOutput,
RuleTester,
RuleTesterConfig,
RunTests,
TestCaseError,
ValidTestCase,
};