karma-runner / karma-coverage

A Karma plugin. Generate code coverage.
MIT License
770 stars 247 forks source link

Coverage reported on untested files #239

Open mrt123 opened 8 years ago

mrt123 commented 8 years ago

I have two source files : sign-up.js and login.js, and only one test: login.spec.js.

Expected result:

When I'll uncomment all tests. Empty test-suite will report cover of

sign-up.js

angular.module('app.SignUpCtrl', []).controller('SignUpCtrl', SignUpCtrl);

function SignUpCtrl($scope, account, $timeout, $state, facebook) {
    $scope.signUp = signUp;
    $scope.loginWithFacebook = loginWithFacebook;

    function signUp(email, password, playerName) {
        account.signUp(email, password, playerName, {
            success: signUpSuccess,
            fail: showError
        });
    }

    function loginWithFacebook() {
        facebook.login(function (response) {
            signUpSuccess();
        });
    }

    function signUpSuccess() {
        $state.go('home');
    }

    function showError(user, error) {
        $timeout(function () {   // avoid existing digest
            $scope.error = error.message;
        });
    }
}

login.js

angular .module('app.LoginCtrl', []) .controller('LoginCtrl', LoginCtrl);

function LoginCtrl($scope, account, $timeout, $state, facebook) {

    $scope.loginWithFacebook = loginWithFacebook;
    $scope.login = login;

    function loginWithFacebook() {
        facebook.login(function (response) {
            loginSuccess();
        });
    }

    function login(email, password) {
        account.signIn(email, password, {
            success: loginSuccess, 
            fail: showError
        });
    }

    function loginSuccess() {
        $state.go('home');
    }

    function showError(user, error) {
        $timeout(function () {   // avoid existing digest
            $scope.error = error.message;
        });
    }
}

My config:

        preprocessors: {
            // source files, that you wanna generate coverage for
            '../../app/*/!(*.spec).js' : ['coverage']
        },
skyl commented 6 years ago

If the modules are getting imported at all, it makes sense that the signature(s) and top-level statements would be "covered".

johnjbarton commented 6 years ago

You can try setting the config flag for DEBUG logging to see which files are being preprocessed to add coverage instrumentation as a check on you config.