-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add no-manual-cleanup rule (#72)
* feat: start no-manual-cleanup rule * feat: no-manual-cleanup-rule * docs: remove badges no-manual-cleanup * docs: add skip auto cleanup
- Loading branch information
Thomas Lombart
committed
Jan 30, 2020
1 parent
cddf204
commit 050cd4f
Showing
5 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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.", | ||
}, | ||
], | ||
})), | ||
], | ||
}); |