Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement mockResolved/RejectedValue methods using mockImplementation #5720

Merged
merged 1 commit into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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