-
Notifications
You must be signed in to change notification settings - Fork 18
adds no-all-mocks-methods
rule
#204
Changes from all commits
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,35 @@ | ||
# Disallows jest allMocks methods. | ||
|
||
This rule discourages the use of overly broad Jest methods such as `resetAllMocks`, `clearAllMocks`, `restoreAllMocks` and `resetModules`. | ||
|
||
## Rule Details | ||
|
||
These methods are discouraged because there should be explicit connections to the contents of your test file. Mocks should be reset/cleared/restored individually based on the purpose of your test suite. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
jest.resetAllMocks(); | ||
``` | ||
|
||
```js | ||
jest.clearAllMocks(); | ||
``` | ||
|
||
```js | ||
jest.restoreAllMocks(); | ||
``` | ||
|
||
```js | ||
jest.resetModules(); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
jest.mock(); | ||
``` | ||
|
||
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. 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. |
||
## Further Reading | ||
|
||
- [Shopify Jest Best Practices](https://github.com/Shopify/web-foundation/blob/master/Best%20practices/Jest.md#best-practices) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallows jest allMocks methods.', | ||
category: 'Best Practices', | ||
recommended: false, | ||
uri: | ||
'https://github.com/Shopify/eslint-plugin-shopify/blob/master/docs/rules/jest/no-all-mocks-methods.md', | ||
}, | ||
}, | ||
messages: { | ||
allMocksMethod: | ||
'Do not use {{method}} or related methods that are not explicit to a single mock. Instead, clear, reset and restore mocks individually.', | ||
}, | ||
|
||
create(context) { | ||
return { | ||
Identifier(node) { | ||
if (isInvalidMocks(node.name)) { | ||
context.report({ | ||
node, | ||
messageId: 'allMocksMethod', | ||
data: {method: node.name}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function isInvalidMocks(name) { | ||
return [ | ||
'resetAllMocks', | ||
'clearAllMocks', | ||
'restoreAllMocks', | ||
'resetModules', | ||
].some((method) => method === name); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const {RuleTester} = require('eslint'); | ||
|
||
const rule = require('../../../../lib/rules/jest/no-all-mocks-methods'); | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-all-mocks-methods', rule, { | ||
valid: [ | ||
{ | ||
code: `jest.mock()`, | ||
}, | ||
{ | ||
code: `jest.fn()`, | ||
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 rule is fine as is, but you could check that the method is chained off the At the same time, if you solve that problem, a second problem could occur if the methods were destructured off the jest object ( 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. |
||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'jest.resetAllMocks()', | ||
errors: [ | ||
{ | ||
messageId: 'allMocksMethod', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'jest.clearAllMocks()', | ||
errors: [ | ||
{ | ||
messageId: 'allMocksMethod', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'jest.restoreAllMocks()', | ||
errors: [ | ||
{ | ||
messageId: 'allMocksMethod', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'jest.resetModules()', | ||
errors: [ | ||
{ | ||
messageId: 'allMocksMethod', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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.
Is there a better way to describe here the best practices?
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 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 toresetAllMocks
.