Raviw3
Raviw3

Reputation: 51

How to exclude spec files while getting code coverage [Angular]

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.

Upvotes: 5

Views: 11210

Answers (1)

Shashank Vivek
Shashank Vivek

Reputation: 17494

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

Upvotes: 5

Related Questions