Setting Up Your Angular CLI App to Generate Code Coverage Reports Any Time Tests Are Run in a Workspace with Multiple Projects

I currently do development in an Angular CLI application that contains multiple library projects, and recently enabled code coverage reports to be generated automatically every time unit tests were run. This consisted of 4 steps:

  1. Turn on code coverage for each project.
  2. Exclude the libraries from coverage in projects that utilized them (each library has its own coverage).
  3. Set the output directory for each project's code coverage report to a sub-directory of the code coverage directory to avoid overwriting files for each report generated.
  4. Make additions to ignore the code coverage directory from source control.
Here's each of these steps in detail.

Step 1: Turn on code coverage for each project
In angular.json, go to the architect/test/options node for each project and add a property called codeCoverage if it's not already there. Set the value to true, as shown below:

"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "projects/product-lib/src/test.ts",
"tsConfig": "projects/product-lib/tsconfig.spec.json",
"karmaConfig": "projects/product-lib/karma.conf.js",
"codeCoverage": true

Step 2: Exclude utilized libraries from coverage
If you have a project that is utilizing library projects also contained within the workspace, you'll want to exclude them from the code coverage analysis of the projects that make use of them (but make sure they have their own code coverage report). Not only will this prevent extra, unnecessary analysis, it will also most likely prevent a "RangeError: Maximum call stack size exceeded" error from Karma. You can exclude any library projects using the codeCoverageExclude property. Below is an example where I exclude all of the library projects from my main app's code coverage:

"codeCoverage": true,
"codeCoverageExclude": [
"dist/org-angular-core/**/*",
"dist/attestation-lib/**/*",
"dist/product-lib/**/*",
"dist/profile-lib/**/*",
"dist/program-lib/**/*"
]

Step 3: Set the output directory for each project's code coverage report to a sub-directory of the code coverage directory
By default, all code coverage reports are generated in the coverage directory at the root of your workspace. By changing the settings in karma.conf.js, you can specify a different location. I chose to generate each report in a sub-directory of the coverage directory. In the example below, profile-lib is the name of the sub-directory I'll be placing the profile-lib library's code coverage reports into:

coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../coverage/profile-lib'),
reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true
}

Step 4: Tell source control to ignore your coverage folder(s)
By placing the individual projects' code coverage reports into sub-directories of the main coverage directory, you can easily tell source control to ignore all of them. Here's how I did it in our .gitignore file:

# Code coverage report folder
coverage/

Easy.

I hope this helps someone!





Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

Silent Renew and the "login_required" Error When Using oidc-client

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader