-
Notifications
You must be signed in to change notification settings - Fork 135
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
feat: add no-manual-cleanup rule #72
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Disallow the use of `cleanup` (no-manual-cleanup) | ||
|
||
TODO: SUMMARY | ||
|
||
## Rule Details | ||
|
||
TODO: DETAILS | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
// TODO: EXAMPLES | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
// TODO: EXAMPLES | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [cleanup API in React Testing Library](https://testing-library.com/docs/react-testing-library/api#cleanup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add skipping auto cleanup here: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const { getDocsUrl } = require('../utils'); | ||
|
||
const findCleanupSpecifier = specifiers => { | ||
return specifiers.find(specifier => specifier.imported.name === 'cleanup'); | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: ' Disallow the use of `cleanup`', | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: getDocsUrl('no-manual-cleanup'), | ||
}, | ||
messages: { | ||
noManualCleanup: | ||
"`cleanup` is performed automatically after each test by {{library}}, you don't need manual cleanups.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not right: the automatic cleanup is performed depending on your testing framework, not on your testing library package. This rule should just mention that manual cleanups are dissalowed as the testing framework taking care of it automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it's true the automatic cleanup is performed depending on the testing framework, not all testing libraries package have a It does not make sense for me to make this rule fire even if the testing library package doesn't have a I agree this rule should be removed from shareable configs as we don't know the test runner used though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough about short-circuiting when importing from a testing library package without (Damn, "testing library" and "testing framework" are not making easier this discussion). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll fix it too, thanks for the feedback! 👍 By the way, there are some rules like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's actually one of the reasons I don't like to write code for checking specific testing library packages as if a new one comes up tomorrow we could have to update rules. I prefer to leave them agnostic from any package, specially those not enabled by default, so users can set them up under their responsibility. Of course, we should mention in proper rule doc which are those testing library packages supposed to be compatible with the rule for clarification. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is that we have shareable configs for Angular, Vue, etc. But we are not adding any support for Svelte, Preact, etc. I think we should extend the rules that already work with specific packages (such as no-debug, or no-dom-import) to other potential testing libraries 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah of course 🤦♂️ And probably we should open an issue about including more frameworks for sure! |
||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
|
||
create: function(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
const testingLibraryWithCleanup = node.source.value.match( | ||
/@testing-library\/(react|vue)/ | ||
); | ||
|
||
// Early return if the library doesn't support `cleanup` | ||
if (!testingLibraryWithCleanup) { | ||
return; | ||
} | ||
|
||
const cleanupSpecifier = findCleanupSpecifier(node.specifiers); | ||
|
||
if (cleanupSpecifier) { | ||
context.report({ | ||
node: cleanupSpecifier, | ||
messageId: 'noManualCleanup', | ||
data: { | ||
library: testingLibraryWithCleanup[0], | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'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, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-manual-cleanup', rule, { | ||
valid: [ | ||
{ | ||
code: `import { render } from "@testing-library/react"`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { render, cleanup } from "@testing-library/react"`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
column: 18, // error points to `cleanup` | ||
message: | ||
"`cleanup` is performed automatically after each test by @testing-library/react, you don't need manual cleanups.", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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.
I don't think we should enable this rule for any shareable config for reasons explained in the original issue.