feat(@jest/globals, jest-mock): add jest.Spied* utility types by mrazauskas · Pull Request #13440 · jestjs/jest (original) (raw)
Summary
@types/jest provides jest.SpyInstance<T> and jest.SpiedFunction<T> utility types. Jest currently does not exposed these on the jest namespace.
Note that jest.SpyInstance<T> works only on methods, but can’t be used on class constructors, getters and setters. It is equal to jest.SpiedFunction<T> only that jest.SpyInstance<T> takes two type arguments instead of one.
To cover more use cases, I created jest.SpiedClass<T>, jest.SpiedFunction<T>, jest.SpiedGetter<T>, jest.SpiedSetter<T>. Names are similar to jest.Mocked* utilities.
jest.Mocked<T> is very smart type. I wanted to have somewhat similar jest.Spied<T>. It works only with classes and functions, because there is no way to differentiate between a getter and a setter. I mean, either of them would simply have type of string or number not a function signature. Hence user will have to use jest.SpiedGetter<T> or jest.SpiedSetter<T>.
Test plan
Type test added.