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 jest.Spied*
utility types
#62781
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
// Pawel Fajfer <https://github.com/pawfa> | ||
// Alexandre Germain <https://github.com/gerkindev> | ||
// Adam Jones <https://github.com/domdomegg> | ||
// Tom Mrazauskas <https://github.com/mrazauskas> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
// Minimum TypeScript Version: 4.3 | ||
|
||
|
@@ -389,18 +390,18 @@ declare namespace jest { | |
: Value extends Func | ||
? SpyInstance<ReturnType<Value>, ArgsType<Value>> | ||
: never; | ||
function spyOn<T extends {}, M extends FunctionPropertyNames<Required<T>>>( | ||
object: T, | ||
method: M, | ||
): FunctionProperties<Required<T>>[M] extends Func | ||
? SpyInstance<ReturnType<FunctionProperties<Required<T>>[M]>, ArgsType<FunctionProperties<Required<T>>[M]>> | ||
: never; | ||
function spyOn<T extends {}, M extends ConstructorPropertyNames<Required<T>>>( | ||
object: T, | ||
method: M, | ||
): Required<T>[M] extends new (...args: any[]) => any | ||
? SpyInstance<InstanceType<Required<T>[M]>, ConstructorArgsType<Required<T>[M]>> | ||
: never; | ||
function spyOn<T extends {}, M extends FunctionPropertyNames<Required<T>>>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swapping the order here breaks this use case: jest.spyOn(Storage.prototype, 'getItem').mockImplementation((key) => null);
undoing this change breaks let typedSpy1: jest.SpiedClass<typeof globalThis.Date>;
typedSpy1 = jest.spyOn(globalThis, 'Date'); How can we make both work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #63182. Seems like it will make both cases work as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #63196 to address this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed that one, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to add. As you noticed swapping the order made |
||
object: T, | ||
method: M, | ||
): FunctionProperties<Required<T>>[M] extends Func | ||
? SpyInstance<ReturnType<FunctionProperties<Required<T>>[M]>, ArgsType<FunctionProperties<Required<T>>[M]>> | ||
: never; | ||
/** | ||
* Indicates that the module system should never return a mocked version of | ||
* the specified module from require() (e.g. that it should always return the real module). | ||
|
@@ -1166,9 +1167,38 @@ declare namespace jest { | |
interface SpyInstance<T = any, Y extends any[] = any> extends MockInstance<T, Y> {} | ||
|
||
/** | ||
* Represents a function that has been spied on. | ||
* Constructs the type of a spied class. | ||
*/ | ||
type SpiedFunction<T extends (...args: any[]) => any> = SpyInstance<ReturnType<T>, ArgsType<T>>; | ||
type SpiedClass<T extends abstract new (...args: any) => any> = SpyInstance< | ||
InstanceType<T>, | ||
ConstructorParameters<T> | ||
>; | ||
|
||
/** | ||
* Constructs the type of a spied function. | ||
*/ | ||
type SpiedFunction<T extends (...args: any) => any> = SpyInstance<ReturnType<T>, ArgsType<T>>; | ||
|
||
/** | ||
* Constructs the type of a spied getter. | ||
*/ | ||
type SpiedGetter<T> = SpyInstance<T, []>; | ||
|
||
/** | ||
* Constructs the type of a spied setter. | ||
*/ | ||
type SpiedSetter<T> = SpyInstance<void, [T]>; | ||
|
||
/** | ||
* Constructs the type of a spied class or function. | ||
*/ | ||
type Spied<T extends (abstract new (...args: any) => any) | ((...args: any) => any)> = T extends abstract new ( | ||
...args: any | ||
) => any | ||
? SpiedClass<T> | ||
: T extends (...args: any) => any | ||
? SpiedFunction<T> | ||
: never; | ||
|
||
/** | ||
* Wrap a function with mock definitions | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to be notified if types are changing here, so I could adjust types in Jest repo.