- spyOn<
T extends {},
Key extends string | number | symbol,
A extends "get" | "set" = PropertyAccessors<Key, T>,
Value = Required<T>[Key],
>(
object: T,
method: Key,
accessType: A,
): A extends SetAccessor
? SpyInstance<void, [Value]>
: A extends GetAccessor
? SpyInstance<Value, []>
: Value extends Constructor
? SpyInstance<InstanceType<Value>, ConstructorArgsType<Value>>
: Value extends Func
? SpyInstance<ReturnType<Value>, ArgsType<Value>>
: never Parameters
- object: T
- method: Key
- accessType: A
- spyOn<T extends {}, M extends string | number | symbol>(
object: T,
method: M,
): ConstructorProperties<Required<T>>[M] extends new (...args: any[]) => any
? SpyInstance<
InstanceType<ConstructorProperties<Required<T>>[M]>,
ConstructorArgsType<ConstructorProperties<Required<T>>[M]>,
>
: never Type Parameters
- T extends {}
- M extends string | number | symbol
- spyOn<T extends {}, M extends string | number | symbol>(
object: T,
method: M,
): FunctionProperties<Required<T>>[M] extends Func
? SpyInstance<
ReturnType<FunctionProperties<Required<T>>[M]>,
ArgsType<FunctionProperties<Required<T>>[M]>,
>
: never Type Parameters
- T extends {}
- M extends string | number | symbol
Creates a mock function similar to jest.fn but also tracks calls to
object[methodName]
Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.