Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
],
},
],
});