Skip to content
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

Merged
merged 4 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
[![Tweet][tweet-badge]][tweet-url]

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

## Installation
Expand Down Expand Up @@ -142,6 +144,7 @@ To enable this configuration use the `extends` property in your
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
| [no-get-by-for-checking-element-not-present](docs/rules/no-get-by-for-checking-element-not-present) | Disallow the use of `getBy*` queries when checking elements are not present | | |
| [no-manual-cleanup](docs/rules/no-manual-cleanup.md) | Disallow the use of `cleanup` | ![react-badge][] ![vue-badge][] | |
Copy link
Member

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.

| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |

[build-badge]: https://img.shields.io/travis/testing-library/eslint-plugin-testing-library?style=flat-square
Expand Down Expand Up @@ -190,6 +193,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
23 changes: 23 additions & 0 deletions docs/rules/no-manual-cleanup.md
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)
Copy link
Member

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

3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rules = {
'no-debug': require('./rules/no-debug'),
'no-dom-import': require('./rules/no-dom-import'),
'no-get-by-for-checking-element-not-present': require('./rules/no-get-by-for-checking-element-not-present'),
'no-manual-cleanup': require('./rules/no-manual-cleanup'),
'prefer-explicit-assert': require('./rules/prefer-explicit-assert'),
};

Expand Down Expand Up @@ -39,6 +40,7 @@ module.exports = {
...recommendedRules,
'testing-library/no-debug': 'warn',
'testing-library/no-dom-import': ['error', 'react'],
'testing-library/no-manual-cleanup': 'warn',
},
},
vue: {
Expand All @@ -48,6 +50,7 @@ module.exports = {
'testing-library/await-fire-event': 'error',
'testing-library/no-debug': 'warn',
'testing-library/no-dom-import': ['error', 'vue'],
'testing-library/no-manual-cleanup': 'warn',
},
},
},
Expand Down
52 changes: 52 additions & 0 deletions lib/rules/no-manual-cleanup.js
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.",
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

@thomaslombart thomaslombart Jan 29, 2020

Choose a reason for hiding this comment

The 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 cleanup method, like Angular's testing library.

It does not make sense for me to make this rule fire even if the testing library package doesn't have a cleanup method.

I agree this rule should be removed from shareable configs as we don't know the test runner used though.

Copy link
Member

Choose a reason for hiding this comment

The 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 cleanup, but still the error message is wrong as the one handling the automatic cleanup is the testing framework (e.g. jest), not testing library package (e.g. @testing-library/angular).

(Damn, "testing library" and "testing framework" are not making easier this discussion).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 no-debug where we don't support frameworks like Svelte Testing Library, we should probably open a new issue to include them.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 🙂

Copy link
Member

Choose a reason for hiding this comment

The 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],
},
});
}
},
};
},
};
2 changes: 2 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Object {
"error",
"react",
],
"testing-library/no-manual-cleanup": "warn",
},
}
`;
Expand Down Expand Up @@ -64,6 +65,7 @@ Object {
"error",
"vue",
],
"testing-library/no-manual-cleanup": "warn",
},
}
`;
43 changes: 43 additions & 0 deletions tests/lib/rules/no-manual-cleanup.js
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.",
},
],
},
],
});