mindparse
mindparse

Reputation: 7225

Angular CLI - Get coverage report to include all sources

I am trying to figure out how to include all my .ts sources in the generated coverage report from the angular CLI. Currently I am only getting coverage for files that have an associated spec with tests against.

I have tried adding the includeAllSources flag to my karma.conf.js file but this made no difference.

Whats the correct approach here? I am using Angular CLI 6.1.5

Thanks

Upvotes: 11

Views: 4789

Answers (2)

Ihor
Ihor

Reputation: 3809

Solution provided by Narm is really great, however still not ideal, as requires importing manually at least all the lazy loaded modules + root one to the tests and also on big projects there are almost always some non-used/forgotten components which are not part of any production code and which will leave in the source code for ever untested/undiscovered.

Solution:

https://github.com/kopach/karma-sabarivka-reporter.

To use it → install npm install --save-dev karma-sabarivka-reporter

And update karma.conf.js as described here https://github.com/kopach/karma-sabarivka-reporter#-usage

After this – all files matching pattern will be included into final coverage result

Upvotes: 5

Narm
Narm

Reputation: 10844

The simplest solution that worked for me on Angular(v6) was just adding a app.module.spec.ts file to compliment your app.module.ts and within that .spec include the following code

import './app.module';

Apparently due to the fact app.module.ts is the root of your application including a .spec for that module will result in the inclusion of all your files during code coverage (ng test --code-coverage)

Upvotes: 22

Related Questions