Skip to content

Commit

Permalink
fix(no-manual-cleanup): properly check all @testing-library imports (
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Oct 17, 2024
1 parent 27db389 commit 62a3f61
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/rules/no-manual-cleanup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -110,12 +111,15 @@ export default createTestingLibraryRule<Options, MessageIds>({

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);
}
}

Expand Down
51 changes: 51 additions & 0 deletions tests/lib/rules/no-manual-cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
({
Expand Down Expand Up @@ -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',
},
],
},
],
});

0 comments on commit 62a3f61

Please sign in to comment.