-
Notifications
You must be signed in to change notification settings - Fork 150
feat(await-async-query): add auto-fix #917
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
Open
neriyarden
wants to merge
8
commits into
testing-library:main
Choose a base branch
from
neriyarden:feat--add-a-fixer-for-await-async-queries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8544eda
feat: create a fixer
neriyarden 5059bc2
feat: fix promise references
neriyarden fe691cd
feat: fix promise wrappers
neriyarden 5c72406
docs: update docs
neriyarden 5fbbc50
feat: refactor loop to map in fixer
neriyarden a582606
refactor: improve comments readability
neriyarden bfbd457
Update lib/rules/await-async-queries.ts
MichaelDeBoey 7ab273d
Merge upstream/main into feature branch
neriyarden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ import { ASTUtils, TSESTree } from '@typescript-eslint/utils'; | |
import { createTestingLibraryRule } from '../create-testing-library-rule'; | ||
import { | ||
findClosestCallExpressionNode, | ||
findClosestFunctionExpressionNode, | ||
getDeepestIdentifierNode, | ||
getFunctionName, | ||
getInnermostReturningFunction, | ||
getVariableReferences, | ||
isMemberExpression, | ||
isPromiseHandled, | ||
} from '../node-utils'; | ||
|
||
|
@@ -35,6 +37,7 @@ export default createTestingLibraryRule<Options, MessageIds>({ | |
asyncQueryWrapper: | ||
'promise returned from `{{ name }}` wrapper over async query must be handled', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
|
@@ -75,22 +78,39 @@ export default createTestingLibraryRule<Options, MessageIds>({ | |
closestCallExpressionNode.parent | ||
); | ||
|
||
// check direct usage of async query: | ||
// const element = await findByRole('button') | ||
/** | ||
* Check direct usage of async query: | ||
* const element = await findByRole('button'); | ||
*/ | ||
if (references.length === 0) { | ||
if (!isPromiseHandled(identifierNode)) { | ||
context.report({ | ||
node: identifierNode, | ||
messageId: 'awaitAsyncQuery', | ||
data: { name: identifierNode.name }, | ||
fix: (fixer) => { | ||
if ( | ||
isMemberExpression(identifierNode.parent) && | ||
ASTUtils.isIdentifier(identifierNode.parent.object) && | ||
identifierNode.parent.object.name === 'screen' | ||
) { | ||
return fixer.insertTextBefore( | ||
identifierNode.parent, | ||
'await ' | ||
); | ||
} | ||
return fixer.insertTextBefore(identifierNode, 'await '); | ||
}, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
// check references usages of async query: | ||
// const promise = findByRole('button') | ||
// const element = await promise | ||
/** | ||
* Check references usages of async query: | ||
* const promise = findByRole('button'); | ||
* const element = await promise; | ||
*/ | ||
for (const reference of references) { | ||
if ( | ||
ASTUtils.isIdentifier(reference.identifier) && | ||
|
@@ -100,6 +120,10 @@ export default createTestingLibraryRule<Options, MessageIds>({ | |
node: identifierNode, | ||
messageId: 'awaitAsyncQuery', | ||
data: { name: identifierNode.name }, | ||
fix: (fixer) => | ||
references.map((ref) => | ||
fixer.insertTextBefore(ref.identifier, 'await ') | ||
), | ||
}); | ||
return; | ||
} | ||
|
@@ -108,11 +132,56 @@ export default createTestingLibraryRule<Options, MessageIds>({ | |
functionWrappersNames.includes(identifierNode.name) && | ||
!isPromiseHandled(identifierNode) | ||
) { | ||
// check async queries used within a wrapper previously detected | ||
// Check async queries used within a wrapper previously detected | ||
context.report({ | ||
node: identifierNode, | ||
messageId: 'asyncQueryWrapper', | ||
data: { name: identifierNode.name }, | ||
fix: (fixer) => { | ||
const functionExpression = | ||
findClosestFunctionExpressionNode(node); | ||
|
||
if (!functionExpression) return null; | ||
|
||
let IdentifierNodeFixer; | ||
if (isMemberExpression(identifierNode.parent)) { | ||
/** | ||
* If the wrapper is a property of an object, | ||
* add 'await' before the object, e.g.: | ||
* const obj = { wrapper: () => screen.findByText(/foo/i) }; | ||
* await obj.wrapper(); | ||
*/ | ||
IdentifierNodeFixer = fixer.insertTextBefore( | ||
identifierNode.parent, | ||
'await ' | ||
); | ||
} else { | ||
/** | ||
* Add 'await' before the wrapper function, e.g.: | ||
* const wrapper = () => screen.findByText(/foo/i); | ||
* await wrapper(); | ||
*/ | ||
IdentifierNodeFixer = fixer.insertTextBefore( | ||
identifierNode, | ||
'await ' | ||
); | ||
} | ||
|
||
if (functionExpression.async) { | ||
return IdentifierNodeFixer; | ||
} else { | ||
/** | ||
* Mutate the actual node so if other nodes exist in this | ||
* function expression body they don't also try to fix it. | ||
*/ | ||
functionExpression.async = true; | ||
|
||
return [ | ||
IdentifierNodeFixer, | ||
fixer.insertTextBefore(functionExpression, 'async '), | ||
]; | ||
} | ||
}, | ||
Comment on lines
+141
to
+184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one! |
||
}); | ||
} | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -361,6 +361,14 @@ ruleTester.run(RULE_NAME, rule, { | |
}); | ||
`, | ||
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }], | ||
output: `// async queries without await operator or then method are not valid | ||
import { render } from '${testingFramework}' | ||
|
||
test("An example test", async () => { | ||
doSomething() | ||
const foo = await ${query}('foo') | ||
}); | ||
`, | ||
}) as const | ||
) | ||
), | ||
|
@@ -382,6 +390,13 @@ ruleTester.run(RULE_NAME, rule, { | |
data: { name: query }, | ||
}, | ||
], | ||
output: `// async screen queries without await operator or then method are not valid | ||
import { render } from '@testing-library/react' | ||
|
||
test("An example test", async () => { | ||
await screen.${query}('foo') | ||
}); | ||
`, | ||
}) as const | ||
), | ||
...ALL_ASYNC_COMBINATIONS_TO_TEST.map( | ||
|
@@ -403,6 +418,14 @@ ruleTester.run(RULE_NAME, rule, { | |
data: { name: query }, | ||
}, | ||
], | ||
output: ` | ||
import { render } from '@testing-library/react' | ||
|
||
test("An example test", async () => { | ||
doSomething() | ||
const foo = await ${query}('foo') | ||
}); | ||
`, | ||
}) as const | ||
), | ||
...ALL_ASYNC_COMBINATIONS_TO_TEST.map( | ||
|
@@ -425,6 +448,15 @@ ruleTester.run(RULE_NAME, rule, { | |
data: { name: query }, | ||
}, | ||
], | ||
output: ` | ||
import { render } from '@testing-library/react' | ||
|
||
test("An example test", async () => { | ||
const foo = ${query}('foo') | ||
expect(await foo).toBeInTheDocument() | ||
expect(await foo).toHaveAttribute('src', 'bar'); | ||
}); | ||
`, | ||
}) as const | ||
), | ||
|
||
|
@@ -440,6 +472,13 @@ ruleTester.run(RULE_NAME, rule, { | |
}) | ||
`, | ||
errors: [{ messageId: 'awaitAsyncQuery', line: 5, column: 27 }], | ||
output: ` | ||
import { render } from "another-library" | ||
|
||
test('An example test', async () => { | ||
const example = await ${query}("my example") | ||
}) | ||
`, | ||
}) as const | ||
), | ||
|
||
|
@@ -458,11 +497,26 @@ ruleTester.run(RULE_NAME, rule, { | |
const element = queryWrapper() | ||
}) | ||
|
||
test("An invalid example test", async () => { | ||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }], | ||
output: ` | ||
function queryWrapper() { | ||
doSomethingElse(); | ||
|
||
return screen.${query}('foo') | ||
} | ||
|
||
test("An invalid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
|
||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
}) as const | ||
), | ||
// unhandled promise from async query arrow function wrapper is invalid | ||
|
@@ -480,11 +534,26 @@ ruleTester.run(RULE_NAME, rule, { | |
const element = queryWrapper() | ||
}) | ||
|
||
test("An invalid example test", async () => { | ||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
errors: [{ messageId: 'asyncQueryWrapper', line: 9, column: 27 }], | ||
output: ` | ||
const queryWrapper = () => { | ||
doSomethingElse(); | ||
|
||
return ${query}('foo') | ||
} | ||
|
||
test("An invalid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
|
||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
}) as const | ||
), | ||
// unhandled promise implicitly returned from async query arrow function wrapper is invalid | ||
|
@@ -498,11 +567,22 @@ ruleTester.run(RULE_NAME, rule, { | |
const element = queryWrapper() | ||
}) | ||
|
||
test("An invalid example test", async () => { | ||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
errors: [{ messageId: 'asyncQueryWrapper', line: 5, column: 27 }], | ||
output: ` | ||
const queryWrapper = () => screen.${query}('foo') | ||
|
||
test("An invalid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
|
||
test("A valid example test", async () => { | ||
const element = await queryWrapper() | ||
}) | ||
`, | ||
}) as const | ||
), | ||
|
||
|
@@ -517,6 +597,11 @@ ruleTester.run(RULE_NAME, rule, { | |
}) | ||
`, | ||
errors: [{ messageId: 'awaitAsyncQuery', line: 3, column: 25 }], | ||
output: ` | ||
test('An invalid example test', () => { | ||
const element = await findByIcon('search') | ||
}) | ||
`, | ||
}, | ||
|
||
{ | ||
|
@@ -545,6 +630,30 @@ ruleTester.run(RULE_NAME, rule, { | |
}) | ||
`, | ||
errors: [{ messageId: 'asyncQueryWrapper', line: 19, column: 34 }], | ||
output: `// 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(await selectors.username(), testData.name) // <-- unhandled here | ||
userEvent.type(await selectors.email(), testData.email) | ||
userEvent.type(await selectors.password(), testData.password) | ||
// ... | ||
}) | ||
`, | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it really matter if the parent object is
screen
? I think it should work no matter what's the name of its parent is.For example:
In this case,
view.findByText
wouldn't be autofixed, but it should. Can you add this scenario to the tests, and adjust the rule fix accordingly?