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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *