Skip to content

Commit

Permalink
feat: create prefer-mock-promise-shorthand rule
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jul 15, 2022
1 parent 87d845d commit 087b1c2
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ installations requiring long-term consistency.
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | ![fixable][] |
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | ![fixable][] |
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | |
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
Expand Down
34 changes: 34 additions & 0 deletions docs/rules/prefer-mock-promise-shorthand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Prefer mock resolved/rejected shorthands for promises (`prefer-mock-promise-shorthand`)

When working with mocks of functions that return promises, Jest provides some
syntactic sugar functions to reduce the amount of boilerplate you have to write.

These methods should be preferred when possible.

## Rule Details

The following patterns are warnings:

```js
jest.fn().mockImplementation(() => Promise.resolve(123));
jest
.spyOn(fs.promises, 'readFile')
.mockReturnValue(Promise.reject(new Error('oh noes!')));

myFunction
.mockReturnValueOnce(Promise.resolve(42))
.mockImplementationOnce(() => Promise.resolve(42))
.mockReturnValue(Promise.reject(new Error('too many calls!')));
```

The following patterns are not warnings:

```js
jest.fn().mockResolvedValue(123);
jest.spyOn(fs.promises, 'readFile').mockRejectedValue(new Error('oh noes!'));

myFunction
.mockResolvedValueOnce(42)
.mockResolvedValueOnce(42)
.mockRejectedValue(new Error('too many calls!'));
```
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/rules.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Object {
"jest/prefer-hooks-in-order": "error",
"jest/prefer-hooks-on-top": "error",
"jest/prefer-lowercase-title": "error",
"jest/prefer-mock-promise-shorthand": "error",
"jest/prefer-snapshot-hint": "error",
"jest/prefer-spy-on": "error",
"jest/prefer-strict-equal": "error",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { resolve } from 'path';
import plugin from '../';

const numberOfRules = 49;
const numberOfRules = 50;
const ruleNames = Object.keys(plugin.rules);
const deprecatedRules = Object.entries(plugin.rules)
.filter(([, rule]) => rule.meta.deprecated)
Expand Down
Loading

0 comments on commit 087b1c2

Please sign in to comment.