Skip to content

Commit e9aa321

Browse files
javinorcpojer
authored andcommitted
implement mockResolved/RejectedValue methods using mockImplementation (#5720)
1 parent 52dad15 commit e9aa321

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
([#5670](https://github.com/facebook/jest/pull/5670))
3434
* `[jest-runtime]` Prevent Babel warnings on large files
3535
([#5702](https://github.com/facebook/jest/pull/5702))
36+
* `[jest-mock]` Prevent `mockRejectedValue` from causing unhandled rejection
37+
([#5720](https://github.com/facebook/jest/pull/5720))
3638

3739
### Chore & Maintenance
3840

docs/MockFunctionAPI.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
250250
Simple sugar function for:
251251

252252
```js
253-
jest.fn().mockReturnValue(Promise.resolve(value));
253+
jest.fn().mockImplementation(() => Promise.resolve(value));
254254
```
255255

256256
Useful to mock async functions in async tests:
@@ -270,7 +270,7 @@ test('async test', async () => {
270270
Simple sugar function for:
271271

272272
```js
273-
jest.fn().mockReturnValueOnce(Promise.resolve(value));
273+
jest.fn().mockImplementationOnce(() => Promise.resolve(value));
274274
```
275275

276276
Useful to resolve different values over multiple async calls:
@@ -297,7 +297,7 @@ test('async test', async () => {
297297
Simple sugar function for:
298298

299299
```js
300-
jest.fn().mockReturnValue(Promise.reject(value));
300+
jest.fn().mockImplementation(() => Promise.reject(value));
301301
```
302302

303303
Useful to create async mock functions that will always reject:
@@ -317,7 +317,7 @@ test('async test', async () => {
317317
Simple sugar function for:
318318

319319
```js
320-
jest.fn().mockReturnValueOnce(Promise.reject(value));
320+
jest.fn().mockImplementationOnce(() => Promise.reject(value));
321321
```
322322

323323
Example usage:

packages/jest-mock/src/index.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,6 @@ function getSlots(object?: Object): Array<string> {
227227
return Object.keys(slots);
228228
}
229229

230-
function wrapAsyncParam(
231-
fn: any => any,
232-
asyncAction: 'resolve' | 'reject',
233-
): any => any {
234-
if (asyncAction === 'reject') {
235-
return value => fn(Promise.reject(value));
236-
}
237-
238-
return value => fn(Promise.resolve(value));
239-
}
240-
241230
class ModuleMockerClass {
242231
_environmentGlobal: Global;
243232
_mockState: WeakMap<Function, MockFunctionState>;
@@ -418,12 +407,11 @@ class ModuleMockerClass {
418407
return f;
419408
};
420409

421-
f.mockResolvedValueOnce = wrapAsyncParam(
422-
f.mockReturnValueOnce,
423-
'resolve',
424-
);
410+
f.mockResolvedValueOnce = value =>
411+
f.mockImplementationOnce(() => Promise.resolve(value));
425412

426-
f.mockRejectedValueOnce = wrapAsyncParam(f.mockReturnValueOnce, 'reject');
413+
f.mockRejectedValueOnce = value =>
414+
f.mockImplementationOnce(() => Promise.reject(value));
427415

428416
f.mockReturnValue = value => {
429417
// next function call will return specified return value or this one
@@ -433,9 +421,11 @@ class ModuleMockerClass {
433421
return f;
434422
};
435423

436-
f.mockResolvedValue = wrapAsyncParam(f.mockReturnValue, 'resolve');
424+
f.mockResolvedValue = value =>
425+
f.mockImplementation(() => Promise.resolve(value));
437426

438-
f.mockRejectedValue = wrapAsyncParam(f.mockReturnValue, 'reject');
427+
f.mockRejectedValue = value =>
428+
f.mockImplementation(() => Promise.reject(value));
439429

440430
f.mockImplementationOnce = fn => {
441431
// next function call will use this mock implementation return value

0 commit comments

Comments
 (0)