Skip to content

Commit 8bd20d6

Browse files
committed
fix(await-async-query): check queries being called as member expressions
First, abstract a isQueryUsage function to check if a node should be checked for this rule. Then, add new selector for queries being called as part of MemberExpression and check their parent against the rule. Finally, use the queryName prop to make sure the right error message is printed
1 parent 32aede1 commit 8bd20d6

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/rules/await-async-query.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,26 @@ module.exports = {
2828

2929
create: function(context) {
3030
const testingLibraryQueryUsage = [];
31+
const isQueryUsage = node =>
32+
!isAwaited(node.parent.parent) &&
33+
!isPromiseResolved(node) &&
34+
!hasClosestExpectResolvesRejects(node);
35+
3136
return {
3237
[`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
33-
if (
34-
!isAwaited(node.parent.parent) &&
35-
!isPromiseResolved(node) &&
36-
!hasClosestExpectResolvesRejects(node)
37-
) {
38-
testingLibraryQueryUsage.push(node);
38+
if (isQueryUsage(node)) {
39+
testingLibraryQueryUsage.push({ node, queryName: node.name });
40+
}
41+
},
42+
[`MemberExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
43+
// Perform checks in parent MemberExpression insted of current identifier
44+
const parent = node.parent;
45+
if (isQueryUsage(parent)) {
46+
testingLibraryQueryUsage.push({ node: parent, queryName: node.name });
3947
}
4048
},
4149
'Program:exit'() {
42-
testingLibraryQueryUsage.forEach(node => {
50+
testingLibraryQueryUsage.forEach(({ node, queryName }) => {
4351
const variableDeclaratorParent = node.parent.parent;
4452

4553
const references =
@@ -54,7 +62,7 @@ module.exports = {
5462
node,
5563
messageId: 'awaitAsyncQuery',
5664
data: {
57-
name: node.name,
65+
name: queryName,
5866
},
5967
});
6068
} else {
@@ -68,7 +76,7 @@ module.exports = {
6876
node,
6977
messageId: 'awaitAsyncQuery',
7078
data: {
71-
name: node.name,
79+
name: queryName,
7280
},
7381
});
7482

tests/lib/rules/await-async-query.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ ruleTester.run('await-async-query', rule, {
151151
},
152152
],
153153
})),
154+
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
155+
code: `async () => {
156+
screen.${query}('foo')
157+
}
158+
`,
159+
errors: [
160+
{
161+
messageId: 'awaitAsyncQuery',
162+
},
163+
],
164+
})),
165+
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
166+
code: `const foo = screen.${query}('foo')`,
167+
errors: [
168+
{
169+
messageId: 'awaitAsyncQuery',
170+
},
171+
],
172+
})),
154173
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
155174
code: `async () => {
156175
const foo = ${query}('foo')

0 commit comments

Comments
 (0)