-
Notifications
You must be signed in to change notification settings - Fork 155
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
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) | ||
|
Member
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.", | ||
|
||
| }, | ||
| 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.