1
1
import type { Linter } from 'eslint'
2
2
3
3
export type Awaitable < T > = Promise < T > | T
4
- export interface ValidTestCaseBase < RuleOptions = any > extends CompatConfigOptions , RuleTesterBehaviorOptions {
4
+ export interface ValidTestCaseBase < RuleOptions = any , MessageId extends string = string > extends CompatConfigOptions , RuleTesterBehaviorOptions {
5
5
name ?: string
6
6
description ?: string
7
7
code : string
8
8
options ?: RuleOptions
9
9
filename ?: string
10
10
only ?: boolean
11
11
skip ?: boolean
12
- before ?: ( this : NormalizedTestCase , configs : Linter . Config [ ] ) => Awaitable < void >
13
- after ?: ( this : NormalizedTestCase , result : Linter . FixReport ) => Awaitable < void >
12
+ before ?: ( this : NormalizedTestCase < RuleOptions , MessageId > , configs : Linter . Config [ ] ) => Awaitable < void >
13
+ after ?: ( this : NormalizedTestCase < RuleOptions , MessageId > , result : Linter . FixReport ) => Awaitable < void >
14
14
15
15
/**
16
16
* @deprecated Use `after` instead
17
17
*/
18
18
onResult ?: ( result : Linter . FixReport ) => void
19
19
}
20
20
21
- export interface TestCaseError extends Partial < Linter . LintMessage > {
21
+ export type TestCaseError < MessageId extends string = string > = Partial < Linter . LintMessage > & {
22
22
/**
23
23
* Data for interpolate the error message
24
24
*/
@@ -27,32 +27,33 @@ export interface TestCaseError extends Partial<Linter.LintMessage> {
27
27
* Alias to `nodeType`
28
28
*/
29
29
type ?: string
30
+ messageId ?: MessageId
30
31
}
31
32
32
- export interface InvalidTestCaseBase < RuleOptions = any > extends ValidTestCaseBase < RuleOptions > {
33
+ export interface InvalidTestCaseBase < RuleOptions = any , MessageId extends string = string > extends ValidTestCaseBase < RuleOptions , MessageId > {
33
34
/**
34
35
* Expected errors.
35
36
* If a number is provided, it asserts that the number of errors is equal to the number provided.
36
37
* If an array of strings is provided, it asserts that the error messageIds are equal to the array provided.
37
38
* If an array of objects is provided, it asserts that the errors are partially equal to the objects provided.
38
39
*/
39
- errors ?: number | ( string | TestCaseError ) [ ] | ( ( errors : Linter . LintMessage [ ] ) => Awaitable < void > )
40
+ errors ?: number | ( MessageId | TestCaseError < MessageId > ) [ ] | ( ( errors : Linter . LintMessage [ ] ) => Awaitable < void > )
40
41
/**
41
42
* Assert if output is expected.
42
43
* Pass `null` to assert that the output is the same as the input.
43
44
*/
44
45
output ?: string | null | ( ( output : string , input : string ) => Awaitable < void > )
45
46
}
46
47
47
- export interface NormalizedTestCase extends InvalidTestCaseBase {
48
+ export interface NormalizedTestCase < RuleOptions = any , MessageId extends string = string > extends InvalidTestCaseBase < RuleOptions , MessageId > {
48
49
type : 'valid' | 'invalid'
49
50
code : string
50
51
}
51
52
52
- export type InvalidTestCase < RuleOptions = any > = InvalidTestCaseBase < RuleOptions > | string
53
+ export type InvalidTestCase < RuleOptions = any , MessageId extends string = string > = InvalidTestCaseBase < RuleOptions , MessageId > | string
53
54
export type ValidTestCase < RuleOptions = any > = ValidTestCaseBase < RuleOptions > | string
54
55
55
- export type TestCase < RuleOptions = any > = ValidTestCase < RuleOptions > | InvalidTestCase < RuleOptions >
56
+ export type TestCase < RuleOptions = any , MessageId extends string = string > = ValidTestCase < RuleOptions > | InvalidTestCase < RuleOptions , MessageId >
56
57
57
58
export interface TestExecutionResult extends Linter . FixReport {
58
59
/**
@@ -73,23 +74,23 @@ export interface CompatConfigOptions {
73
74
74
75
export type RuleModule = any // to allow any rule module
75
76
76
- export interface RuleTester < RuleOptions = any > {
77
+ export interface RuleTester < RuleOptions = any , MessageId extends string = string > {
77
78
/**
78
79
* Run a single test case
79
80
*/
80
- each : ( arg : TestCase < RuleOptions > ) => Promise < { testcase : NormalizedTestCase , result : TestExecutionResult } >
81
+ each : ( arg : TestCase < RuleOptions , MessageId > ) => Promise < { testcase : NormalizedTestCase < RuleOptions , MessageId > , result : TestExecutionResult } >
81
82
/**
82
83
* Run a single valid test case
83
84
*/
84
- valid : ( arg : ValidTestCase < RuleOptions > ) => Promise < { testcase : NormalizedTestCase , result : TestExecutionResult } >
85
+ valid : ( arg : ValidTestCase < RuleOptions > ) => Promise < { testcase : NormalizedTestCase < RuleOptions , MessageId > , result : TestExecutionResult } >
85
86
/**
86
87
* Run a single invalid test case
87
88
*/
88
- invalid : ( arg : InvalidTestCase < RuleOptions > ) => Promise < { testcase : NormalizedTestCase , result : TestExecutionResult } >
89
+ invalid : ( arg : InvalidTestCase < RuleOptions , MessageId > ) => Promise < { testcase : NormalizedTestCase < RuleOptions , MessageId > , result : TestExecutionResult } >
89
90
/**
90
91
* ESLint's RuleTester style test runner, that runs multiple test cases
91
92
*/
92
- run : ( options : TestCasesOptions < RuleOptions > ) => Promise < void >
93
+ run : ( options : TestCasesOptions < RuleOptions , MessageId > ) => Promise < void >
93
94
}
94
95
95
96
export interface RuleTesterBehaviorOptions {
@@ -137,11 +138,11 @@ export interface RuleTesterInitOptions extends CompatConfigOptions, RuleTesterBe
137
138
defaultFilenames ?: Partial < DefaultFilenames >
138
139
}
139
140
140
- export interface TestCasesOptions < RuleOptions = any > {
141
+ export interface TestCasesOptions < RuleOptions = any , MessageId extends string = string > {
141
142
valid ?: ( ValidTestCase < RuleOptions > | string ) [ ]
142
- invalid ?: ( InvalidTestCase < RuleOptions > | string ) [ ]
143
+ invalid ?: ( InvalidTestCase < RuleOptions , MessageId > | string ) [ ]
143
144
/**
144
145
* Callback to be called after each test case
145
146
*/
146
- onResult ?: ( _case : NormalizedTestCase , result : Linter . FixReport ) => void | Promise < void >
147
+ onResult ?: ( _case : NormalizedTestCase < RuleOptions , MessageId > , result : Linter . FixReport ) => void | Promise < void >
147
148
}
0 commit comments