I had the same issue and I found a simple workaround that does the trick for me without any big configuration.

  1. In your app folder, create a file app.module.spec.ts
  2. In this file add an import to your app module.

import './app.module';

That's it ;)

The point is, your app module is most likely the central part of your application which imports any other used files directly or indirectly. Now that you have created the spec file, everything that is included by your module should also be included in the test coverage report.

I am not 100% sure if this works with lazy loaded modules. If not, you can simply import those lazy loaded modules in your app.module.spec.ts as well, or create a spec file per module, where you import the module.

Answer from tom van green on Stack Overflow
🌐
Angular
angular.dev › guide › testing › code-coverage
Code coverage • Angular
If you want to create code-coverage reports every time you test, you can set the coverage option to true in your angular.json file: { "projects": { "your-project-name": { "architect": { "test": { "builder": "@angular/build:unit-test", "options": ...
🌐
Testing-angular
testing-angular.com › measuring-code-coverage
Measuring code coverage – Testing Angular
February 17, 2021 - Istanbul creates an HTML page for every directory and every file. By following the links, you can descend to reports for the individual files. For example, the coverage report for photo-item.component.ts of the Flickr search: The report renders the source code annotated with the information how many times a line was called.
Top answer
1 of 7
18

I had the same issue and I found a simple workaround that does the trick for me without any big configuration.

  1. In your app folder, create a file app.module.spec.ts
  2. In this file add an import to your app module.

import './app.module';

That's it ;)

The point is, your app module is most likely the central part of your application which imports any other used files directly or indirectly. Now that you have created the spec file, everything that is included by your module should also be included in the test coverage report.

I am not 100% sure if this works with lazy loaded modules. If not, you can simply import those lazy loaded modules in your app.module.spec.ts as well, or create a spec file per module, where you import the module.

2 of 7
10

Here is the way to do this:

  1. Add client section to your karma.conf.js like this:

    plugins: [
        ...
    ],
    client: {
        codeCoverage: config.angularCli.codeCoverage
    },
    files: [
        ...
    ],
    
  2. Change your test.ts to require files according to codeCoverage parameter:

    let context; 
    
    if (__karma__.config.codeCoverage) {
        context = require.context('./app/', true, /\.ts/);
    } else {
        context = require.context('./app/', true, /\.spec\.ts/);
    }
    
    context.keys().map(context);
    

UPDATE:

Since Angular CLI 1.5.0 additional steps are required:

  1. Next to tsconfig.spec.json add tsconfig-cc.spec.json file with the following content:

    {
      "extends": "./tsconfig.spec.json",
      "include": [
        "**/*.ts"
      ]
    }
    
  2. In your angular-cli.json add the following to apps array:

    {
      "root": "src/",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "testTsconfig": "tsconfig-cc.spec.json"
    }
    
  3. In your karma.conf.js add the following to angularCli section:

    app: config.angularCli.codeCoverage ? '1' : '0'  
    

    eventually it should look something like this:

    angularCli: {
        environment: 'dev',
        app: config.angularCli.codeCoverage ? '1' : '0'
    },
    

So what's happening here?

Apparently they have fixed Angular compiler plugin and it takes the file globs from tsconfig.spec.json now. As long as we include only **/*.spec.ts in tsconfig.spec.json these are the only files that will be included in coverage.

The obvious solution is making tsconfig.spec.json include all the files (in addition to require.context). However, this will slow down all the tests even when running without coverage (which we don't want to).

One of the solutions is using the ability of angular-cli to work with multiple apps.
By adding another entry into apps array, we're adding another configuration for "another" (which is actually the same one) app.
We strip out all the irrelevant information in this config, leaving just the test configuration, and put another tsconfig which includes all the ts files.
Finally, we're telling angular-cli karma plugin to run the tests with the configuration of the second app (index 1) in case it is running with code coverage and run with the configuration of the first app (index 0) if it is running without code coverage.

Important note: in this configuration I assume you have only one application in .angular-cli.json. In case you have more you have to adjust indexes accordingly.

🌐
Angular
v17.angular.io › cli › test
Angular
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
🌐
Daniel Kreider
danielk.tech › home › how-to-set-up-angular-code-coverage-in-vs-code
How to set up Angular code coverage in VS Code | Daniel Kreider
Go to Extensions in VS Code, search for Coverage Gutters and install it. Once installed, you'll see green lines to show what part of your Angular code is being tested.
🌐
GitHub
github.com › angular › angular-cli › issues › 11227
ng test --code-coverage gives wrong results and do not include all files · Issue #11227 · angular/angular-cli
June 13, 2018 - ng new CoverageTest cd CoverageTest ng g component componentA ng g component componentB // add dumy public method to componentb.ts /* Additionally create some file and put there some simple class with one method. eg. class ServiceA { public getHello(): string { return 'hello'; } } */ // now generate code coverage ng test --code-coverage --watch false // open coverage/index.html
Author   zielinskikamil
Find elsewhere
🌐
Angular
v17.angular.io › guide › testing-code-coverage
Find out how much code you're testing
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
🌐
TutorialsPoint
tutorialspoint.com › angular_cli › angular_cli_code_coverage.htm
Angular CLI - Code Coverage
21 03 2025 10:35:24.588:INFO [karma-server]: Karma v6.4.4 server started at http://localhost:9876/ 21 03 2025 10:35:24.591:INFO [launcher]: Launching browsers Chrome with concurrency unlimited 21 03 2025 10:35:24.609:INFO [launcher]: Starting browser Chrome 21 03 2025 10:35:25.652:INFO [Chrome 134.0.0.0 (Windows 10)]: Connected on socket V0y20VWrmDZ4YAdqAAAB with id 24384158 Chrome 134.0.0.0 (Windows 10): Executed 3 of 3 SUCCESS (0.111 secs / 0.09 secs) TOTAL: 3 SUCCESS =============================== Coverage summary =============================== Statements : 100% ( 3/3 ) Branches : 100% ( 0/0 ) Functions : 100% ( 0/0 ) Lines : 100% ( 2/2 )
🌐
Coryrylan
coryrylan.com › blog › enforcing-code-coverage-in-angular-cli-projects
Enforcing Code Coverage in Angular CLI Projects - Angular 17 | 16
November 24, 2023 - With the Angular CLI its easy to test, run coverage reports and enforces a minimum coverage for the project. To run our unit tests and generate our report run the following code: ... Once ran the unit tests will run a single time reporting any issues. If all tests pass, we will see a new /coverage folder generated. In the coverage folder, we can simply double click the index.html file ...
🌐
Angular Minds
angularminds.com › blog › methods-to-increase-code-coverage-in-angular
Methods to Increase Code Coverage in Angular
October 3, 2024 - Percentage of Coverage: Displays ... ... File-Level Coverage: Shows coverage metrics for individual files, including the number of lines, statements, branches, and functions covered....
🌐
Angular
angular.dev › cli › test
ng test • Angular
One or more named builder configurations as a comma-separated list as specified in the "configurations" section in angular.json. The builder uses the named configurations to run the given target. For more information, see https://angular.dev/reference/configs/workspace-config#alternate-build-configurations. ... Enables coverage reporting for tests. If not specified, the coverage configuration from a runner configuration file ...
🌐
Daniel Kreider
danielk.tech › home › angular-code-coverage
Angular Code Coverage (How to measure and use) | Daniel Kreider
December 9, 2024 - Today we're going to show you how to set up code coverage in your Angular project.
🌐
Andrewhalil
andrewhalil.com › 2021 › 11 › 02 › test-code-coverage-within-angular-applications
Test Code Coverage in an Angular Application – andrewhalil.com
November 2, 2021 - To view the actual source and the uncovered lines, click on the source file hyperlink: The lines in red are the uncovered lines that do not have any related unit tests. To enforce code coverage for every test, the following key/value will need to be added to angular.json within the options key:
🌐
Medium
dkreider.medium.com › how-to-set-up-angular-code-coverage-in-vs-code-dbdceaac8951
How to set up Angular code coverage in VS Code | by Daniel Kreider | Medium
January 29, 2024 - One. GO! Open the angular.json file in the root folder of your Angular project. Scroll to the test section and add the codeCoverage: true line. Like this. "test": { "builder": "@angular-devkit/build-angular:karma", "options": { ... "codeCoverage": ...
🌐
Compile N Run
compilenrun.com › angular tutorial › angular testing › angular code coverage
Angular Code Coverage | Compile N Run
Here's how to enable and configure it: To run your tests with coverage reporting, simply add the --code-coverage flag to the test command: ... This will generate a coverage report in your project's /coverage directory.
🌐
Medium
manivelarjunan.medium.com › angular-7-unit-testing-code-coverage-5c7a238315b6
Angular 7 + unit testing + code coverage | by Manivel Arjunan | Medium
December 29, 2018 - If you want to create code-coverage reports every time you run the test suite, you can set the following option in the CLI configuration file, angular.json: ... Whenever test suite executed successfully, you will see a “coverage” folder in project root directory. ... Open the index.html file in your favorite browser to view the test case coverage for all the files in the project.
🌐
Medium
medium.com › ngconf › angular-unit-testing-code-coverage-lies-603c6c85f801
Angular Unit Testing Code-Coverage Lies | by Jared Youtsey | ngconf | Medium
September 15, 2021 - Here we start to see how we can misinterpret the meaning of a code-coverage report. The report only shows us what lines were executed, not what lines were tested. In Angular, when you compile a component in a unit test ngOnInit and everything it calls will get “covered” immediately. Let’s compound the issue. How much of the template is getting tested? The coverage report doesn’t even report on the template files!