8
8
const ruleTester = createRuleTester ( ) ;
9
9
10
10
ruleTester . run ( RULE_NAME , rule , {
11
- // TODO: add variants for custom queries for each map
12
11
valid : [
13
12
// sync queries without await are valid
14
13
...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
@@ -17,6 +16,23 @@ ruleTester.run(RULE_NAME, rule, {
17
16
}
18
17
` ,
19
18
} ) ) ,
19
+ // custom sync queries without await are valid
20
+ `() => {
21
+ const element = getByIcon('search')
22
+ }
23
+ ` ,
24
+ `() => {
25
+ const element = queryByIcon('search')
26
+ }
27
+ ` ,
28
+ `() => {
29
+ const element = getAllByIcon('search')
30
+ }
31
+ ` ,
32
+ `() => {
33
+ const element = queryAllByIcon('search')
34
+ }
35
+ ` ,
20
36
// sync queries without await inside assert are valid
21
37
...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
22
38
code : `() => {
@@ -40,9 +56,28 @@ ruleTester.run(RULE_NAME, rule, {
40
56
}
41
57
` ,
42
58
} ) ) ,
59
+
60
+ // sync query awaited but not related to custom module is invalid but not reported
61
+ {
62
+ settings : { 'testing-library/module' : 'test-utils' } ,
63
+ code : `
64
+ import { screen } from 'somewhere-else'
65
+ () => {
66
+ const element = await screen.getByRole('button')
67
+ }
68
+ ` ,
69
+ } ,
70
+ // sync query awaited but not matching filename pattern is invalid but not reported
71
+ {
72
+ settings : { 'testing-library/filename-pattern' : '^.*\\.(nope)\\.js$' } ,
73
+ code : `
74
+ () => {
75
+ const element = await getByRole('button')
76
+ }
77
+ ` ,
78
+ } ,
43
79
] ,
44
80
45
- // TODO: add variants for custom queries for each map
46
81
invalid : [
47
82
// sync queries with await operator are not valid
48
83
...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
@@ -58,6 +93,39 @@ ruleTester.run(RULE_NAME, rule, {
58
93
} ,
59
94
] ,
60
95
} ) ) ,
96
+ // custom sync queries with await operator are not valid
97
+ {
98
+ code : `
99
+ async () => {
100
+ const element = await getByIcon('search')
101
+ }
102
+ ` ,
103
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 31 } ] ,
104
+ } ,
105
+ {
106
+ code : `
107
+ async () => {
108
+ const element = await queryByIcon('search')
109
+ }
110
+ ` ,
111
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 31 } ] ,
112
+ } ,
113
+ {
114
+ code : `
115
+ async () => {
116
+ const element = await screen.getAllByIcon('search')
117
+ }
118
+ ` ,
119
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 38 } ] ,
120
+ } ,
121
+ {
122
+ code : `
123
+ async () => {
124
+ const element = await screen.queryAllByIcon('search')
125
+ }
126
+ ` ,
127
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 3 , column : 38 } ] ,
128
+ } ,
61
129
// sync queries with await operator inside assert are not valid
62
130
...SYNC_QUERIES_COMBINATIONS . map ( ( query ) => ( {
63
131
code : `async () => {
@@ -102,5 +170,29 @@ ruleTester.run(RULE_NAME, rule, {
102
170
} ,
103
171
] ,
104
172
} ) ) ,
173
+
174
+ // sync query awaited and related to testing library module
175
+ // with custom module setting is not valid
176
+ {
177
+ settings : { 'testing-library/module' : 'test-utils' } ,
178
+ code : `
179
+ import { screen } from '@testing-library/react'
180
+ () => {
181
+ const element = await screen.getByRole('button')
182
+ }
183
+ ` ,
184
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
185
+ } ,
186
+ // sync query awaited and related to custom module is not valid
187
+ {
188
+ settings : { 'testing-library/module' : 'test-utils' } ,
189
+ code : `
190
+ import { screen } from 'test-utils'
191
+ () => {
192
+ const element = await screen.getByRole('button')
193
+ }
194
+ ` ,
195
+ errors : [ { messageId : 'noAwaitSyncQuery' , line : 4 , column : 38 } ] ,
196
+ } ,
105
197
] ,
106
198
} ) ;
0 commit comments