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

refactor(jest-mock)!: rename and clean up utility types #12435

Merged
merged 12 commits into from
Feb 23, 2022
10 changes: 6 additions & 4 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export interface Mock<T, Y extends Array<unknown> = Array<unknown>>
(...args: Y): T;
}

type Unpromisify<T> = T extends Promise<infer R> ? R : never;
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved

export interface MockInstance<T, Y extends Array<unknown>> {
_isMockFunction: true;
_protoImpl: Function;
Expand All @@ -142,8 +144,8 @@ export interface MockInstance<T, Y extends Array<unknown>> {
mockReturnThis(): this;
mockReturnValue(value: T): this;
mockReturnValueOnce(value: T): this;
mockResolvedValue(value: Awaited<T>): this;
mockResolvedValueOnce(value: Awaited<T>): this;
mockResolvedValue(value: Unpromisify<T>): this;
mockResolvedValueOnce(value: Unpromisify<T>): this;
mockRejectedValue(value: unknown): this;
mockRejectedValueOnce(value: unknown): this;
}
Expand Down Expand Up @@ -751,7 +753,7 @@ export class ModuleMocker {
// next function call will return this value or default return value
f.mockImplementationOnce(() => value);

f.mockResolvedValueOnce = (value: Awaited<T>) =>
f.mockResolvedValueOnce = (value: Unpromisify<T>) =>
f.mockImplementationOnce(() => Promise.resolve(value as T));

f.mockRejectedValueOnce = (value: unknown) =>
Expand All @@ -761,7 +763,7 @@ export class ModuleMocker {
// next function call will return specified return value or this one
f.mockImplementation(() => value);

f.mockResolvedValue = (value: Awaited<T>) =>
f.mockResolvedValue = (value: Unpromisify<T>) =>
f.mockImplementation(() => Promise.resolve(value as T));

f.mockRejectedValue = (value: unknown) =>
Expand Down