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

[jest] Add mock functions for v22 #1918

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -68,7 +74,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 Down Expand Up @@ -48,6 +52,28 @@ foo.doStuff = jest.fn().mockName("10");
// $ExpectError mockName expects a string, not a number
foo.doStuff = jest.fn().mockName(10);

foo.doAsyncStuff = jest.fn().mockResolvedValue(10);
foo.doAsyncStuff = jest.fn().mockReturnValue(Promise.resolve(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);
foo.doAsyncStuff = jest.fn().mockReturnValueOnce(Promise.resolve(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