0

My Karma coverage report shows to cover the local variable. is that possible or its a karma-coverage report issue.

Please have a look at the Angular Controller Code.

'use strict';

angular.module('moduleName')
    .controller('FormController', [ '$log',
        function FormController($log) {

            // Controller Local Variables.
            var that = this;

            that.hideLoader = function () {
                that.isLoading = false;
            };

        }
]);

enter image description here

My Question: Is that possible to cover the local variables and function parameter conditions. for instance is below.

that.hideLoader = function (userObj) {
   var id = userObj.id;

   if(id) {
     that.isLoading = false;
   }
   else {
     that.isError = true;
   }
};

In the above example, I have declared user object id attribute to local id variable. now its very tough to cover the code. in this case, jasmine advise to reduce local variable or its karma-coverage report suggestion ?

Alamelu Venkat
  • 471
  • 1
  • 4
  • 15

1 Answers1

0

my karma coverage report wants to cover the local variable. is that possible or its a karma-coverage report issue.

The coverage tool is working correctly by checking that your tests cover every line of code. That is the definition of code coverage.

On the other hand:

var that = this;
that.hideLoader = function() { that.isLoading = false};

are NOT local variables. As defined, they are properties of your controller. Word of caution: please use "use strict" and don't use undeclared properties such as that.isLoading. It is not very readable and it is bad practice even though the language allows for it.

Also, when asking questions please paste the code and not images of the code.

Update

The answer to your question is yes. Karma-coverage reports on every line of code touched (green), or untouched (red).

Wilmer SH
  • 1,417
  • 12
  • 20