50

I am running the following command to unit test and generate code code coverage report.

ng test --code-coverage

It is working fine and writing code coverage report in coverage folder.

In this I got all files and directory coverage report

enter image description here

But I want to exclude specific files/directory let say src/app/quote/services/generated. How to do that?

Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84

2 Answers2

71

With the latest CLI, inside angular.json

  "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "./karma.conf.js",
            "codeCoverageExclude": ["src/testing/**/*"],
Greg
  • 1,007
  • 1
  • 9
  • 9
  • 1
    In case anyone else is having trouble with this in a library inside a monorepo, it is worth noting that the directory is specified from the root, ie `"projects/lib-something/lib/foldertoexclude/*"`. – coppereyecat Oct 19 '21 at 18:48
  • I'm using Angular 13 and codeCoverageExclude stopped working.\ – Paritosh Nov 24 '22 at 13:40
58

Updated September 2019

With Angular CLI 6, angular-cli.json has been renamed to angular.json which contains the configuration. In angular.json, codeCoverage expects a boolean value, which sets whether code-coverage should be done with every test run or not. To exclude files from code coverage, there is a property codeCoverageExclude which accepts an array of files to be excluded from code coverage.

angular.json

"test": {
  "codeCoverageExclude": ["src/assets/vendor/**"],,
  ...
}

Updated answer

rc.0 has been released. You should be able to add the code snippet below to ignore files.

Original answer

Currently, you aren't able to do so in beta.32.3. A change is coming to allow this to happen. In the next release (probably rc.0), you will be able to do the following:

.angular-cli.json

"test": {
  "codeCoverage": {
    "exclude": [
      "src/app/quote/services/generated/**/*"
    ]
  },
  ...
}
Community
  • 1
  • 1
delasteve
  • 2,637
  • 2
  • 17
  • 10
  • Thanks a lot for the information. How do you came to know that? Is there any release plan. Please post the link here. – Partha Sarathi Ghosh Feb 24 '17 at 05:58
  • I proposed the solution that Filipe Silva ultimately implemented. The [changeset](https://github.com/angular/angular-cli/commit/b6893d0b7fb351baf2ae499decb7740411305ef5) is already in `master`. It will be in the next CLI release, whether it's called `beta.33` or `RC.0`. The next CLI release will happen after Angular itself reaches `RC.0`. – delasteve Feb 24 '17 at 12:52
  • @ParthaSarathiGhosh `RC.0` has been released. If you upgrade your project, you should be able to use my answer. – delasteve Feb 25 '17 at 15:34
  • Here is a similar type of question for `ng lint`. Please have a look if you can help me. http://stackoverflow.com/questions/42507838/angular-cli-exclude-fiels-directory-for-ng-linit – Partha Sarathi Ghosh Feb 28 '17 at 11:42
  • For newer versions of Angular (6 and up I think) see the other answer in this question. it is now `codeCoverageExclude` instead! – Chris Barr May 16 '19 at 20:44
  • @delasteve do you know how to exclude all files ending with .action.ts, because your answer is specific by folder? correct me if im wrong. thanks in advace – rosiejaneenomar Apr 01 '20 at 03:16
  • 6
    `codeCoverageExclude` should actually be inside `test.options` as stated by @Greg in another answer – atwright147 Dec 23 '20 at 10:55
  • 3
    I'm using Angular 14 and codeCoverageExclude stopped working – Álister Lopes Ferreira Jun 07 '22 at 20:56
  • @ÁlisterLopesFerreira Same here. – Victor Zakharov Jun 20 '22 at 13:23