Skip to content

Resolve "Unit tests coverage: Enable Karma Coverage"

Noel Simmel requested to merge 1287-unit-tests-coverage-enable-karma-coverage into main

Closes #1287 (closed)

Background (for future reference)

This didn't work out of the box because we don't have our component files in app/src, as is the norm for Angular projects. We have them in lib and I had to change sourceRoot in angular.json accordingly, because as it turns out this is where Karma (or karma-coverage?) looks for spec files (see here). The code coverage computation seems to not take into consideration the path specified in angular.json under "test" (this can actually be removed now that I changed sourceRoot).

There might be other ways to include files in the coverage computation (like file parameter in karma config or ng test --include, maybe also parameters in the karma config), but none of them did anything for me.

This doesn't seem to affect any other parts of the pipeline, I was still able to build the app normally. I also couldn't find anything about what else sourceRoot is actually used for in the docs.

I made it so that the code coverage is computed on each run. We can turn that off again with "codeCoverage": false in angular.json, then we'd have to call it explicitly with ng test --code-coverage

Testing

Run yarn install to install the new dependency, then run ng test.

Output (after 480 tests):

=============================== Coverage summary ===============================
Statements   : 35.48% ( 2986/8414 )
Branches     : 22.64% ( 536/2367 )
Functions    : 26.99% ( 711/2634 )
Lines        : 35.84% ( 2657/7412 )
================================================================================

It also creates/updates the ./coverage directory (not part of the git repo). You can open the index.html in your browser and see the test coverage for each component.

Edited by Noel Simmel

Merge request reports

Loading