-
Notifications
You must be signed in to change notification settings - Fork 149
feat(prefer-wait-for): new rule prefer-wait-for #88
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
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d8a6818
feat(prefer-wait-for): new rule prefer-wait-for
Belco90 3dc56de
docs(prefer-wait-for): pr fixes
Belco90 ea8fcce
test(prefer-wait-for): increase coverage
Belco90 fe39525
refactor(prefer-wait-for): use ternary
Belco90 550155f
docs(prefer-wait-for): PR small fixes
Belco90 d550a23
test(prefer-wait-for): include imports
Belco90 4e21af6
fix(prefer-wait-for): fist attempt to report related imports
Belco90 0c4ae1e
refactor(prefer-wait-for): merge wait reports under single func
Belco90 506f956
fix(prefer-wait-for): fix single imports
Belco90 8e2ba9a
fix(prefer-wait-for): fix several imports
Belco90 e69fb42
fix(prefer-wait-for): guard against empty named imports
Belco90 938e907
fix(prefer-wait-for): fix more imports cases
Belco90 c5b413a
feat(prefer-wait-for): new rule prefer-wait-for
Belco90 df677fe
docs(prefer-wait-for): pr fixes
Belco90 0c698cd
test(prefer-wait-for): increase coverage
Belco90 a841ffe
refactor(prefer-wait-for): use ternary
Belco90 c2be2ec
docs(prefer-wait-for): PR small fixes
Belco90 b410020
test(prefer-wait-for): include imports
Belco90 41414a4
fix(prefer-wait-for): fist attempt to report related imports
Belco90 99dac28
refactor(prefer-wait-for): merge wait reports under single func
Belco90 12980e8
fix(prefer-wait-for): fix single imports
Belco90 623f88b
fix(prefer-wait-for): fix several imports
Belco90 82fa2df
fix(prefer-wait-for): guard against empty named imports
Belco90 03b352d
fix(prefer-wait-for): fix more imports cases
Belco90 67c1f5b
Merge remote-tracking branch 'origin/feature/rule-prefer-wait-for' in…
Belco90 158be04
Merge remote-tracking branch 'origin/v3' into feature/rule-prefer-wai…
Belco90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Use `waitFor` instead of deprecated wait methods (prefer-wait-for) | ||
|
||
`dom-testing-library` v7 released a new async util called `waitFor` which satisfies the use cases of `wait`, `waitForElement`, and `waitForDomChange` making them deprecated. | ||
|
||
## Rule Details | ||
|
||
This rule aims to use `waitFor` async util rather than previous deprecated ones. | ||
|
||
Deprecated `wait` async utils are: | ||
|
||
- `wait` | ||
- `waitForElement` | ||
- `waitForDomChange` | ||
|
||
> This rule will auto fix deprecated async utils for you, including the necessary empty callback for `waitFor`. This means `wait();` will be replaced with `waitFor(() => {});` | ||
|
||
Examples of **incorrect** code for this rule: | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js | ||
const foo = async () => { | ||
await wait(); | ||
await wait(() => {}); | ||
await waitForElement(() => {}); | ||
await waitForDomChange(); | ||
await waitForDomChange(mutationObserverOptions); | ||
await waitForDomChange({ timeout: 100}); | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js | ||
const foo = async () => { | ||
// new waitFor method | ||
await waitFor(() => {}); | ||
|
||
// previous waitForElementToBeRemoved is not deprecated | ||
await waitForElementToBeRemoved(() => {}); | ||
}; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
When using dom-testing-library (or any other Testing Library relying on dom-testing-library) prior to v7. | ||
|
||
## Further Reading | ||
|
||
- [dom-testing-library v7 release](https://github.com/testing-library/dom-testing-library/releases/tag/v7.0.0) |
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
const { getDocsUrl } = require('../utils'); | ||
|
||
const DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange']; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Use `waitFor` instead of deprecated wait methods', | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: getDocsUrl('prefer-wait-for'), | ||
}, | ||
messages: { | ||
preferWaitForMethod: | ||
'`{{ methodName }}` is deprecated in favour of `waitFor`', | ||
preferWaitForImport: 'import `waitFor` instead of deprecated async utils', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
|
||
create: function(context) { | ||
const importNodes = []; | ||
const waitNodes = []; | ||
|
||
const reportImport = node => { | ||
context.report({ | ||
node: node, | ||
messageId: 'preferWaitForImport', | ||
fix(fixer) { | ||
const excludedImports = [...DEPRECATED_METHODS, 'waitFor']; | ||
|
||
// get all import names excluding all testing library `wait*` utils... | ||
const newImports = node.specifiers | ||
.filter( | ||
specifier => !excludedImports.includes(specifier.imported.name) | ||
) | ||
.map(specifier => specifier.imported.name); | ||
|
||
// ... and append `waitFor` | ||
newImports.push('waitFor'); | ||
|
||
// build new node with new imports and previous source value | ||
const newNode = `import { ${newImports.join(',')} } from '${ | ||
node.source.value | ||
}';`; | ||
|
||
return fixer.replaceText(node, newNode); | ||
}, | ||
}); | ||
}; | ||
|
||
const reportWait = node => { | ||
context.report({ | ||
node: node, | ||
messageId: 'preferWaitForMethod', | ||
data: { | ||
methodName: node.name, | ||
}, | ||
fix(fixer) { | ||
const { parent } = node; | ||
const [arg] = parent.arguments; | ||
const fixers = []; | ||
|
||
if (arg) { | ||
// if method been fixed already had a callback | ||
// then we just replace the method name. | ||
fixers.push(fixer.replaceText(node, 'waitFor')); | ||
|
||
if (node.name === 'waitForDomChange') { | ||
// if method been fixed is `waitForDomChange` | ||
// then the arg received was options object so we need to insert | ||
// empty callback before. | ||
fixers.push(fixer.insertTextBefore(arg, `() => {}, `)); | ||
} | ||
} else { | ||
// if wait method been fixed didn't have any callback | ||
// then we replace the method name and include an empty callback. | ||
fixers.push(fixer.replaceText(parent, 'waitFor(() => {})')); | ||
} | ||
|
||
return fixers; | ||
}, | ||
}); | ||
}; | ||
|
||
return { | ||
'ImportDeclaration[source.value=/testing-library/]'(node) { | ||
const importedNames = node.specifiers | ||
.map(specifier => specifier.imported && specifier.imported.name) | ||
.filter(Boolean); | ||
|
||
if ( | ||
importedNames.some(importedName => | ||
DEPRECATED_METHODS.includes(importedName) | ||
) | ||
) { | ||
importNodes.push(node); | ||
} | ||
}, | ||
'CallExpression > Identifier[name=/^(wait|waitForElement|waitForDomChange)$/]'( | ||
node | ||
) { | ||
waitNodes.push(node); | ||
}, | ||
'Program:exit'() { | ||
waitNodes.forEach(waitNode => { | ||
reportWait(waitNode); | ||
}); | ||
|
||
importNodes.forEach(importNode => { | ||
reportImport(importNode); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.
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.
💯