diff --git a/lib/rules/no-manual-cleanup.ts b/lib/rules/no-manual-cleanup.ts index 3313c420..9074d669 100644 --- a/lib/rules/no-manual-cleanup.ts +++ b/lib/rules/no-manual-cleanup.ts @@ -1,15 +1,16 @@ -import { ASTUtils, TSESTree, TSESLint } from '@typescript-eslint/utils'; +import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils'; import { createTestingLibraryRule } from '../create-testing-library-rule'; import { + getImportModuleName, getVariableReferences, + ImportModuleNode, + isImportDeclaration, isImportDefaultSpecifier, isImportSpecifier, isMemberExpression, isObjectPattern, isProperty, - ImportModuleNode, - isImportDeclaration, } from '../node-utils'; export const RULE_NAME = 'no-manual-cleanup'; @@ -110,12 +111,15 @@ export default createTestingLibraryRule({ return { 'Program:exit'() { - const testingLibraryImportName = helpers.getTestingLibraryImportName(); const customModuleImportNode = helpers.getCustomModuleImportNode(); - if (testingLibraryImportName?.match(CLEANUP_LIBRARY_REGEXP)) { - for (const importNode of helpers.getAllTestingLibraryImportNodes()) { - reportCandidateModule(importNode); + for (const testingLibraryImportNode of helpers.getAllTestingLibraryImportNodes()) { + const testingLibraryImportName = getImportModuleName( + testingLibraryImportNode + ); + + if (testingLibraryImportName?.match(CLEANUP_LIBRARY_REGEXP)) { + reportCandidateModule(testingLibraryImportNode); } } diff --git a/tests/lib/rules/no-manual-cleanup.test.ts b/tests/lib/rules/no-manual-cleanup.test.ts index 61208801..1f14312d 100644 --- a/tests/lib/rules/no-manual-cleanup.test.ts +++ b/tests/lib/rules/no-manual-cleanup.test.ts @@ -69,6 +69,38 @@ ruleTester.run(RULE_NAME, rule, { ], } as const) ), + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map( + (lib) => + ({ + code: ` + import { render, cleanup } from "${lib}" + import userEvent from "@testing-library/user-event" + `, + errors: [ + { + line: 2, + column: 24, // error points to `cleanup` + messageId: 'noManualCleanup', + }, + ], + } as const) + ), + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map( + (lib) => + ({ + code: ` + import userEvent from "@testing-library/user-event" + import { render, cleanup } from "${lib}" + `, + errors: [ + { + line: 3, + column: 24, // error points to `cleanup` + messageId: 'noManualCleanup', + }, + ], + } as const) + ), ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map( (lib) => ({ @@ -252,5 +284,24 @@ ruleTester.run(RULE_NAME, rule, { ], } as const) ), + { + code: ` + import { cleanup as cleanupVue } from "@testing-library/vue"; + import { cleanup as cleanupReact } from "@testing-library/react"; + afterEach(() => { cleanupVue(); cleanupReact(); }); + `, + errors: [ + { + line: 2, + column: 14, + messageId: 'noManualCleanup', + }, + { + line: 3, + column: 14, + messageId: 'noManualCleanup', + }, + ], + }, ], });