measuring coverage for entire codebase might be not a good metric, but sometimes we want to ensure that critical parts of our app are covered.
I was thinking about something like
/* @jest-coverage-threshold: 90% */
describe('critical piece of the app', () => {
// ...
});
would that be something we can do in jest?
I believe there is quite a lot of impact to be had if you'd like to work on improving code coverage support in Jest and I'm happy to meet sometime this or next week to chat and also happy to put this on the H2 plan for Jest. Here are a few thoughts about this:
We've had a lot of discussions about this. Generally my team hasn't been excited about this idea so much and there are some concerns:
However, there is a lot of stuff that we need to improve on around coverage, which is, besides mocking and matchers, another unloved part of Jest that we could easily fix and make awesome. Some of the bugs that I have on the list:
jest -i --coverage is broken for some reason.Another solution that I've been thinking about is to run every test twice on a diff (at FB) and compare the coverage percentage and comment on the diff if the threshold is lowered. Instead of running diffs twice, we could also store coverage info in a db, of course. Basically the way I see it is that coverage is an additional tool that helps during review and forcing people to write tests will probably make a lot of people angry. This is why we should the covered lines in phabricator diffs at FB which has been super useful for diff reviews.
I'm gonna close this out for now as it doesn't seem like we'll be investing in it anytime soon. Feel free to reopen if we choose to do this, though.
is there a way to enforce something like no file should have less than 90% coverage?
it seems jest only supports global (i.e. aggregate) thresholds. say I have set global threshold to 90%, I have a hundred files most of them with 100% coverage but have some files with only 20% coverage, the test still passes cause overall coverage is above 90%
Upvote for @dalimian suggestion.
Seconded, this would be really useful to enforce coverage throughout projects
Thirdededed!
Most helpful comment
is there a way to enforce something like no file should have less than 90% coverage?
it seems jest only supports global (i.e. aggregate) thresholds. say I have set global threshold to 90%, I have a hundred files most of them with 100% coverage but have some files with only 20% coverage, the test still passes cause overall coverage is above 90%