5

I'm trying to get the code coverage of my angular project. I'm not very well versed with the tools. I decided to use "istanbul-instrumenter-loader": "^3.0.1". I tried taking help from this question:

  1. angular cli exclude files/directory for ng test--code-coverage and
  2. Regex that doesn't match spec.ts and spec.tsx but should match any other .ts and .tsx

And many other solutions given on the same thread. My problem is that I want to exclude spec files which I wrote for unit testing. Here is the screenshot of what I'm getting: screenshot. Please correct my mistake and feel free to ask for missing information.

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
Raviw3
  • 51
  • 1
  • 3
  • 1
    What happened when you tried the `codeCoverageExclude` in `angular.json` ? – Shashank Vivek Apr 28 '20 at 08:44
  • I added the app.component.spec.ts file in codeCoverageExclude property. But there was no difference in the report. The file was still included as shown in the screenshot. – Raviw3 Apr 28 '20 at 11:27

1 Answers1

5

To exclude code coverage, you should not just need to specify spec file such as ["src/app/user-card/user-card.component.spec.ts"]

"test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "codeCoverageExclude": ["src/app/user-card/user-card.component.spec.ts"],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },

but rather the entire ts files of that folder which could be used to generate coverage report. (service , component and so on). Hence try to use **.ts as shown below

        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "codeCoverageExclude": ["src/app/user-card/**.ts"],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },

Take a look at this post where mock files are excluded from the coverage

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • Thank you so much Sir. We'll try this solution for sure. – Tanzeel Apr 30 '20 at 13:44
  • @Tanzeel: Did it work ? Please ask your friend to mark it as an answer so that it can help others looking for similar question – Shashank Vivek May 24 '20 at 10:55
  • We've parked this story for sometime. Sure I'll ask him today about this question. – Tanzeel May 24 '20 at 11:30
  • This is not what I want to do. I want to specifically skip the coverage for spec files and still want to run the test cases for the non-spec files. I tried your solution but it skips all the files in that folder. – Raviw3 May 26 '20 at 08:39
  • @Raviw3: There is no "coverage for spec files" as you are asking. Spec files are used for coverage, they are not included in coverage. You are surely mixing things up. Can you share the screenshot of folder and which files you want to "exclude" ? – Shashank Vivek May 26 '20 at 15:05