Skip to content

Commit dfa7dfd

Browse files
committed
fix(no-manual-cleanup): properly check all @testing-library imports
1 parent 4dc7caa commit dfa7dfd

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

lib/rules/no-manual-cleanup.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { ASTUtils, TSESTree, TSESLint } from '@typescript-eslint/utils';
1+
import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
22

33
import { createTestingLibraryRule } from '../create-testing-library-rule';
44
import {
5+
getImportModuleName,
56
getVariableReferences,
7+
ImportModuleNode,
8+
isImportDeclaration,
69
isImportDefaultSpecifier,
710
isImportSpecifier,
811
isMemberExpression,
912
isObjectPattern,
1013
isProperty,
11-
ImportModuleNode,
12-
isImportDeclaration,
1314
} from '../node-utils';
1415

1516
export const RULE_NAME = 'no-manual-cleanup';
@@ -110,12 +111,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
110111

111112
return {
112113
'Program:exit'() {
113-
const testingLibraryImportName = helpers.getTestingLibraryImportName();
114114
const customModuleImportNode = helpers.getCustomModuleImportNode();
115115

116-
if (testingLibraryImportName?.match(CLEANUP_LIBRARY_REGEXP)) {
117-
for (const importNode of helpers.getAllTestingLibraryImportNodes()) {
118-
reportCandidateModule(importNode);
116+
for (const testingLibraryImportNode of helpers.getAllTestingLibraryImportNodes()) {
117+
const testingLibraryImportName = getImportModuleName(
118+
testingLibraryImportNode
119+
);
120+
121+
if (testingLibraryImportName?.match(CLEANUP_LIBRARY_REGEXP)) {
122+
reportCandidateModule(testingLibraryImportNode);
119123
}
120124
}
121125

tests/lib/rules/no-manual-cleanup.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ ruleTester.run(RULE_NAME, rule, {
6969
],
7070
} as const)
7171
),
72+
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
73+
(lib) =>
74+
({
75+
code: `
76+
import { render, cleanup } from "${lib}"
77+
import userEvent from "@testing-library/user-event"
78+
`,
79+
errors: [
80+
{
81+
line: 2,
82+
column: 24, // error points to `cleanup`
83+
messageId: 'noManualCleanup',
84+
},
85+
],
86+
} as const)
87+
),
88+
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
89+
(lib) =>
90+
({
91+
code: `
92+
import userEvent from "@testing-library/user-event"
93+
import { render, cleanup } from "${lib}"
94+
`,
95+
errors: [
96+
{
97+
line: 3,
98+
column: 24, // error points to `cleanup`
99+
messageId: 'noManualCleanup',
100+
},
101+
],
102+
} as const)
103+
),
72104
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
73105
(lib) =>
74106
({
@@ -252,5 +284,24 @@ ruleTester.run(RULE_NAME, rule, {
252284
],
253285
} as const)
254286
),
287+
{
288+
code: `
289+
import { cleanup as cleanupVue } from "@testing-library/vue";
290+
import { cleanup as cleanupReact } from "@testing-library/react";
291+
afterEach(() => { cleanupVue(); cleanupReact(); });
292+
`,
293+
errors: [
294+
{
295+
line: 2,
296+
column: 14,
297+
messageId: 'noManualCleanup',
298+
},
299+
{
300+
line: 3,
301+
column: 14,
302+
messageId: 'noManualCleanup',
303+
},
304+
],
305+
},
255306
],
256307
});

0 commit comments

Comments
 (0)