-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add no-manual-cleanup rule #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Disallow the use of `cleanup` (no-manual-cleanup) | ||
|
||
`cleanup` is performed automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). In this case, it's unnecessary to do manual cleanups after each test unless you skip the auto-cleanup with environment variables such as `RTL_SKIP_AUTO_CLEANUP` for React. | ||
|
||
## Rule Details | ||
|
||
This rule disallows the import/use of `cleanup` in your test files. It fires if you import `cleanup` from one of these libraries: | ||
|
||
- [Marko Testing Library](https://testing-library.com/docs/marko-testing-library/api#cleanup) | ||
- [Preact Testing Library](https://testing-library.com/docs/preact-testing-library/api#cleanup) | ||
- [React Testing Library](https://testing-library.com/docs/react-testing-library/api#cleanup) | ||
- [Svelte Testing Library](https://testing-library.com/docs/svelte-testing-library/api#cleanup) | ||
- [Vue Testing Library](https://testing-library.com/docs/vue-testing-library/api#cleanup) | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
import { cleanup } from '@testing-library/react'; | ||
|
||
const { cleanup } = require('@testing-library/react'); | ||
|
||
import utils from '@testing-library/react'; | ||
afterEach(() => utils.cleanup()); | ||
|
||
const utils = require('@testing-library/react'); | ||
afterEach(utils.cleanup); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
import { cleanup } from 'any-other-library'; | ||
|
||
const utils = require('any-other-library'); | ||
utils.cleanup(); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [cleanup API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#cleanup) | ||
- [Skipping Auto Cleanup](https://testing-library.com/docs/react-testing-library/setup#skipping-auto-cleanup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
const CLEANUP_LIBRARY_REGEX = /(@testing-library\/(preact|react|svelte|vue))|@marko\/testing-library/; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: ' Disallow the use of `cleanup`', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noManualCleanup: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
|
||
create: function(context) { | ||
let defaultImportFromTestingLibrary; | ||
let defaultRequireFromTestingLibrary; | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const testingLibraryWithCleanup = node.source.value.match( | ||
CLEANUP_LIBRARY_REGEX | ||
); | ||
|
||
// Early return if the library doesn't support `cleanup` | ||
if (!testingLibraryWithCleanup) { | ||
return; | ||
} | ||
|
||
if (node.specifiers[0].type === 'ImportDefaultSpecifier') { | ||
defaultImportFromTestingLibrary = node; | ||
} | ||
|
||
const cleanupSpecifier = node.specifiers.find( | ||
specifier => | ||
specifier.imported && specifier.imported.name === 'cleanup' | ||
); | ||
|
||
if (cleanupSpecifier) { | ||
context.report({ | ||
node: cleanupSpecifier, | ||
messageId: 'noManualCleanup', | ||
}); | ||
} | ||
}, | ||
VariableDeclarator(node) { | ||
if ( | ||
node.init && | ||
node.init.callee && | ||
node.init.callee.name === 'require' | ||
) { | ||
const requiredModule = node.init.arguments[0]; | ||
const testingLibraryWithCleanup = requiredModule.value.match( | ||
CLEANUP_LIBRARY_REGEX | ||
); | ||
|
||
// Early return if the library doesn't support `cleanup` | ||
if (!testingLibraryWithCleanup) { | ||
return; | ||
} | ||
|
||
if (node.id.type === 'ObjectPattern') { | ||
const cleanupProperty = node.id.properties.find( | ||
property => property.key.name === 'cleanup' | ||
); | ||
if (cleanupProperty) { | ||
context.report({ | ||
node: cleanupProperty, | ||
messageId: 'noManualCleanup', | ||
}); | ||
} | ||
} else { | ||
defaultRequireFromTestingLibrary = node.id; | ||
} | ||
} | ||
}, | ||
'Program:exit'() { | ||
if (defaultImportFromTestingLibrary) { | ||
const references = context.getDeclaredVariables( | ||
defaultImportFromTestingLibrary | ||
)[0].references; | ||
|
||
reportImportReferences(context, references); | ||
} | ||
|
||
if (defaultRequireFromTestingLibrary) { | ||
const references = context | ||
.getDeclaredVariables(defaultRequireFromTestingLibrary.parent)[0] | ||
.references.slice(1); | ||
|
||
reportImportReferences(context, references); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function reportImportReferences(context, references) { | ||
if (references && references.length > 0) { | ||
references.forEach(reference => { | ||
const utilsUsage = reference.identifier.parent; | ||
if ( | ||
utilsUsage && | ||
utilsUsage.property && | ||
utilsUsage.property.name === 'cleanup' | ||
) { | ||
context.report({ | ||
node: utilsUsage.property, | ||
messageId: 'noManualCleanup', | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-manual-cleanup'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
|
||
const ALL_TESTING_LIBRARIES_WITH_CLEANUP = [ | ||
'@testing-library/preact', | ||
'@testing-library/react', | ||
'@testing-library/svelte', | ||
'@testing-library/vue', | ||
'@marko/testing-library', | ||
]; | ||
|
||
ruleTester.run('no-manual-cleanup', rule, { | ||
valid: [ | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `import { render } from "${lib}"`, | ||
})), | ||
{ | ||
code: `import { cleanup } from "any-other-library"`, | ||
}, | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `import utils from "${lib}"`, | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: ` | ||
import utils from "${lib}" | ||
utils.render() | ||
`, | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `const { render, within } = require("${lib}")`, | ||
})), | ||
{ | ||
code: `const { cleanup } = require("any-other-library")`, | ||
}, | ||
{ | ||
code: ` | ||
const utils = require("any-other-library") | ||
utils.cleanup() | ||
`, | ||
}, | ||
{ | ||
// For test coverage | ||
code: `const utils = render("something")`, | ||
}, | ||
], | ||
invalid: [ | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `import { render, cleanup } from "${lib}"`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 18, // error points to `cleanup` | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `import { cleanup as myCustomCleanup } from "${lib}"`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 10, // error points to `cleanup` | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `import utils, { cleanup } from "${lib}"`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 17, // error points to `cleanup` | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: ` | ||
import utils from "${lib}" | ||
afterEach(() => utils.cleanup()) | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
column: 31, | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: ` | ||
import utils from "${lib}" | ||
afterEach(utils.cleanup) | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
column: 25, | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: `const { cleanup } = require("${lib}")`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 9, // error points to `cleanup` | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: ` | ||
const utils = require("${lib}") | ||
afterEach(() => utils.cleanup()) | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
column: 31, | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ | ||
code: ` | ||
const utils = require("${lib}") | ||
afterEach(utils.cleanup) | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
column: 25, | ||
message: | ||
"`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", | ||
}, | ||
], | ||
})), | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add skipping auto cleanup here:
Skipping Auto Cleanup