Categories
TypeScript Answers

How to use Jasmine’s spyOn upon a private method with TypeScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.