angular cli exclude files/directory for `ng test --code-coverage`
Asked Answered
P

2

53

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?

Patsis answered 15/2, 2017 at 5:52 Comment(4)
update with the code that shows this table data.Respirable
There is no code for table data. This is the coverage report is being generated by Karma Istanbul Coverage Reporter. The Command I shared is generating the report.Patsis
Can anyone please help.Patsis
Hi @ParthaSarathiGhosh, I'm facing similar issues. In my case, I've spec files and I want to skip those files and thus get code coverage without them.Chevy
P
62

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/**/*"
    ]
  },
  ...
}
Partan answered 23/2, 2017 at 21:7 Comment(9)
Thanks a lot for the information. How do you came to know that? Is there any release plan. Please post the link here.Patsis
I proposed the solution that Filipe Silva ultimately implemented. The changeset 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.Partan
@ParthaSarathiGhosh RC.0 has been released. If you upgrade your project, you should be able to use my answer.Partan
Here is a similar type of question for ng lint. Please have a look if you can help me. #42508338Patsis
For newer versions of Angular (6 and up I think) see the other answer in this question. it is now codeCoverageExclude instead!Bisulfate
@Partan 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 advaceSubscript
codeCoverageExclude should actually be inside test.options as stated by @Abubekr in another answerEx
I'm using Angular 14 and codeCoverageExclude stopped workingFoveola
@ÁlisterLopesFerreira Same here.Conjoin
A
74

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/**/*"],
Abubekr answered 14/5, 2018 at 4:26 Comment(2)
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/*".Crupper
I'm using Angular 13 and codeCoverageExclude stopped working.\Gilley
P
62

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/**/*"
    ]
  },
  ...
}
Partan answered 23/2, 2017 at 21:7 Comment(9)
Thanks a lot for the information. How do you came to know that? Is there any release plan. Please post the link here.Patsis
I proposed the solution that Filipe Silva ultimately implemented. The changeset 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.Partan
@ParthaSarathiGhosh RC.0 has been released. If you upgrade your project, you should be able to use my answer.Partan
Here is a similar type of question for ng lint. Please have a look if you can help me. #42508338Patsis
For newer versions of Angular (6 and up I think) see the other answer in this question. it is now codeCoverageExclude instead!Bisulfate
@Partan 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 advaceSubscript
codeCoverageExclude should actually be inside test.options as stated by @Abubekr in another answerEx
I'm using Angular 14 and codeCoverageExclude stopped workingFoveola
@ÁlisterLopesFerreira Same here.Conjoin

© 2022 - 2024 — McMap. All rights reserved.