Sometimes, we want to use Jasmine’s spyOn upon a private method with TypeScript.
In this article, we’ll look at how to use Jasmine’s spyOn upon a private method with TypeScript.
How to use Jasmine’s spyOn upon a private method with TypeScript?
To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.
For instance, in our test callback, we write
spyOn<any>(fakePerson, "sayHello");
expect(fakePerson["sayHello"]).toHaveBeenCalled();
to call spyOn with the <any> type parameter to spy on the fakePerson.sayHello method.
Then we call expect with fakePerson["sayHello"] to check that fakePerson.sayHello has been called with toHaveBeenCalled.
Conclusion
To use Jasmine’s spyOn upon a private method with TypeScript, we can put the any type as the type parameter for spyOn.