From 9714f04f47216c230be96ff48355ff83708ebfe0 Mon Sep 17 00:00:00 2001 From: Patrick Ahmetovic Date: Mon, 13 Feb 2023 20:52:10 +0100 Subject: [PATCH] fix(await-async-util): false positives due to empty strings in function wrapper names --- lib/rules/await-async-utils.ts | 10 +++++-- tests/lib/rules/await-async-utils.test.ts | 32 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/rules/await-async-utils.ts b/lib/rules/await-async-utils.ts index 9f545eba..a52e96d0 100644 --- a/lib/rules/await-async-utils.ts +++ b/lib/rules/await-async-utils.ts @@ -44,10 +44,16 @@ export default createTestingLibraryRule({ function detectAsyncUtilWrapper(node: TSESTree.Identifier) { const innerFunction = getInnermostReturningFunction(context, node); + if (!innerFunction) { + return; + } - if (innerFunction) { - functionWrappersNames.push(getFunctionName(innerFunction)); + const functionName = getFunctionName(innerFunction); + if (functionName.length === 0) { + return; } + + functionWrappersNames.push(functionName); } /* diff --git a/tests/lib/rules/await-async-utils.test.ts b/tests/lib/rules/await-async-utils.test.ts index 65248eac..7eb211bb 100644 --- a/tests/lib/rules/await-async-utils.test.ts +++ b/tests/lib/rules/await-async-utils.test.ts @@ -292,6 +292,38 @@ ruleTester.run(RULE_NAME, rule, { await setup().waitForAsyncUtil(); }); + `, + })), + ...ASYNC_UTILS.map((asyncUtil) => ({ + code: ` + import React from 'react'; + import { render, act } from '@testing-library/react'; + + const doWithAct = async (timeout) => { + await act(async () => await ${asyncUtil}(screen.getByTestId('my-test'))); + }; + + describe('Component', () => { + const mock = jest.fn(); + + it('test', async () => { + let Component = () => { + mock(1); + return
; + }; + render(); + + await doWithAct(500); + + const myNumberTestVar = 1; + const myBooleanTestVar = false; + const myArrayTestVar = [1, 2]; + const myStringTestVar = 'hello world'; + const myObjectTestVar = { hello: 'world' }; + + expect(mock).toHaveBeenCalledWith(myNumberTestVar); + }); + }); `, })), ]),