-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use jasmine.spyOn on private/protected functions #14811
Comments
Is spyOn usage on private methods recommended? You could have used a cast ("as any") in the extreme cases when you badly need that, but why should we make it a default behavior? |
Recommend or not, it blocks actual jasmine functionality, which is something typings should never do. If something works in Javascript in must also work in Typescript. There is no other way than to make it the default again, as the function without generic parameter will always be the default if you do not explicitly add a generic parameter to your function call spyOn(console,'log'); // --> declare function spyOn(object: any, method: string): jasmine.Spy;
spyOn<SomeType>(console,'log'); // -->declare function spyOn<T>(object: T, method: keyof T): jasmine.Spy;
// This only works if there is no function with generic parameter declared at all
spyOn(console,'log'); // -->declare function spyOn<T>(object: T, method: keyof T): jasmine.Spy; |
It also does not only affect private or protected functions but also scenarios when a object gets additional functions during runtime with a decorator pattern or in setups where the objects do not use full type support (Typescript is a opt in language after all) Also what does not work with the const test='log';
spyOn(console, test); All in all these changes broke a few of our tests that are totally valid in terms of Javascript runtime. |
@lukas-zech-software, unfortunately I cannot agree with you regarding your previous answer (the next one I haven't read but soon I will). Consider the behavior of it('should fail here or not??', function () {
spyOn({}, 'missingProperty');
}); If you run this test, you are going to get the following error:
That makes me think that by using TypeScript's A bit off the topic, but actually I think we could go even further and make sure the value of the key is a function since that's what Jasmine demands. Regarding your statement I can demonstrate another example:
class Foo {
private prop: string;
} If I follow your logic which says that if I'm able to do that in JavaScript: new Foo().prop = 'value'; then the TypeScript should not prevent me from doing that either, which is not the state of the things because it does prevent you from doing that. Returning to the topic I'm saying that in reality nothing prevents you from modifying objects in a free-style manner, when you are using spyOn<any>(new Foo(), 'prop');
spyOn(new Foo() as any, 'prop'); I'll gladly listen to the opinions of the other maintainers and to your other arguments but personally I would not rush the things because |
@lukas-zech-software , I think that by adding Regarding an example with a variable, it is really sad but still I think we need to collect more opinions from the community and maintainers. I'm saying I might be mistakening, but I tried also to list my considerations here in the thread as well. I don't think there's a 100% right solution which will satisfy everyone, so we just need to agree which is going to be a default: @zhengbli , what's your opinion about the issue? |
I see your point and I too am in favour off having as strong types as possible, actually What caused me to open this issue was, that the new version of the typing break code that is actually fine and working, by changing the default value from As I mentioned before, Typescript is opt-in, use as much typing as you like. It does not enforce the strictest typing possible and needs you explicitly reduce it too a level you're fine with. That said, I can life with the changes but lets hear some other opinions on this. If no one lese follows my arguments, I'll happily close this PR. |
I do agree that the non-generic version should be provided as an option. In TS 2.3 it will be possible to make a default value for the generic type parameter (microsoft/TypeScript#2175), but even with that the resulted use case should be the same: you have two options, to be stricter or not. |
After using the current typings and adapting my code accordingly I now see how bringing back the non-generic version complicates the usage of the generic function way to much to be reasonable. In most cases you will want to use the generic version anyway, the need to explicitly set the generic parameter to get the benefit of it is a real annoyance. Opting out of the typing by using Therefore I will close this issue and the PR. If someone else sees this issue differently he or she may reopen this ticket, but as long as this doesn't happen, I'll assume that everyone ise more happy with the practical approach rather than with the 'academic' one 😉 |
@Lukas-Zech, thanks and also I apologize that I haven't provided some meaningful examples for the problem. Back then I wanted to demonstrate that if we are going to add the any-version of What is bad that I have no stats-based evidence that the generic version is okay in most cases. My only hope is that I provided enough other considerations that make sense. Again, many thanks for the discussion. |
this problem is really annoying. we have a lot of tests relying on spy(s) of protected/private methods. I'v ended up with the following:
could you please make it a part of jasmine typings? it should make both parties happy:
|
@types/2.5.43
package and had problems.Changes introduced in #14481 break functionality of jasmine as it is not longer possible to use
spyOn
functions in classes that are marked asprivate
orprotected
The
keyof
operator introduced in the PR can only seepublic
methodsDetails see this Typescript issue microsoft/TypeScript#13543
Solution is to bring back the un-generic version of
spyOn
and leave the generic version as opt-in overloadPR is coming shortly
The text was updated successfully, but these errors were encountered: