Skip to content

Commit 79973d5

Browse files
committed
test(no-await-sync-query): add more cases for custom queries and settings
1 parent 1364182 commit 79973d5

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

tests/lib/rules/no-await-sync-query.test.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
const ruleTester = createRuleTester();
99

1010
ruleTester.run(RULE_NAME, rule, {
11-
// TODO: add variants for custom queries for each map
1211
valid: [
1312
// sync queries without await are valid
1413
...SYNC_QUERIES_COMBINATIONS.map((query) => ({
@@ -17,6 +16,23 @@ ruleTester.run(RULE_NAME, rule, {
1716
}
1817
`,
1918
})),
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+
`,
2036
// sync queries without await inside assert are valid
2137
...SYNC_QUERIES_COMBINATIONS.map((query) => ({
2238
code: `() => {
@@ -40,9 +56,28 @@ ruleTester.run(RULE_NAME, rule, {
4056
}
4157
`,
4258
})),
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+
},
4379
],
4480

45-
// TODO: add variants for custom queries for each map
4681
invalid: [
4782
// sync queries with await operator are not valid
4883
...SYNC_QUERIES_COMBINATIONS.map((query) => ({
@@ -58,6 +93,39 @@ ruleTester.run(RULE_NAME, rule, {
5893
},
5994
],
6095
})),
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+
},
61129
// sync queries with await operator inside assert are not valid
62130
...SYNC_QUERIES_COMBINATIONS.map((query) => ({
63131
code: `async () => {
@@ -102,5 +170,29 @@ ruleTester.run(RULE_NAME, rule, {
102170
},
103171
],
104172
})),
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+
},
105197
],
106198
});

0 commit comments

Comments
 (0)