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
Add 'ignoreGuardStatements' option #586
base: main
Are you sure you want to change the base?
Conversation
If it's just one or two inputs you're checking in a test, is leaving a comment telling nyc to ignore the coverage really so spammy? And if you are validating more data, I think you should rely on a validation framework, like joi. Then it would just be one if statement that you'd have to check. And besides - should you not be testing that your guard statements work as you expect? You are not really covering all your bases if not. |
@c-bandy Yes, I personnaly dislike having half my comments being coverage tests ignores. It makes it hard to see actual comments. As it happens, I already extensively use Joi. But:
As for your question ("Should you not be testing...") => Some checks are just impossible (or at least very hard) to test because they're just safety checks what will never happen. |
Then I have to question why you have these safety checks anyway if you don't think they are important enough to test for |
Why would anyone write coverage tests ? Picture this method: const getMember = obj => obj.member; As a developer writing it, I could just decide: ok, lets be nice with others, and add a guard: const getMember = obj => {
if (!obj) {
throw new Error('Dude, you the "obj " argument you've given me is not okay');`
}
return obj.member;
} This guard is not a piece of logic, I dont care to test it, this method will fail either case... it's just here to have nicer logs. On the other hand, if I'm writing those kind of guards, my coverage percentage is dropping... which just encourages me not to write them, and to fill my logs with unexpressive Oh, sure I could write an So why would I write those ? Just to be nice, to add a bit of robusteness, readability and disambiguation. |
But again, I wrote this to fill a need that I had. I can only offer my point of view :) If you tell me "sorry, that's not our philosophy", I wont have anything to add to it, but I really feel that istanbul is enforcing a level of strictness and constraint in those kind of use cases that seems a bit overwhelming. As a user, I would really like it to help me opt-out of this kind of things easily. |
FYI, I'm not really a contributor, or responsible for what goes into the software. I am merely a user. What exactly is it about having multiple comments that is messy? I think you are just being explicit about which behaviour you would like to have. If you don't like the comments, you can remove them. You will have less code coverage, but that code coverage was only really a lie to begin with. It was uncovered code after all. With your solution, you solve your problem in particular but open up a lot of ambiguity in other cases. "Is this always desired behaviour" is a question you have to ask yourself with a global option like this. Because any other user in the same context may not expect, or want this. It sounds a bit too magic for me personally. Just my 2¢. |
In our case, there is not much ambiguity given that most of the developers are not even aware that we have code coverage. Its just a metric that help me to manage people, in order to know who tests well, or who must be encouraged to test more. The other problem when developpers dont pay attention or are not aware of coverage testing: /% istanbul ignore if %/
if (!obj) {
throw new Error('Error !');
} copy/paste it or edit and have something like that: /% istanbul ignore if %/
if (!obj) {
sendEmailToUser('There is an error');
throw new Error('Error !');
} ... well now, a comment not removed hides the fact that a method that had no logic and now has is untested... I really like the idea that if somebody enriches a condition that was too simple to require coverage, it automatically pops in my radar. |
Coverage tests are exactly to ensure every single line is covered - any ignored line is something that's not tested. |
@ljharb I beg to differ... checking that every single line is covered is not a means to an end. There is no point in thinking about lines. In my opinion, when you write code, you dont write "lines", you write your intention. You write business logic. Lines of code are just the mean to achieve your goal. So again in my opinion, it's pointless to think about covering lines. You want to ensure that your business logic has been covered by tests. Not thinking like that leads (you guessed it, "in my opinon") to thinking your tests around your code, not around your logic. Well, I think that it just incentives you to lock down implementation details in unit tests, and to write less expressive tests. That's why I'd like to get everything that's not business logic covering out of the way without having to think about it too much. But I can see how it is a matter of personal preference. That's why I understand this point of view cannot be a default (my pull request is an opt-in feature). However, I'm pretty sure I'm not the only one thinking like that. The others are just kind of happy having 80% coverage. I'm not. I'd like to have a 100% coverage that ignored dummy code without having to write thousands of comments for that. In other words, I dont see any point in checking that code you dont really care about is tested. |
You’re totally right - but error conditions are part of the intention you absolutely want to cover. If you have ignores anywhere, then you don’t actually have 100% coverage - 100% means every line/branch/function/statement. |
You're semantically right. 100% is 100%. You might even be right when you speak to a library developer. You really want to test everything if you want to have a library that exposes a stable api for every single edge case. But why has the I dont feel so much compelled to do so on a business application, on which I dont have time, resource, nor real interest to invest on testing the edge cases so much. It's easy to forget that coverage is NOT unit testing. If an edge case is important, just test it. You dont need your coverage to force you to do it. In those cases, I really find it useful to know which part of my application have significant parts uncovered. |
Motivation
One writes dummy statements like this on a daily basis:
They are quite harmfull, often "useless" (just here to check something that is supposed to never happen), and are, most of the time, useless to check for coverage.
This pull requests allows to pass an option to istanbul instrumenter that systematically ignores such 'guard' statements, without having to clutter our code with thousands of
/* istanbul ignore if */
sSolution
What would you think of the
ignoreGuardStatements
that this pull request introduces ?It works as follows:
Of course, you can combine several of these options to ignore multiple guard patterns (that's why the option is an array).
The git is pretty simple: It ignores automatically any
if
orelse
statement that has a single statement body which matches one of the patterns provided as option (return, break, throw...)