Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adding async utility versions of mockReturnValue & mockreturnValueOnce
  • Loading branch information
m4n3z40 authored and SimenB committed Jan 21, 2018
commit 0ac07e4d279a013d85acf958c5c03977c072d86e
49 changes: 49 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,55 @@ 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 promise.then(value => expect(value).toBe('abcd'));
});

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

const promise1 = fn().then(value => expect(value).toBe('abcde'));
const promise2 = fn().then(value => expect(value).toBe('abcd'));

return Promise.all([promise1, promise2]);
});

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 promise.catch(rejection => expect(rejection).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);

const promise1 = fn().catch(rejection => expect(rejection).toBe(err));
const promise2 = fn().catch(rejection =>
expect(rejection).toBe(defaultErr),
);

return Promise.all([promise1, promise2]);
});

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