Skip to content

Adding async versions of mockReturnValue & mockreturnValueOnce #5318

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

Merged
merged 5 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 47 additions & 0 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,53 @@ describe('moduleMocker', () => {
expect(fake(2)).toEqual(4);
});

it('supports mocking resolvable async functions', () => {
const fn = moduleMocker.fn();
fn.mockResolvedValue('abcd');

const promise = fn();

expect(promise).toBeInstanceOf(Promise);

return expect(promise).resolves.toBe('abcd');
});

it('supports mocking resolvable async functions only once', () => {
const fn = moduleMocker.fn();
fn.mockResolvedValue('abcd');
fn.mockResolvedValueOnce('abcde');

return Promise.all([
expect(fn()).resolves.toBe('abcde'),
expect(fn()).resolves.toBe('abcd'),
]);
});

it('supports mocking rejectable async functions', () => {
const err = new Error('rejected');
const fn = moduleMocker.fn();
fn.mockRejectedValue(err);

const promise = fn();

expect(promise).toBeInstanceOf(Promise);

return expect(promise).rejects.toBe(err);
});

it('supports mocking rejectable async functions only once', () => {
const defaultErr = new Error('default rejected');
const err = new Error('rejected');
const fn = moduleMocker.fn();
fn.mockRejectedValue(defaultErr);
fn.mockRejectedValueOnce(err);

return Promise.all([
expect(fn()).rejects.toBe(err),
expect(fn()).rejects.toBe(defaultErr),
]);
});

describe('timestamps', () => {
const RealDate = Date;

Expand Down
22 changes: 22 additions & 0 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ 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 @@ -407,6 +418,13 @@ class ModuleMockerClass {
return f;
};

f.mockResolvedValueOnce = wrapAsyncParam(
f.mockReturnValueOnce,
'resolve',
);

f.mockRejectedValueOnce = wrapAsyncParam(f.mockReturnValueOnce, 'reject');

f.mockReturnValue = value => {
// next function call will return specified return value or this one
const mockConfig = this._ensureMockConfig(f);
Expand All @@ -415,6 +433,10 @@ class ModuleMockerClass {
return f;
};

f.mockResolvedValue = wrapAsyncParam(f.mockReturnValue, 'resolve');

f.mockRejectedValue = wrapAsyncParam(f.mockReturnValue, 'reject');

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