diff --git a/lib/rules/prefer-find-by.ts b/lib/rules/prefer-find-by.ts index 97c99300..40bb865a 100644 --- a/lib/rules/prefer-find-by.ts +++ b/lib/rules/prefer-find-by.ts @@ -464,10 +464,10 @@ export default createTestingLibraryRule({ const textDestructuring = sourceCode.getText( allVariableDeclarations ); - const text = `${textDestructuring.substring( - 0, - textDestructuring.length - 2 - )}, ${findByMethod} }`; + const text = textDestructuring.replace( + /(\s*})$/, + `, ${findByMethod}$1` + ); allFixes.push(fixer.replaceText(allVariableDeclarations, text)); } diff --git a/tests/lib/rules/prefer-find-by.test.ts b/tests/lib/rules/prefer-find-by.test.ts index 7975a023..aaafdaef 100644 --- a/tests/lib/rules/prefer-find-by.test.ts +++ b/tests/lib/rules/prefer-find-by.test.ts @@ -457,6 +457,35 @@ ruleTester.run(RULE_NAME, rule, { }) `, })), + ...createScenario((waitMethod: string, queryMethod: string) => ({ + code: ` + import {${waitMethod}} from '@testing-library/foo'; + it('tests', async () => { + const {${queryMethod}} = render() + const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull()) + }) + `, + errors: [ + { + messageId: 'preferFindBy', + data: { + queryVariant: getFindByQueryVariant(queryMethod), + queryMethod: queryMethod.split('By')[1], + prevQuery: queryMethod, + waitForMethodName: waitMethod, + }, + }, + ], + output: ` + import {${waitMethod}} from '@testing-library/foo'; + it('tests', async () => { + const {${queryMethod}, ${buildFindByMethod(queryMethod)}} = render() + const submitButton = await ${buildFindByMethod( + queryMethod + )}('foo', { name: 'baz' }) + }) + `, + })), ...createScenario((waitMethod: string, queryMethod: string) => ({ code: ` import {${waitMethod}} from '@testing-library/foo';