Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage for angular 2

How do you find the code coverage for angular 2 code? Is there any plugin for vs code editor or webstorm that I can use? I am using Jasmine and Karma for unit testing of my code.

like image 874
Abhi Avatar asked Jun 09 '17 17:06

Abhi


People also ask

What is code coverage in angular?

Code coverage, also called test coverage, tells you which parts of your code are executed by running the unit and integration tests. Code coverage is typically expressed as percent values, for example, 79% statements, 53% branches, 74% functions, 78% lines.

How do I check my karma coverage?

Code coverage enforcementlink To enable this, open the Karma test platform configuration file, karma. conf. js , and add the check property in the coverageReporter: key. The check property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.

What should my code coverage be?

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.


3 Answers

If you want to see overall test coverage statistics than of course in Angular CLI you can just type, and see the bottom of your command prompt window

ng test --code-coverage

result:

console view of tests coverage

if you want to see component's individual coverage of tests follow these steps.

  1. npm install --save-dev karma-teamcity-reporter

  2. Add require('karma-teamcity-reporter') to list of plugins in karma.conf.js

  3. ng test --code-coverage --reporters=teamcity,coverage-istanbul

note that list of reporters is comma-separated, as we have added a new reporter, teamcity.

after running this command you can see the folder coverage in your dir and open index.html for a graphical view of test coverage.

enter image description here

You can also set the coverage threshold that you want to achieve, in karma.conf.js, like this.

coverageIstanbulReporter: {
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true,
      thresholds: {
        statements: 90,
        lines: 90,
        branches: 90,
        functions: 90
      }
    },
like image 107
ahmadalibaloch Avatar answered Oct 16 '22 21:10

ahmadalibaloch


First install the dependencies.

npm install karma karma-jasmine karma-chrome-launcher karma-jasmine-html-reporter karma-coverage-istanbul-reporter

Then run ng test.

ng test --code-coverage

Then run the server that shows you your report.

http-server -c-1 -o -p 9875 ./coverage

You should see something like this:

enter image description here

I wrote a blog post about this here.

like image 7
Jason Swett Avatar answered Oct 16 '22 22:10

Jason Swett


I struggled with this one. The solution I found was

ng test --code-coverage

But make sure that in your karma.conf.js file, you have a reporter specified (I use 'coverage-istanbul')

e.g. reporters: ['coverage-istanbul']

The coverage report will be in a directory called 'coverage' in your root directory.

like image 1
maia Avatar answered Oct 16 '22 22:10

maia