@@ -5,8 +5,17 @@ import {
55} from '../../../lib/utils' ;
66import { createRuleTester } from '../test-utils' ;
77
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+
814const ruleTester = createRuleTester ( ) ;
915
16+ type RuleValidTestCase = ValidTestCase < [ ] > ;
17+ type RuleInvalidTestCase = InvalidTestCase < MessageIds , [ ] > ;
18+
1019const SUPPORTED_TESTING_FRAMEWORKS = [
1120 '@testing-library/dom' ,
1221 '@testing-library/angular' ,
@@ -18,7 +27,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
1827ruleTester . run ( RULE_NAME , rule , {
1928 valid : [
2029 // sync queries without await are valid
21- ...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
30+ ...SYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
2231 code : `() => {
2332 const element = ${ query } ('foo')
2433 }
@@ -61,23 +70,23 @@ ruleTester.run(RULE_NAME, rule, {
6170 } ,
6271
6372 // sync queries without await inside assert are valid
64- ...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
73+ ...SYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
6574 code : `() => {
6675 expect(${ query } ('foo')).toBeEnabled()
6776 }
6877 ` ,
6978 } ) ) ,
7079
7180 // async queries with await operator are valid
72- ...ASYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
81+ ...ASYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
7382 code : `async () => {
7483 const element = await ${ query } ('foo')
7584 }
7685 ` ,
7786 } ) ) ,
7887
7988 // async queries with then method are valid
80- ...ASYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
89+ ...ASYNC_QUERIES_COMBINATIONS . map < RuleValidTestCase > ( ( query ) => ( {
8190 code : `() => {
8291 ${ query } ('foo').then(() => {});
8392 }
@@ -128,22 +137,19 @@ ruleTester.run(RULE_NAME, rule, {
128137
129138 invalid : [
130139 // 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 () => {
135142 const element = await ${ query } ('foo')
136143 }
137144 ` ,
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+ } ) ) ,
147153 // custom sync queries with await operator are not valid
148154 {
149155 code : `
@@ -178,73 +184,63 @@ ruleTester.run(RULE_NAME, rule, {
178184 errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 38 } ] ,
179185 } ,
180186 // 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 () => {
185189 expect(await ${ query } ('foo')).toBeEnabled()
186190 }
187191 ` ,
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+ } ) ) ,
197200
198201 // 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 () => {
203204 const element = await screen.${ query } ('foo')
204205 }
205206 ` ,
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+ } ) ) ,
215215
216216 // 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 () => {
221219 expect(await screen.${ query } ('foo')).toBeEnabled()
222220 }
223221 ` ,
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+ } ) ) ,
233230
234231 // sync query awaited and related to testing library module
235232 // 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 : `
241237 import { screen } from '${ testingFramework } '
242238 () => {
243239 const element = await screen.getByRole('button')
244240 }
245241 ` ,
246- errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
247- } ) as const
242+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
243+ } )
248244 ) ,
249245 // sync query awaited and related to custom module is not valid
250246 {
0 commit comments