function

test.spyOn

function spyOn<T extends object, K extends string | number | symbol>(
obj: T,
property: K,
accessType: 'get'
): Mock<() => T[K]>;

Create a spy on a property's getter. The spy calls the original getter until you give it another implementation.

spyOn(navigator, "userAgent", "get").mockReturnValue("test");
function spyOn<T extends object, K extends string | number | symbol>(
obj: T,
property: K,
accessType: 'set'
): Mock<(value: T[K]) => void>;

Create a spy on a property's setter. The spy calls the original setter until you give it another implementation.

function spyOn<T extends object, K extends string | number | symbol>(
obj: T,
methodOrPropertyValue: K
): Mock<Extract<T[K], (...args: any[]) => any>>;

Create a spy on an object property or method

Referenced types

type Mock<T extends (...args: any[]) => any> = JestMock.Mock<T>