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

Commit

Permalink
adds no-all-mocks-methods rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxee committed Nov 2, 2018
1 parent 31cf086 commit 4896a8e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/rules/jest/no-all-mocks-methods.md
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)
35 changes: 35 additions & 0 deletions lib/rules/jest/no-all-mocks-methods.js
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);
}
41 changes: 41 additions & 0 deletions tests/lib/rules/jest/no-all-mocks-methods.js
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'),
},
],
});

0 comments on commit 4896a8e

Please sign in to comment.