-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add rule no-multiple-assertions-wait-for #189
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
Belco90
merged 16 commits into
testing-library:v4
from
renatoagds:no-multiple-assertions-wait-for
Jun 30, 2020
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ac3b73b
feat: add initial files for no-multiple-expect-wait-for rule
renatoagds b5042f8
fix: add expect fields in test
renatoagds a1ce529
feat: add no-multiple-assertion-wait-for logic
renatoagds 9210399
feat: add findClosestCalleName in node-utils
renatoagds e406e40
feat: add check for expect and rename file
renatoagds aaab505
docs: add no-multiple-assertions-wait-for rule doc
renatoagds e4330cb
Merge branch 'v4' of github.com:testing-library/eslint-plugin-testing…
renatoagds 8a016e2
docs: add link for no-multiple-assertions-wait-for doc
renatoagds 2edcd96
docs: insert function example in no-multiple-assertions-wait-for
renatoagds 303f2a9
refactor: remove find closest call node from node-utils
renatoagds c4396ed
fix: check expect based in total
renatoagds bea31aa
docs: better english in no-multiple-assertions-wait-for rule details
renatoagds 15fd206
fix: use correct rule name in no-multiple-assertions-wait-for
renatoagds e693ae3
docs: improve docs for no-multiple-assertions-wait-for
renatoagds 352d8a1
fix: typo in no-multiple-assertions-wait-for
renatoagds a7612dd
fix: better english in no-multiple-assertions-wait-for
renatoagds 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,54 @@ | ||
# Multiple assertions inside `waitFor` are not preferred (no-multiple-assertions-wait-for) | ||
|
||
## Rule Details | ||
|
||
This rule aims to ensure the correct usage of `expect` inside `waitFor`, in the way that they're intended to be used. | ||
When using multiples assertions inside `waitFor`, if one fails, you have to wait for timeout before see it failing. | ||
Putting one assertion, you can both wait for the UI to settle to the state you want to assert on, | ||
and also fail faster if one of the assertions do end up failing | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```js | ||
const foo = async () => { | ||
await waitFor(() => { | ||
expect(a).toEqual('a'); | ||
expect(b).toEqual('b'); | ||
}); | ||
|
||
// or | ||
await waitFor(function() { | ||
expect(a).toEqual('a') | ||
expect(b).toEqual('b'); | ||
}) | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
const foo = async () => { | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await waitFor(() => expect(a).toEqual('a')); | ||
|
||
// this rule only looks for expect | ||
await waitFor(() => { | ||
fireEvent.keyDown(input, { key: 'ArrowDown' }); | ||
expect(b).toEqual('b'); | ||
}); | ||
|
||
// or | ||
await waitFor(() => { | ||
console.log('testing-library'); | ||
expect(b).toEqual('b'); | ||
}); | ||
|
||
// or | ||
await waitFor(function() { | ||
expect(a).toEqual('a') | ||
}) | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [about `waitFor`](https://testing-library.com/docs/dom-testing-library/api-async#waitfor) | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
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,63 @@ | ||
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils' | ||
import { getDocsUrl } from '../utils' | ||
import { isBlockStatement, findClosestCallNode, isMemberExpression, isCallExpression, isIdentifier } from '../node-utils' | ||
|
||
export const RULE_NAME = 'no-multiple-expect-wait-for'; | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const WAIT_EXPRESSION_QUERY = | ||
'CallExpression[callee.name=/^(waitFor)$/]'; | ||
|
||
export type MessageIds = 'noMultipleAssertionWaitFor'; | ||
type Options = []; | ||
|
||
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
"It's preferred to avoid multiple assertions in `waitFor`", | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noMultipleAssertionWaitFor: 'Avoid use multiple assertions to `waitFor`', | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create: function(context) { | ||
function reporttMultipleAssertion( | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: TSESTree.BlockStatement | ||
) { | ||
const totalExpect = (body: Array<TSESTree.Node>): Array<TSESTree.Node> => | ||
body.filter((node: TSESTree.ExpressionStatement) => { | ||
if ( | ||
isCallExpression(node.expression) && | ||
isMemberExpression(node.expression.callee) && | ||
isCallExpression(node.expression.callee.object) | ||
) { | ||
const object: TSESTree.CallExpression = node.expression.callee.object | ||
const expressionName: string = isIdentifier(object.callee) && object.callee.name | ||
return expressionName === 'expect' | ||
} else { | ||
return false | ||
} | ||
}) | ||
|
||
if (isBlockStatement(node) && totalExpect(node.body).length > 1) { | ||
context.report({ | ||
node, | ||
loc: node.loc.start, | ||
messageId: 'noMultipleAssertionWaitFor', | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
[`${WAIT_EXPRESSION_QUERY} > ArrowFunctionExpression > BlockStatement`]: reporttMultipleAssertion, | ||
[`${WAIT_EXPRESSION_QUERY} > FunctionExpression > BlockStatement`]: reporttMultipleAssertion, | ||
renatoagds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
}) |
115 changes: 115 additions & 0 deletions
115
tests/lib/rules/no-multiple-assertions-wait-for.test.ts
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,115 @@ | ||
import { createRuleTester } from '../test-utils'; | ||
import rule, { RULE_NAME } from '../../../lib/rules/no-multiple-assertions-wait-for'; | ||
|
||
const ruleTester = createRuleTester({ | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}); | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
await waitFor(() => expect(a).toEqual('a')) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() { | ||
expect(a).toEqual('a') | ||
}) | ||
`, | ||
}, | ||
// this needs to be check by other rule | ||
{ | ||
code: ` | ||
await waitFor(() => { | ||
fireEvent.keyDown(input, {key: 'ArrowDown'}) | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() { | ||
fireEvent.keyDown(input, {key: 'ArrowDown'}) | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(() => { | ||
console.log('testing-library') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() { | ||
console.log('testing-library') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(() => {}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() {}) | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(() => { | ||
// testing | ||
}) | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
await waitFor(() => { | ||
expect(a).toEqual('a') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
errors: [{ messageId: 'noMultipleAssertionWaitFor' }] | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(() => { | ||
expect(a).toEqual('a') | ||
console.log('testing-library') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
errors: [{ messageId: 'noMultipleAssertionWaitFor' }] | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() { | ||
expect(a).toEqual('a') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
errors: [{ messageId: 'noMultipleAssertionWaitFor' }] | ||
}, | ||
{ | ||
code: ` | ||
await waitFor(function() { | ||
expect(a).toEqual('a') | ||
console.log('testing-library') | ||
expect(b).toEqual('b') | ||
}) | ||
`, | ||
errors: [{ messageId: 'noMultipleAssertionWaitFor' }] | ||
} | ||
] | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.