From c0b6e6f11fc4c536ad45c98cb058daef82b58258 Mon Sep 17 00:00:00 2001 From: justintoman Date: Sat, 23 Sep 2023 08:05:41 -0500 Subject: [PATCH] fix(await-async-events): sync userEvent.setup() should not be reported (#817) Fixes #800 Co-authored-by: Justin Toman <--global> --- lib/rules/await-async-events.ts | 4 ++++ tests/lib/rules/await-async-events.test.ts | 23 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/rules/await-async-events.ts b/lib/rules/await-async-events.ts index 53c62659..96adfcb2 100644 --- a/lib/rules/await-async-events.ts +++ b/lib/rules/await-async-events.ts @@ -16,6 +16,7 @@ export const RULE_NAME = 'await-async-events'; export type MessageIds = 'awaitAsyncEvent' | 'awaitAsyncEventWrapper'; const FIRE_EVENT_NAME = 'fireEvent'; const USER_EVENT_NAME = 'userEvent'; +const USER_EVENT_SETUP_FUNCTION_NAME = 'setup'; type EventModules = (typeof EVENTS_SIMULATORS)[number]; export type Options = [ { @@ -90,6 +91,9 @@ export default createTestingLibraryRule({ messageId?: MessageIds; fix?: TSESLint.ReportFixFunction; }): void { + if (node.name === USER_EVENT_SETUP_FUNCTION_NAME) { + return; + } if (!isPromiseHandled(node)) { context.report({ node: closestCallExpression.callee, diff --git a/tests/lib/rules/await-async-events.test.ts b/tests/lib/rules/await-async-events.test.ts index fe133c0c..2f0ce78e 100644 --- a/tests/lib/rules/await-async-events.test.ts +++ b/tests/lib/rules/await-async-events.test.ts @@ -181,6 +181,29 @@ ruleTester.run(RULE_NAME, rule, { ]), ...USER_EVENT_ASYNC_FRAMEWORKS.flatMap((testingFramework) => [ + { + code: ` + import userEvent from '${testingFramework}' + test('setup method called is valid', () => { + userEvent.setup() + }) + `, + options: [{ eventModule: 'userEvent' }] as const, + }, + { + code: ` + import userEvent from '${testingFramework}' + function customSetup() { + return { + user: userEvent.setup() + }; + } + test('setup method called and returned is valid', () => { + const {user} = customSetup(); + }) + `, + options: [{ eventModule: 'userEvent' }] as const, + }, ...USER_EVENT_ASYNC_FUNCTIONS.map((eventMethod) => ({ code: ` import userEvent from '${testingFramework}'