Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

adds no-all-mocks-methods rule #204

Merged
merged 3 commits into from
Nov 14, 2019
Merged

adds no-all-mocks-methods rule #204

merged 3 commits into from
Nov 14, 2019

Conversation

jaxee
Copy link
Contributor

@jaxee jaxee commented Nov 2, 2018

Closes part of https://github.com/Shopify/eslint-plugin-shopify/issues/181
For more information see: https://github.com/Shopify/web-foundation/blob/master/Best%20practices/Jest.md#best-practices

Do not use jest.resetAllMocks(), jest.clearAllMocks(), jest.restoreAllMocks(), and jest.resetModules().

Why? These methods are overly broad and do not have explicit connections to the contents of your test file. Instead, reset/ clear/ restore individual mocks that are set up explicitly for the purposes of your test suite.

First time adding a rule, feedback welcomed 🙏

cc @djirdehh

The following patterns are not warnings:

```js
jest.mock();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to describe here the best practices?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, you could go one step further and fill in the alternative for each method you are restricting here. For example, showing a full example using mockReset(); as the alternative to resetAllMocks.

Copy link
Contributor

@cartogram cartogram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it took me so long to review this. 😞

Nothing major on my end, this looks great! It was very cool working with you on this one.

The following patterns are not warnings:

```js
jest.mock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, you could go one step further and fill in the alternative for each method you are restricting here. For example, showing a full example using mockReset(); as the alternative to resetAllMocks.

```js
jest.mock();
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, there is a note here about when to its safe to disable this rule. Its pretty template-y but I would add it for consistency.

context.report({
node,
message:
'Do not use {{method}} or related methods that are not explicit to your test. Instead, target individual mocks.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could be even more specific here. Suggestion:Do not use {{method}} or related methods that are not explicit to a single mock. Instead, clear, reset and restore mocks individually.

code: `jest.mock()`,
},
{
code: `jest.fn()`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule is fine as is, but you could check that the method is chained off the jest object. Right now, if there were a method in your application called clearAllMocks (completely unrelated to jest) that would be considered invalid. This is highly unlikely, but you could alter this rule to prevent against it.

At the same time, if you solve that problem, a second problem could occur if the methods were destructured off the jest object (const {clearAllMocks} = jest; or const clearAllMocks = jest.clearAllMocks or even somethingTotallyDifferent = jest.clearAllMocks). I think your rule will catch this right now.

You could protect against all of the above at the same time, but it is a lot more complex and I don't anticipate too many problems with the rule as is. However, it would be a fun challenge. I am also more than happy to pair again on it.

@cartogram
Copy link
Contributor

@jaxee Thanks for your help with this! 🙏 I am going to quickly wrap up the final things so that we can get this out of the PR list. 🙂

@cartogram cartogram merged commit 74f182b into master Nov 14, 2019
@cartogram cartogram deleted the fix-no-all-mocks-methods branch November 14, 2019 07:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants