Skip to content

Commit

Permalink
Add mockResolvedValue, mockResolvedValueOnce, mockRejectedValue, mock…
Browse files Browse the repository at this point in the history
…RejectedValueOnce to jest mocked functions
  • Loading branch information
julienw committed Mar 6, 2018
1 parent dcd1531 commit 690a542
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
25 changes: 24 additions & 1 deletion definitions/npm/jest_v22.x.x/flow_v0.39.x-/jest_v22.x.x.js
@@ -1,3 +1,9 @@
type ExtractPromiseValueFunc = <Value>(Promise<Value>) => Value;
type ExtractPromiseValue<PromiseValue> = $Call<
ExtractPromiseValueFunc,
PromiseValue
>;

type JestMockFn<TArguments: $ReadOnlyArray<*>, TReturn> = {
(...args: TArguments): TReturn,
/**
Expand Down Expand Up @@ -63,7 +69,24 @@ type JestMockFn<TArguments: $ReadOnlyArray<*>, TReturn> = {
/**
* Sugar for only returning a value once inside your mock
*/
mockReturnValueOnce(value: TReturn): JestMockFn<TArguments, TReturn>
mockReturnValueOnce(value: TReturn): JestMockFn<TArguments, TReturn>,

/**
* Sugar functions for returning a resolved Promise (Jest v22.2+)
*/
mockResolvedValue(
value: ExtractPromiseValue<TReturn>
): JestMockFn<TArguments, TReturn>,

mockResolvedValueOnce(
value: ExtractPromiseValue<TReturn>
): JestMockFn<TArguments, TReturn>,

/**
* Sugar functions for returning a rejected Promise (Jest v22.2+)
*/
mockRejectedValue(value: any): JestMockFn<TArguments, TReturn>,
mockRejectedValueOnce(value: any): JestMockFn<TArguments, TReturn>
};

type JestAsymmetricEqualityType = {
Expand Down
32 changes: 29 additions & 3 deletions definitions/npm/jest_v22.x.x/flow_v0.39.x-/test_jest-v22.x.x.js
Expand Up @@ -13,12 +13,16 @@ jest.atoMockOff();
const mockFn = jest.fn();
mockFn.mock.calls.map(String).map(a => a + a);

type Foo = {
doStuff: string => number
};
type Foo = {|
doStuff: string => number,
doAsyncStuff: void => Promise<number>
|};
const foo: Foo = {
doStuff(str: string): number {
return 5;
},
doAsyncStuff(): Promise<number> {
return Promise.resolve(5);
}
};
foo.doStuff = jest.fn().mockImplementation(str => 10);
Expand All @@ -44,6 +48,28 @@ foo.doStuff = jest.fn().mockReturnValueOnce(10);
// $ExpectError Mock function expected to return number, not string.
foo.doStuff = jest.fn().mockReturnValueOnce("10");

foo.doAsyncStuff = jest.fn().mockResolvedValue(10);
// $ExpectError Mock function expected to return Promise<number>, not Promise<string>.
foo.doAsyncStuff = jest.fn().mockResolvedValue("10");
// $ExpectError Can't return a Promise from a non async function
foo.doStuff = jest.fn().mockResolvedValue(10);

foo.doAsyncStuff = jest.fn().mockResolvedValueOnce(10);
// $ExpectError Mock function expected to return Promise<number>, not Promise<string>.
foo.doAsyncStuff = jest.fn().mockResolvedValueOnce("10");
// $ExpectError Can't return a Promise from a non async function
foo.doStuff = jest.fn().mockResolvedValueOnce(10);

foo.doAsyncStuff = jest.fn().mockRejectedValue(new Error("error"));
// NOTE: No error as Flow doesn't type rejections
foo.doAsyncStuff = jest.fn().mockRejectedValue("10");

foo.doAsyncStuff = jest.fn().mockRejectedValueOnce(new Error("error"));
// NOTE: No error as Flow doesn't type rejections
foo.doAsyncStuff = jest.fn().mockRejectedValueOnce("10");



const mockedDoStuff = (foo.doStuff = jest.fn().mockImplementation(str => 10));
mockedDoStuff.mock.calls[0][0].indexOf("a");
// $ExpectError function `doesntExist` not found in string.
Expand Down

0 comments on commit 690a542

Please sign in to comment.