@@ -5,8 +5,17 @@ import {
5
5
} from '../../../lib/utils' ;
6
6
import { createRuleTester } from '../test-utils' ;
7
7
8
+ import type { MessageIds } from '../../../lib/rules/no-await-sync-queries' ;
9
+ import type {
10
+ InvalidTestCase ,
11
+ ValidTestCase ,
12
+ } from '@typescript-eslint/rule-tester' ;
13
+
8
14
const ruleTester = createRuleTester ( ) ;
9
15
16
+ type RuleValidTestCase = ValidTestCase < [ ] > ;
17
+ type RuleInvalidTestCase = InvalidTestCase < MessageIds , [ ] > ;
18
+
10
19
const SUPPORTED_TESTING_FRAMEWORKS = [
11
20
'@testing-library/dom' ,
12
21
'@testing-library/angular' ,
@@ -18,7 +27,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
18
27
ruleTester . run ( RULE_NAME , rule , {
19
28
valid : [
20
29
// sync queries without await are valid
21
- ...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
30
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
22
31
code : `() => {
23
32
const element = ${ query } ('foo')
24
33
}
@@ -61,23 +70,23 @@ ruleTester.run(RULE_NAME, rule, {
61
70
} ,
62
71
63
72
// sync queries without await inside assert are valid
64
- ...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
73
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
65
74
code : `() => {
66
75
expect(${ query } ('foo')).toBeEnabled()
67
76
}
68
77
` ,
69
78
} ) ) ,
70
79
71
80
// async queries with await operator are valid
72
- ...ASYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
81
+ ...ASYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
73
82
code : `async () => {
74
83
const element = await ${ query } ('foo')
75
84
}
76
85
` ,
77
86
} ) ) ,
78
87
79
88
// async queries with then method are valid
80
- ...ASYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
89
+ ...ASYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
81
90
code : `() => {
82
91
${ query } ('foo').then(() => {});
83
92
}
@@ -128,22 +137,19 @@ ruleTester.run(RULE_NAME, rule, {
128
137
129
138
invalid : [
130
139
// sync queries with await operator are not valid
131
- ...SYNC_QUERIES_COMBINATIONS . map (
132
- ( query ) =>
133
- ( {
134
- code : `async () => {
140
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleInvalidTestCase > ( ( query ) => ( {
141
+ code : `async () => {
135
142
const element = await ${ query } ('foo')
136
143
}
137
144
` ,
138
- errors : [
139
- {
140
- messageId : 'noAwaitSyncQuery' ,
141
- line : 2 ,
142
- column : 31 ,
143
- } ,
144
- ] ,
145
- } ) as const
146
- ) ,
145
+ errors : [
146
+ {
147
+ messageId : 'noAwaitSyncQuery' ,
148
+ line : 2 ,
149
+ column : 31 ,
150
+ } ,
151
+ ] ,
152
+ } ) ) ,
147
153
// custom sync queries with await operator are not valid
148
154
{
149
155
code : `
@@ -178,73 +184,63 @@ ruleTester.run(RULE_NAME, rule, {
178
184
errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 38 } ] ,
179
185
} ,
180
186
// sync queries with await operator inside assert are not valid
181
- ...SYNC_QUERIES_COMBINATIONS . map (
182
- ( query ) =>
183
- ( {
184
- code : `async () => {
187
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleInvalidTestCase > ( ( query ) => ( {
188
+ code : `async () => {
185
189
expect(await ${ query } ('foo')).toBeEnabled()
186
190
}
187
191
` ,
188
- errors : [
189
- {
190
- messageId : 'noAwaitSyncQuery' ,
191
- line : 2 ,
192
- column : 22 ,
193
- } ,
194
- ] ,
195
- } ) as const
196
- ) ,
192
+ errors : [
193
+ {
194
+ messageId : 'noAwaitSyncQuery' ,
195
+ line : 2 ,
196
+ column : 22 ,
197
+ } ,
198
+ ] ,
199
+ } ) ) ,
197
200
198
201
// sync queries in screen with await operator are not valid
199
- ...SYNC_QUERIES_COMBINATIONS . map (
200
- ( query ) =>
201
- ( {
202
- code : `async () => {
202
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleInvalidTestCase > ( ( query ) => ( {
203
+ code : `async () => {
203
204
const element = await screen.${ query } ('foo')
204
205
}
205
206
` ,
206
- errors : [
207
- {
208
- messageId : 'noAwaitSyncQuery' ,
209
- line : 2 ,
210
- column : 38 ,
211
- } ,
212
- ] ,
213
- } ) as const
214
- ) ,
207
+ errors : [
208
+ {
209
+ messageId : 'noAwaitSyncQuery' ,
210
+ line : 2 ,
211
+ column : 38 ,
212
+ } ,
213
+ ] ,
214
+ } ) ) ,
215
215
216
216
// sync queries in screen with await operator inside assert are not valid
217
- ...SYNC_QUERIES_COMBINATIONS . map (
218
- ( query ) =>
219
- ( {
220
- code : `async () => {
217
+ ...SYNC_QUERIES_COMBINATIONS . map < RuleInvalidTestCase > ( ( query ) => ( {
218
+ code : `async () => {
221
219
expect(await screen.${ query } ('foo')).toBeEnabled()
222
220
}
223
221
` ,
224
- errors : [
225
- {
226
- messageId : 'noAwaitSyncQuery' ,
227
- line : 2 ,
228
- column : 29 ,
229
- } ,
230
- ] ,
231
- } ) as const
232
- ) ,
222
+ errors : [
223
+ {
224
+ messageId : 'noAwaitSyncQuery' ,
225
+ line : 2 ,
226
+ column : 29 ,
227
+ } ,
228
+ ] ,
229
+ } ) ) ,
233
230
234
231
// sync query awaited and related to testing library module
235
232
// with custom module setting is not valid
236
- ...SUPPORTED_TESTING_FRAMEWORKS . map (
237
- ( testingFramework ) =>
238
- ( {
239
- settings : { 'testing-library/utils-module' : 'test-utils' } ,
240
- code : `
233
+ ...SUPPORTED_TESTING_FRAMEWORKS . map < RuleInvalidTestCase > (
234
+ ( testingFramework ) => ( {
235
+ settings : { 'testing-library/utils-module' : 'test-utils' } ,
236
+ code : `
241
237
import { screen } from '${ testingFramework } '
242
238
() => {
243
239
const element = await screen.getByRole('button')
244
240
}
245
241
` ,
246
- errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
247
- } ) as const
242
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
243
+ } )
248
244
) ,
249
245
// sync query awaited and related to custom module is not valid
250
246
{
0 commit comments