Why does the source-maps flag effect the code coverage report in Angular 7?
Asked Answered
S

1

6

When I run this command ng test --browsers=ChromeHeadless --source-map=false --code-coverage=true --watch=true, I note that my tests return the following code-coverage summary:

Code Coverage Without Source Maps - Numbers seem right.

enter image description here

However, the code coverage as it appears in my coverage/ directory has some nonsensical highlighting going on. See these examples:

Example 1 Without Source Maps - Highlighting seems wrong. (Only pieces of lines listed as untested?)

enter image description here

Example 2 Without Source Maps - Highlighting seems wrong. (Only pieces of lines listed as untested?)

enter image description here

Now, when I run the following command, ng test --browsers=ChromeHeadless --source-map=true --code-coverage=true --watch=true (NOTE: THE ONLY CHANGE IS THE SOURCE MAP VALUE), I get incorrect code coverage numbers but the highlighting is fixed. See below.

Code Coverage With Source Maps - Numbers seem off.

enter image description here

Example 1 With Source Maps - Highlighting seems right.

enter image description here

Example 2 With Source Maps - Highlighting seems right.

enter image description here

There are other abnormalities that appear while using source maps too, like my test suite registering that it Executed 37 of 35 tests successfully.

Why does using Source Maps cause this much of a difference in my code coverage? Shouldn't the numbers and statistics be the same whether I am using Source Maps or not? Similarly, shouldn't the same lines be highlighted exactly the same way whether I'm using Source Maps or not?

Why the discrepancy?

Sectarian answered 6/5, 2019 at 20:28 Comment(3)
I discovered your question today because I found that your issue also occurs in Angular 8.2.14.Gander
@knight did u get any solution?Disepalous
I hope somebody could give an exlanation why this happens. I'm also getting this behavior.Erroneous
H
1

The reason is that uncovered lines don't make sense to you - because you don't have source map thus karma run's on minified (since it's default behavior of Angular AOT usually) files - and then as it run on minified it cannot correctly match source code with report coverage.

Solution would be to run ng test --browsers=ChromeHeadless --code-coverage=true --watch=true

Hailstone answered 11/3, 2022 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.