-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Absolute coverage thresholds #7056
Comments
I think this is the reason why Vitest doesn't support it. And also I think using the negative numbers is fine, but if you can come up with better API we could add that too.
All coverage threholds related logic is in vitest/packages/vitest/src/utils/coverage.ts Lines 339 to 391 in 2fb585a
Example test case: vitest/test/coverage-test/test/threshold-failure.test.ts Lines 1 to 33 in 2fb585a
|
Thanks for the pointers @AriPerkkio - very helpful! I submitted a pull request here: #7061. |
Clear and concise description of the problem
As a developer using Vitest, I want a way to specify absolute coverage thresholds instead of percentage coverage thresholds. The benefit of absolute thresholds is that you can freeze the code coverage at a specific number of uncovered lines, which then ensures that test coverage must be considered for any new additions to the code.
For example, let's say I start working on a package that has 1000 lines, 750 of which are covered (75% coverage), so I set the coverage threshold to 75% to "preserve" this amount of coverage. I then add a new feature with 10 lines, 8 of which are covered. The new coverage percentage is 75.05%, so I pass coverage. However, if only 7 lines of the new feature are covered, then the new coverage percentage is 74.95%, and I fail. This feels a bit arbitrary - I've only enforced that new code additions must have as good of coverage as the existing code.
With absolute coverage thresholds, I would specify my coverage threshold as "250 uncovered lines." When adding a new feature with 10 lines, all 10 lines must be covered (or marked to be ignored in coverage).
In my organization, our approach to code coverage is to strive for 100% coverage unless there's a reasonable tradeoff (e.g. a particular is too hard to reproduce in testing to make it worthwhile, in which case we explicitly mark that case to be ignored). This is easy to do for code that already has 100% coverage. But for any legacy code that doesn't, we can't really implement this policy very well without absolute coverage thresholds.
We are currently migrating from jest (which supports this feature) to vitest. We are loving vitest but are sad to lose this important piece of functionality.
Suggested solution
Jest supports configuring a coverage threshold with a positive number, in which case its interpreted as a percentage, or a negative number, in which case its interpreted as an absolute threshold.
The advantage of this approach is that it doesn't add any new configuration options. The disadvantage is that it's not totally clear what's happening unless you read the docs.
I think it would also make sense to have a more explicit configuration option to use absolute or percentage thresholds in the coverage config. E.g. something like
coverage.thresholdType: 'percentage' | 'absolute'
.I haven't yet looked into how to implement this in the code base, but I'd be happy to take the lead on researching and implementing the feature once its agreed that it would be a useful addition.
Alternative
No response
Additional context
Thank you for your time and consideration!
Validations
The text was updated successfully, but these errors were encountered: