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

26,353

Solution 1

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/**/*"
    ]
  },
  ...
}

Solution 2

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/**/*"],
Share:
26,353
Partha Sarathi Ghosh
Author by

Partha Sarathi Ghosh

AngularJS is fun

Updated on January 26, 2021

Comments

  • Partha Sarathi Ghosh
    Partha Sarathi Ghosh over 3 years

    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
    Partha Sarathi Ghosh about 7 years
    Thanks a lot for the information. How do you came to know that? Is there any release plan. Please post the link here.
  • delasteve
    delasteve about 7 years
    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.
  • delasteve
    delasteve about 7 years
    @ParthaSarathiGhosh RC.0 has been released. If you upgrade your project, you should be able to use my answer.
  • Partha Sarathi Ghosh
    Partha Sarathi Ghosh about 7 years
    Here is a similar type of question for ng lint. Please have a look if you can help me. stackoverflow.com/questions/42507838/…
  • FiniteLooper
    FiniteLooper almost 5 years
    For newer versions of Angular (6 and up I think) see the other answer in this question. it is now codeCoverageExclude instead!
  • rosiejaneenomar
    rosiejaneenomar about 4 years
    @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
  • atwright147
    atwright147 over 3 years
    codeCoverageExclude should actually be inside test.options as stated by @Greg in another answer
  • coppereyecat
    coppereyecat over 2 years
    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/*".
  • Álister Lopes Ferreira
    Álister Lopes Ferreira almost 2 years
    I'm using Angular 14 and codeCoverageExclude stopped working