Branch coverage is wrong , seems ts-jest transpiles injectables with some sort of branching and differs from tsc
related issues:
100% coverage
Following repo generated with angular cli, added 1 simple button with router action and test for it.
It reports uncovered branch in constructor with injectable Router service.
@danil-z coverage are wrong due to outdated angular preset. I have since a long time some WIP on the angular preset to make it compatible with latest version of ts-jest, but I had to leave it by side as I'm really busy on other projects.
Anyway, it's ok to keep this issue open for others to know, but issue is actually angular preset that needs to be updated to make it compatible with ts-jest >= 23.10. There might already be an issue there tho.
If you could explain it little bit more, or what exactly is required I'd submit them PR
I've tried to update dependency on ts-jest
"jest-preset-angular": "ahnpnl/jest-preset-angular#dependabot/npm_and_yarn/ts-jest-23.10.4",
but still experiencing same coverage bug
here is open ticket about supporting ts-jest >= 23.10 in angular preset
https://github.com/thymikee/jest-preset-angular/issues/195
@danil-z FYI I've posted there little explain and link to WIP PR with my local work so far
@danil-z
FYI - There has been an issue about this on istanbul for almost 2 years. There is also a matching issue on jest, and jasmine, and everything else that depends on it. So, it's not very likely this will be resolved any time soon. Seeing my 50% branch coverage makes me cry myself to sleep at night. Welcome to the party. :-(
fixed in ts-jest >= 23.10.x or jest-preset-angular 7.0.0-alpha.1 (for people who uses jest with Angular)
@ahnpnl Do you know what was the exact fix? I am asking because I am still seeing the issue with coverage for dependency injection.
Hi, the fix in ts-jest starting from 23.10 that ts-jest uses AST transformer + TS compiler internally via isolatedModules config option to fix that coverage issue.
Got it. I've been using this flag actually. Maybe my problem is something different but I'm still seeing the issue:

I have:
globals: {
'ts-jest': {
tsConfig: '<rootDir>/test/unit/tsconfig.json',
ignoreCoverageForDecorators: true,
ignoreCoverageForAllDecorators: true,
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
],
isolatedModules: true,
},
},
preset: 'jest-preset-angular',
Last time I checked the coverage is correct if leaving isolatedModules with false value (default), true value will make the issue appear again.
There is a performance cost to leave it with false value but you will get type checking + coverage issue gone.
@ahnpnl indeed, setting isolatedModules: false fixes coverage (tested running a single spec file) but the downside is the tests are loading forever (almost 10 minutes now) before even running the first suite.
Test Suites: 0 of 302 total
Tests: 0 total
Snapshots: 0 total
Time: 534s
And still counting ...
Is there a chance to make coverage work properly with isolatedModules: true. Also why is it taking that long?
If I do tsc --noEmit -p test/unit/tsconfig.json it runs very fast and I get type checking outside jest.
Most helpful comment
@danil-z
FYI - There has been an issue about this on istanbul for almost 2 years. There is also a matching issue on jest, and jasmine, and everything else that depends on it. So, it's not very likely this will be resolved any time soon. Seeing my 50% branch coverage makes me cry myself to sleep at night. Welcome to the party. :-(