Skip to content

fix(await-async-query): get correct Identifier related to CallExpression #374

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
merged 1 commit into from
May 9, 2021
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
36 changes: 22 additions & 14 deletions lib/rules/await-async-query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import {
findClosestCallExpressionNode,
getDeepestIdentifierNode,
getFunctionName,
getInnermostReturningFunction,
getVariableReferences,
Expand All @@ -27,9 +28,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
},
messages: {
awaitAsyncQuery: 'promise returned from {{ name }} query must be handled',
awaitAsyncQuery:
'promise returned from `{{ name }}` query must be handled',
asyncQueryWrapper:
'promise returned from {{ name }} wrapper over async query must be handled',
'promise returned from `{{ name }}` wrapper over async query must be handled',
},
schema: [],
},
Expand All @@ -46,10 +48,16 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was here. This is getting an Identifier n levels within a CallExpression, which can lead to an Identifier not related at all to the CallExpression. Fixed just using getDeepestIdentifierNode so we get the Identifier of the CallExpression we are interested in.

if (helpers.isAsyncQuery(node)) {
CallExpression(node) {
const identifierNode = getDeepestIdentifierNode(node);

if (!identifierNode) {
return;
}

if (helpers.isAsyncQuery(identifierNode)) {
// detect async query used within wrapper function for later analysis
detectAsyncQueryWrapper(node);
detectAsyncQueryWrapper(identifierNode);

const closestCallExpressionNode = findClosestCallExpressionNode(
node,
Expand All @@ -68,11 +76,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
// check direct usage of async query:
// const element = await findByRole('button')
if (references && references.length === 0) {
if (!isPromiseHandled(node)) {
if (!isPromiseHandled(identifierNode)) {
return context.report({
node,
node: identifierNode,
messageId: 'awaitAsyncQuery',
data: { name: node.name },
data: { name: identifierNode.name },
});
}
}
Expand All @@ -86,19 +94,19 @@ export default createTestingLibraryRule<Options, MessageIds>({
!isPromiseHandled(reference.identifier)
) {
return context.report({
node,
node: identifierNode,
messageId: 'awaitAsyncQuery',
data: { name: node.name },
data: { name: identifierNode.name },
});
}
}
} else if (functionWrappersNames.includes(node.name)) {
} else if (functionWrappersNames.includes(identifierNode.name)) {
// check async queries used within a wrapper previously detected
if (!isPromiseHandled(node)) {
if (!isPromiseHandled(identifierNode)) {
return context.report({
node,
node: identifierNode,
messageId: 'asyncQueryWrapper',
data: { name: node.name },
data: { name: identifierNode.name },
});
}
}
Expand Down
54 changes: 54 additions & 0 deletions tests/lib/rules/await-async-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ ruleTester.run(RULE_NAME, rule, {
})
`,

// https://github.com/testing-library/eslint-plugin-testing-library/issues/359
`// issue #359
import { render, screen } from 'mocks/test-utils'
import userEvent from '@testing-library/user-event'

const testData = {
name: 'John Doe',
email: '[email protected]',
password: 'extremeSecret',
}

const selectors = {
username: () => screen.findByRole('textbox', { name: /username/i }),
email: () => screen.findByRole('textbox', { name: /e-mail/i }),
password: () => screen.findByLabelText(/password/i),
}

test('this is a valid case', async () => {
render(<SomeComponent />)
userEvent.type(await selectors.username(), testData.name)
userEvent.type(await selectors.email(), testData.email)
userEvent.type(await selectors.password(), testData.password)
// ...
})
`,

// edge case for coverage
// valid async query usage without any function defined
// so there is no innermost function scope found
Expand Down Expand Up @@ -449,5 +475,33 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [{ messageId: 'awaitAsyncQuery', line: 3, column: 25 }],
},

{
code: `// similar to issue #359 but forcing an error in no-awaited wrapper
import { render, screen } from 'mocks/test-utils'
import userEvent from '@testing-library/user-event'

const testData = {
name: 'John Doe',
email: '[email protected]',
password: 'extremeSecret',
}

const selectors = {
username: () => screen.findByRole('textbox', { name: /username/i }),
email: () => screen.findByRole('textbox', { name: /e-mail/i }),
password: () => screen.findByLabelText(/password/i),
}

test('this is a valid case', async () => {
render(<SomeComponent />)
userEvent.type(selectors.username(), testData.name) // <-- unhandled here
userEvent.type(await selectors.email(), testData.email)
userEvent.type(await selectors.password(), testData.password)
// ...
})
`,
errors: [{ messageId: 'asyncQueryWrapper', line: 19, column: 34 }],
},
],
});