This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 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
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(); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Shopify Jest Best Practices](https://github.com/Shopify/web-foundation/blob/master/Best%20practices/Jest.md#best-practices) |
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,35 @@ | ||
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', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
Identifier(node) { | ||
if (isInvalidMocks(node.name)) { | ||
context.report({ | ||
node, | ||
message: | ||
'Do not use {{method}} or related methods that are not explicit to your test. Instead, target individual mocks.', | ||
data: {method: node.name}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function isInvalidMocks(name) { | ||
return [ | ||
'resetAllMocks', | ||
'clearAllMocks', | ||
'restoreAllMocks', | ||
'resetModules', | ||
].some((method) => method === name); | ||
} |
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 @@ | ||
const {RuleTester} = require('eslint'); | ||
const rule = require('../../../../lib/rules/jest/no-all-mocks-methods'); | ||
|
||
const ruleTester = new RuleTester(); | ||
function errorWithMethodName(name) { | ||
return [ | ||
{ | ||
type: 'Identifier', | ||
message: `Do not use ${name} or related methods that are not explicit to your test. Instead, target individual mocks.`, | ||
}, | ||
]; | ||
} | ||
|
||
ruleTester.run('no-all-mocks-methods', rule, { | ||
valid: [ | ||
{ | ||
code: `jest.mock()`, | ||
}, | ||
{ | ||
code: `jest.fn()`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'jest.resetAllMocks()', | ||
errors: errorWithMethodName('resetAllMocks'), | ||
}, | ||
{ | ||
code: 'jest.clearAllMocks()', | ||
errors: errorWithMethodName('clearAllMocks'), | ||
}, | ||
{ | ||
code: 'jest.restoreAllMocks()', | ||
errors: errorWithMethodName('restoreAllMocks'), | ||
}, | ||
{ | ||
code: 'jest.resetModules()', | ||
errors: errorWithMethodName('resetModules'), | ||
}, | ||
], | ||
}); |