1
votes

I am using Grunt to run my unit tests through Karma / Jasmine, and I am using Karma for code coverage, however, code coverage is always 100%, as it can't seem to find the files, here is some of the output:

PhantomJS 1.9.8 (Mac OS X) INFO: 'Starting Tests ...'

PhantomJS 1.9.8 (Mac OS X): Executed 2 of 2 SUCCESS (0.003 secs / 0.021 secs)
DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [proxy]: failed to proxy /app/view/SearchForm.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/Main.js?_dc=1428867938628 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ImageDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ItemDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/News.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/Home.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [coverage]: Writing coverage to /appdir/coverage/PhantomJS 1.9.8 (Mac OS X)
----------|-----------|-----------|-----------|-----------|
File      |   % Stmts |% Branches |   % Funcs |   % Lines |
----------|-----------|-----------|-----------|-----------|
----------|-----------|-----------|-----------|-----------|
All files |       100 |       100 |       100 |       100 |
----------|-----------|-----------|-----------|-----------|

DEBUG [launcher]: Process PhantomJS exited with code 0
DEBUG [temp-dir]: Cleaning temp dir

Done, without errors.

As you see, the unit tests can find the correct files to test against, but the code coverage plugin can't find the same files. I think the reason for this, is that I'm using grunt-contrib-connect instead of the in-built Karma web server. When I use the in-built karma server, the unit tests won't work either, as they can't find the files either. Here is my karma.conf:

module.exports = function (config) {
    config.set({
        basePath: '',

        frameworks: [ 'jasmine' ],

        files: [
            'touch/sencha-touch-all.js',
            'setup.js',
            'app.js',
            {
                pattern: 'app/**/*.js',
                watched: true,
                included: false,
                served: true
            },
            'tests/**/*.js'
        ],

        proxies: {
            '/': 'http://localhost:9000/'
        },

        preprocessors: {
            'app/**/*.js': ['coverage']
        },
        reporters: ['junit', 'progress', 'coverage'],

        coverageReporter: {
            type: 'text'
        },

        port: 9876,
        colors: true,
        logLevel: config.LOG_DEBUG,
        autoWatch: true,
        browsers: [ 'PhantomJS' ],
        captureTimeout: 60000,
        singleRun: false
    });
};

I think the grunt-contrib-connect server is exiting before the code coverage kicks in. Does anyone know why or how to stop this? Even better, does anyone know why the in-built karma server won't work. Here is what happens when I use the in-built karma server:

DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)

etc....

1

1 Answers

2
votes

Your problems could be due to your files settings not having the files available where the coverage plugin expects them, as, the included: false setting requires mannual loading from a script loader such as require.js.

    files: [
        'touch/sencha-touch-all.js',
        'setup.js',
        'app.js',
        {
            pattern: 'app/**/*.js',
            watched: true,
            included: false,
            served: true
        },
        'tests/**/*.js'
    ],

Try a files setting without the manual loader and let karma load the files to see if that works for you.

    files: [
        'touch/sencha-touch-all.js',
        'setup.js',
        'app.js',
        'app/**/*.js',
        'tests/**/*.js'
    ],