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

Add spyOn function introduced in Jest 19 #14867

merged 5 commits into from Mar 12, 2017
Diff settings


Just for now


Add spyOn function introduced in Jest 19

  • Loading branch information...
alexjoverm committed Feb 25, 2017
commit 46f23ff159f5944f09d366b4385b4df9bcef3ed2
Copy path View file
2 jest/index.d.ts 100644 → 100755
@@ -83,6 +83,8 @@ declare namespace jest {
function useFakeTimers(): typeof jest;
/** Instructs Jest to use the real versions of the standard timer functions. */
function useRealTimers(): typeof jest;
/** Creates a mock function similar to jest.fn but also tracks calls to object[methodName] */
function spyOn(object: any, method: string): typeof jest;

This comment has been minimized.


asvetliakov Feb 25, 2017


I'd suggest this

function spyOn<T extends {}, M extends keyof T>(object: T, method: M): MockInstance<T[M]>;

Also return type typeof jest is wrong. jest.spyOn returns mock/spy instance not the global jest object

Better T extends object but this require TS 2.2 and PR won't be merged until 4-6 weeks passed

This comment has been minimized.


alexjoverm Feb 25, 2017

Author Contributor

@asvetliakov you're right about the return type, my mistake.

About the other thing, I see the benefit so you assure is an existing property of that object. Does that take into account custom properties? Because I assume T extends {} creates a new Object type.

Anyway, if that'll take that much time, I could always create another PR by that time, but at least if we merge this now spyOn will be usable in ts-jest.

This comment has been minimized.


asvetliakov Feb 25, 2017


T extends {} infers type from parameter, so it will be your type that you had passed into spyOn. keyof T will filter properties for this type only.

interface MockOptions {
virtual?: boolean;
ProTip! Use n and p to navigate between commits in a pull request.