Skip to content

Commit 82124af

Browse files
committed
fix(prefer-find-by): simplify error message
1 parent a8c1fe4 commit 82124af

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

lib/rules/prefer-find-by.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const RULE_NAME = 'prefer-find-by';
1717
export type MessageIds = 'preferFindBy';
1818
type Options = [];
1919

20-
export const WAIT_METHODS = ['waitFor', 'waitForElement', 'wait'];
20+
export const WAIT_METHODS = ['waitFor', 'waitForElement', 'wait'] as const;
2121

2222
export function getFindByQueryVariant(
2323
queryMethod: string
@@ -53,13 +53,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
5353
type: 'suggestion',
5454
docs: {
5555
description:
56-
'Suggest using find* instead of waitFor to wait for elements',
56+
'Suggest using `find*` query instead of `waitFor` + `get*` to wait for elements',
5757
category: 'Best Practices',
5858
recommended: 'warn',
5959
},
6060
messages: {
6161
preferFindBy:
62-
'Prefer {{queryVariant}}{{queryMethod}} method over using await {{fullQuery}}',
62+
'Prefer `{{queryVariant}}{{queryMethod}}` query over using `{{waitForMethodName}}` + `{{prevQuery}}`',
6363
},
6464
fixable: 'code',
6565
schema: [],
@@ -71,30 +71,39 @@ export default createTestingLibraryRule<Options, MessageIds>({
7171

7272
/**
7373
* Reports the invalid usage of wait* plus getBy/QueryBy methods and automatically fixes the scenario
74-
* @param {TSESTree.CallExpression} node - The CallExpresion node that contains the wait* method
75-
* @param {'findBy' | 'findAllBy'} replacementParams.queryVariant - The variant method used to query: findBy/findByAll.
76-
* @param {string} replacementParams.queryMethod - Suffix string to build the query method (the query-part that comes after the "By"): LabelText, Placeholder, Text, Role, Title, etc.
77-
* @param {ReportFixFunction} replacementParams.fix - Function that applies the fix to correct the code
74+
* @param node - The CallExpresion node that contains the wait* method
75+
* @param replacementParams - Object with info for error message and autofix:
76+
* @param replacementParams.queryVariant - The variant method used to query: findBy/findAllBy.
77+
* @param replacementParams.prevQuery - The query originally used inside `waitFor`
78+
* @param replacementParams.queryMethod - Suffix string to build the query method (the query-part that comes after the "By"): LabelText, Placeholder, Text, Role, Title, etc.
79+
* @param replacementParams.waitForMethodName - wait for method used: waitFor/wait/waitForElement
80+
* @param replacementParams.fix - Function that applies the fix to correct the code
7881
*/
7982
function reportInvalidUsage(
8083
node: TSESTree.CallExpression,
81-
{
82-
queryVariant,
83-
queryMethod,
84-
fix,
85-
}: {
84+
replacementParams: {
8685
queryVariant: 'findBy' | 'findAllBy';
8786
queryMethod: string;
87+
prevQuery: string;
88+
waitForMethodName: string;
8889
fix: ReportFixFunction;
8990
}
9091
) {
92+
const {
93+
queryMethod,
94+
queryVariant,
95+
prevQuery,
96+
waitForMethodName,
97+
fix,
98+
} = replacementParams;
9199
context.report({
92100
node,
93101
messageId: 'preferFindBy',
94102
data: {
95103
queryVariant,
96104
queryMethod,
97-
fullQuery: sourceCode.getText(node),
105+
prevQuery,
106+
waitForMethodName,
98107
},
99108
fix,
100109
});
@@ -104,7 +113,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
104113
'AwaitExpression > CallExpression'(node: TSESTree.CallExpression) {
105114
if (
106115
!ASTUtils.isIdentifier(node.callee) ||
107-
!WAIT_METHODS.includes(node.callee.name)
116+
!helpers.isAsyncUtil(node.callee, WAIT_METHODS)
108117
) {
109118
return;
110119
}
@@ -117,6 +126,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
117126
if (!isCallExpression(argument.body)) {
118127
return;
119128
}
129+
130+
const waitForMethodName = node.callee.name;
131+
120132
// ensure here it's one of the sync methods that we are calling
121133
if (
122134
isMemberExpression(argument.body.callee) &&
@@ -134,6 +146,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
134146
reportInvalidUsage(node, {
135147
queryMethod,
136148
queryVariant,
149+
prevQuery: fullQueryMethod,
150+
waitForMethodName,
137151
fix(fixer) {
138152
const property = ((argument.body as TSESTree.CallExpression)
139153
.callee as TSESTree.MemberExpression).property;
@@ -163,6 +177,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
163177
reportInvalidUsage(node, {
164178
queryMethod,
165179
queryVariant,
180+
prevQuery: fullQueryMethod,
181+
waitForMethodName,
166182
fix(fixer) {
167183
// we know from above callee is an Identifier
168184
if (

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ ruleTester.run(RULE_NAME, rule, {
149149
data: {
150150
queryVariant: getFindByQueryVariant(queryMethod),
151151
queryMethod: queryMethod.split('By')[1],
152-
fullQuery: `${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))`,
152+
prevQuery: queryMethod,
153+
waitForMethodName: waitMethod,
153154
},
154155
},
155156
],
@@ -176,7 +177,8 @@ ruleTester.run(RULE_NAME, rule, {
176177
data: {
177178
queryVariant: getFindByQueryVariant(queryMethod),
178179
queryMethod: queryMethod.split('By')[1],
179-
fullQuery: `${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))`,
180+
prevQuery: queryMethod,
181+
waitForMethodName: waitMethod,
180182
},
181183
},
182184
],
@@ -204,7 +206,8 @@ ruleTester.run(RULE_NAME, rule, {
204206
data: {
205207
queryVariant: 'findBy',
206208
queryMethod: 'Text',
207-
fullQuery: `${waitMethod}(() => getByText('baz', { name: 'button' }))`,
209+
prevQuery: 'getByText',
210+
waitForMethodName: waitMethod,
208211
},
209212
},
210213
],
@@ -231,7 +234,8 @@ ruleTester.run(RULE_NAME, rule, {
231234
data: {
232235
queryVariant: 'findAllBy',
233236
queryMethod: 'Role',
234-
fullQuery: `${waitMethod}(() => getAllByRole('baz', { name: 'button' }))`,
237+
prevQuery: 'getAllByRole',
238+
waitForMethodName: waitMethod,
235239
},
236240
},
237241
],
@@ -252,7 +256,8 @@ ruleTester.run(RULE_NAME, rule, {
252256
data: {
253257
queryVariant: 'findBy',
254258
queryMethod: 'Text',
255-
fullQuery: `waitFor(() => getByText('baz', { name: 'button' }))`,
259+
prevQuery: 'getByText',
260+
waitForMethodName: 'waitFor',
256261
},
257262
},
258263
],
@@ -270,7 +275,8 @@ ruleTester.run(RULE_NAME, rule, {
270275
data: {
271276
queryVariant: 'findBy',
272277
queryMethod: 'Role',
273-
fullQuery: `waitFor(() => getByRole('baz', { name: 'button' }))`,
278+
prevQuery: 'getByRole',
279+
waitForMethodName: 'waitFor',
274280
},
275281
},
276282
],
@@ -294,7 +300,8 @@ ruleTester.run(RULE_NAME, rule, {
294300
data: {
295301
queryVariant: 'findBy',
296302
queryMethod: 'CustomQuery',
297-
fullQuery: `${waitMethod}(() => getByCustomQuery('baz'))`,
303+
prevQuery: 'getByCustomQuery',
304+
waitForMethodName: waitMethod,
298305
},
299306
},
300307
],
@@ -321,7 +328,8 @@ ruleTester.run(RULE_NAME, rule, {
321328
data: {
322329
queryVariant: 'findBy',
323330
queryMethod: 'CustomQuery',
324-
fullQuery: `${waitMethod}(() => screen.getByCustomQuery('baz'))`,
331+
prevQuery: 'getByCustomQuery',
332+
waitForMethodName: waitMethod,
325333
},
326334
},
327335
],

0 commit comments

Comments
 (0)