Skip to content

Pr/fix async checks when calling queries member expressions #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/rules/await-async-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const bar = () => {
findByText('submit');
// ...
};

const baz = () => {
// ...
screen.findAllByPlaceholderText('name');
// ...
};
```

Examples of **correct** code for this rule:
Expand All @@ -50,6 +56,12 @@ const bar = () => {
});
};

const baz = () => {
// ...
await screen.findAllByPlaceholderText('name');
// ...
};

// return the promise within a function is correct too!
const findMyButton = () => findByText('my button');

Expand Down
10 changes: 10 additions & 0 deletions docs/rules/no-await-sync-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const bar = () => {
// ...
});
};

const baz = () => {
// ...
const button = await screen.getByText('submit');
};
```

Examples of **correct** code for this rule:
Expand All @@ -45,6 +50,11 @@ const bar = () => {
const button = getByText('submit');
// ...
};

const baz = () => {
// ...
const button = screen.getByText('submit');
};
```

## Further Reading
Expand Down
33 changes: 18 additions & 15 deletions lib/rules/await-async-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ module.exports = {

create: function(context) {
const testingLibraryQueryUsage = [];
const isQueryUsage = node =>
!isAwaited(node.parent.parent) &&
!isPromiseResolved(node) &&
!hasClosestExpectResolvesRejects(node);

return {
[`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
if (
!isAwaited(node.parent.parent) &&
!isPromiseResolved(node) &&
!hasClosestExpectResolvesRejects(node)
) {
testingLibraryQueryUsage.push(node);
if (isQueryUsage(node)) {
testingLibraryQueryUsage.push({ node, queryName: node.name });
}
},
[`MemberExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
// Perform checks in parent MemberExpression insted of current identifier
const parent = node.parent;
if (isQueryUsage(parent)) {
testingLibraryQueryUsage.push({ node: parent, queryName: node.name });
}
},
'Program:exit'() {
testingLibraryQueryUsage.forEach(node => {
testingLibraryQueryUsage.forEach(({ node, queryName }) => {
const variableDeclaratorParent = node.parent.parent;

const references =
Expand All @@ -49,17 +57,12 @@ module.exports = {
.references.slice(1)) ||
[];

if (
references &&
references.length === 0 &&
!isAwaited(node.parent.parent) &&
!isPromiseResolved(node)
) {
if (references && references.length === 0) {
context.report({
node,
messageId: 'awaitAsyncQuery',
data: {
name: node.name,
name: queryName,
},
});
} else {
Expand All @@ -73,7 +76,7 @@ module.exports = {
node,
messageId: 'awaitAsyncQuery',
data: {
name: node.name,
name: queryName,
},
});

Expand Down
21 changes: 10 additions & 11 deletions lib/rules/no-await-sync-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ module.exports = {
},

create: function(context) {
const reportError = node =>
context.report({
node,
messageId: 'noAwaitSyncQuery',
data: {
name: node.name,
},
});
return {
[`AwaitExpression > CallExpression > Identifier[name=${SYNC_QUERIES_REGEXP}]`](
node
) {
context.report({
node,
messageId: 'noAwaitSyncQuery',
data: {
name: node.name,
},
});
},
[`AwaitExpression > CallExpression > Identifier[name=${SYNC_QUERIES_REGEXP}]`]: reportError,
[`AwaitExpression > CallExpression > MemberExpression > Identifier[name=${SYNC_QUERIES_REGEXP}]`]: reportError,
};
},
};
27 changes: 27 additions & 0 deletions tests/lib/rules/await-async-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ ruleTester.run('await-async-query', rule, {
`,
})),

// async queries declaration are valid
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
await screen.${query}('foo')
}
`,
})),

// async queries with await operator are valid
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
Expand Down Expand Up @@ -151,6 +159,25 @@ ruleTester.run('await-async-query', rule, {
},
],
})),
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
screen.${query}('foo')
}
`,
errors: [
{
messageId: 'awaitAsyncQuery',
},
],
})),
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
code: `const foo = screen.${query}('foo')`,
errors: [
{
messageId: 'awaitAsyncQuery',
},
],
})),
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
const foo = ${query}('foo')
Expand Down
18 changes: 16 additions & 2 deletions tests/lib/rules/no-await-sync-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ ruleTester.run('no-await-sync-query', rule, {
})),
],

invalid:
invalid: [
// sync queries with await operator are not valid
SYNC_QUERIES_COMBINATIONS.map(query => ({
...SYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
await ${query}('foo')
}
Expand All @@ -56,4 +56,18 @@ ruleTester.run('no-await-sync-query', rule, {
},
],
})),

// sync queries in screen with await operator are not valid
...SYNC_QUERIES_COMBINATIONS.map(query => ({
code: `async () => {
await screen.${query}('foo')
}
`,
errors: [
{
messageId: 'noAwaitSyncQuery',
},
],
})),
],
});