-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
prefer-mock-promise-shorthand
rule
- Loading branch information
Showing
6 changed files
with
567 additions
and
1 deletion.
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
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,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!')); | ||
``` |
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
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
Oops, something went wrong.