To make sure typings are correct we use an extension strategy. For this reason if you need your custom spies you need to use our framework to specify and identify them
(this example is taken from jasmine-ts-auto-mock)
To extend a method you need to:
- set your spy function (jasmine.createSpy(name))
Please note that the value returned from provideMethodWithDeferredValue will be a function. You will need to make sure that the method you are providing will not execute the function directly because it will cause Maximum call stack size.
In the example below it's using callFake that will prevent to execute the function directly.
import { Provider } from "ts-auto-mock/extension";
Provider.instance.provideMethodWithDeferredValue((name: string, value: () => any)) => {
return jasmine.createSpy(name).and.callFake(value);
});
- override the type of the return value
type ReturnType = jasmine.Spy;
declare module 'ts-auto-mock/extension' {
interface Method<TR> extends ReturnType {}
}
- create an interface
interface Interface {
methodToSpy: () => string
}
- create a mock
const mock: Interface = createMock<Interface>();
- get the spy from the method You can get the method in two different way
through a function that access to the mock
import { On, method } from "ts-auto-mock/extension";
const spy: jasmine.Spy = On(mock).get(method(mock => mock.methodToSpy));
directly as string
import { On, method } from "ts-auto-mock/extension";
const spy: jasmine.Spy = On(mock).get(method('methodToSpy'));
- trigger the method
someMethodThatWillTriggerInterfaceA();
expect(spy).toHaveBeenCalled();