svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
10.95k stars 1.07k forks source link

Testing overloaded methods is hard #1315

Open Shiuyin opened 3 months ago

Shiuyin commented 3 months ago

I want to test some code that i wrote with SVG.js. I need to use jest.toHaveBeenCalledWith() to see if specific circle methods have been called with the correct params. Unfortunately, .circle() is an overloaded function. TS does not know what is the correct overloaded function one to spy on. So by default it always chooses the last signature known.

There are many posts on the internet that describe the issue like:

The recommended standard is for the most general signature to be the last one. Unfortunately, .circle() with just the size param is not the last one, even though it is the more general one, causing jest to choose the wrong overload in my tests. I managed to workaround it with the 2nd link above, but it's not the greatest of solution.

In might be beneficial to check the typings and make sure that the most general overloaded signature is always the last one.

Fuzzyma commented 2 weeks ago

sorry for the late response. Feel free to create a PR to adapt the typings to your needs. If there is a better way of writing it, a PR is absolutely welcome!