0

Background

We have an existing a huge angular application without any unit tests and we have just started writing the test cases. When I run the command

ng test --code-coverage

Problem

I see all the *.ts files in the report. However, I just need the code coverage of 3 files for which I have created .spec files in my app and not all of them.

What I tried

After reading different stackoverflow posts it seems that there are some options files and preprocessor to be added in the tsconfig.json, and I could add my files like this:

  "files": [
    "src/app/products/details/activity/timeline.component.ts",
  ],
  "preprocessors": {
    "src/app/products/details/activity/timeline.component.ts": ["coverage"],
  },

Question

But this does not seem to work and I still see all the files in the coverage report. How do I tell Angular/Karma/Istanbul to only show coverage of the files for only the files for which I wrote test cases.

Curious Explorer
  • 357
  • 2
  • 14

1 Answers1

0

This answer may help you: How do I exclude files from karma code coverage report?.

Basically you can exclude folders in the preprocessor using !.

preprocessors: {
  // source files, that you wanna generate coverage for
  // do not include tests or libraries
  'src/**/!(spec|mock)/*.js': ['coverage']
},

Another way to exclude files of coverage is skip the actual test using the skip verbs for example in jasmine: xdescribe xit : Jasmine or mocha describe.skip

Or just execute the only test that you want to know the coverage:

describe.only  -> mocha
fdescribe  -> jasmine

Just use this verbs in the file for the tests of the component: "src/app/products/details/activity/timeline.component.ts"

Alejandro
  • 82
  • 3
  • 12