Skip to content

Commit

Permalink
implement mockResolved/RejectedValue methods using mockImplementation (
Browse files Browse the repository at this point in the history
  • Loading branch information
javinor authored and cpojer committed Mar 4, 2018
1 parent 52dad15 commit e9aa321
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
([#5670](https://github.com/facebook/jest/pull/5670))
* `[jest-runtime]` Prevent Babel warnings on large files
([#5702](https://github.com/facebook/jest/pull/5702))
* `[jest-mock]` Prevent `mockRejectedValue` from causing unhandled rejection
([#5720](https://github.com/facebook/jest/pull/5720))

### Chore & Maintenance

Expand Down
8 changes: 4 additions & 4 deletions docs/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
Simple sugar function for:

```js
jest.fn().mockReturnValue(Promise.resolve(value));
jest.fn().mockImplementation(() => Promise.resolve(value));
```

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

```js
jest.fn().mockReturnValueOnce(Promise.resolve(value));
jest.fn().mockImplementationOnce(() => Promise.resolve(value));
```

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

```js
jest.fn().mockReturnValue(Promise.reject(value));
jest.fn().mockImplementation(() => Promise.reject(value));
```

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

```js
jest.fn().mockReturnValueOnce(Promise.reject(value));
jest.fn().mockImplementationOnce(() => Promise.reject(value));
```

Example usage:
Expand Down
26 changes: 8 additions & 18 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,6 @@ function getSlots(object?: Object): Array<string> {
return Object.keys(slots);
}

function wrapAsyncParam(
fn: any => any,
asyncAction: 'resolve' | 'reject',
): any => any {
if (asyncAction === 'reject') {
return value => fn(Promise.reject(value));
}

return value => fn(Promise.resolve(value));
}

class ModuleMockerClass {
_environmentGlobal: Global;
_mockState: WeakMap<Function, MockFunctionState>;
Expand Down Expand Up @@ -418,12 +407,11 @@ class ModuleMockerClass {
return f;
};

f.mockResolvedValueOnce = wrapAsyncParam(
f.mockReturnValueOnce,
'resolve',
);
f.mockResolvedValueOnce = value =>
f.mockImplementationOnce(() => Promise.resolve(value));

f.mockRejectedValueOnce = wrapAsyncParam(f.mockReturnValueOnce, 'reject');
f.mockRejectedValueOnce = value =>
f.mockImplementationOnce(() => Promise.reject(value));

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

f.mockResolvedValue = wrapAsyncParam(f.mockReturnValue, 'resolve');
f.mockResolvedValue = value =>
f.mockImplementation(() => Promise.resolve(value));

f.mockRejectedValue = wrapAsyncParam(f.mockReturnValue, 'reject');
f.mockRejectedValue = value =>
f.mockImplementation(() => Promise.reject(value));

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

0 comments on commit e9aa321

Please sign in to comment.