Skip to content

Commit e31fe03

Browse files
authored
fix(prefer-find-by): Respect waitFor options when autofixing (#679)
Relates to #579 * fix(docs): mark code keywords with `` and add a period to end of sentence * fix(prefer-find-by): autofixer now transfers waitFor options to autofixed
1 parent 2e1e9d5 commit e31fe03

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

docs/rules/prefer-find-by.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries (`testing-library/prefer-find-by`)
22

3-
findBy* queries are a simple combination of getBy* queries and waitFor. The findBy\* queries accept the waitFor options as the last argument. (i.e. screen.findByText('text', queryOptions, waitForOptions))
3+
`findBy*` queries are a simple combination of `getBy*` queries and `waitFor`. The `findBy*` queries accept the `waitFor` options as the last argument. (i.e. `screen.findByText('text', queryOptions, waitForOptions)`)
44

55
## Rule details
66

77
This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`, or the deprecated methods `waitForElement` and `wait`.
8-
This rule analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code
8+
This rule analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code.
99

1010
Examples of **incorrect** code for this rule
1111

@@ -78,7 +78,7 @@ await waitFor(() => expect(getAllByText('bar')).toBeDisabled());
7878

7979
## When Not To Use It
8080

81-
- Not encouraging use of findBy shortcut from testing library best practices
81+
- Not encouraging use of `findBy` shortcut from testing library best practices
8282

8383
## Further Reading
8484

lib/rules/prefer-find-by.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isArrowFunctionExpression,
66
isCallExpression,
77
isMemberExpression,
8+
isObjectExpression,
89
isObjectPattern,
910
isProperty,
1011
} from '../node-utils';
@@ -366,6 +367,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
366367
return;
367368
}
368369

370+
// if there is a second argument to AwaitExpression, it is the options
371+
const waitOptions = node.arguments[1];
372+
let waitOptionsSourceCode = '';
373+
if (isObjectExpression(waitOptions)) {
374+
waitOptionsSourceCode = `, ${sourceCode.getText(waitOptions)}`;
375+
}
376+
369377
const queryVariant = getFindByQueryVariant(fullQueryMethod);
370378
const callArguments = getQueryArguments(argument.body);
371379
const queryMethod = fullQueryMethod.split('By')[1];
@@ -389,7 +397,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
389397
}
390398
const newCode = `${caller}.${queryVariant}${queryMethod}(${callArguments
391399
.map((callArgNode) => sourceCode.getText(callArgNode))
392-
.join(', ')})`;
400+
.join(', ')}${waitOptionsSourceCode})`;
393401
return fixer.replaceText(node, newCode);
394402
},
395403
});

tests/lib/rules/prefer-find-by.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,29 @@ ruleTester.run(RULE_NAME, rule, {
702702
queryMethod
703703
)}('foo', { name: 'baz' })
704704
})
705+
`,
706+
})),
707+
// Issue #579, https://github.com/testing-library/eslint-plugin-testing-library/issues/579
708+
// findBy can have two sets of options: await screen.findByText('text', queryOptions, waitForOptions)
709+
...createScenario((waitMethod: string, queryMethod: string) => ({
710+
code: `import {${waitMethod}} from '${testingFramework}';
711+
const button = await ${waitMethod}(() => screen.${queryMethod}('Count is: 0'), { timeout: 100, interval: 200 })
712+
`,
713+
errors: [
714+
{
715+
messageId: 'preferFindBy',
716+
data: {
717+
queryVariant: getFindByQueryVariant(queryMethod),
718+
queryMethod: queryMethod.split('By')[1],
719+
prevQuery: queryMethod,
720+
waitForMethodName: waitMethod,
721+
},
722+
},
723+
],
724+
output: `import {${waitMethod}} from '${testingFramework}';
725+
const button = await screen.${buildFindByMethod(
726+
queryMethod
727+
)}('Count is: 0', { timeout: 100, interval: 200 })
705728
`,
706729
})),
707730
]),

0 commit comments

Comments
 (0)