简体   繁体   English

为什么我的Karma Jasmine代码覆盖率始终为100%

[英]Why is my Karma Jasmine code coverage always 100%

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: 我正在使用Grunt通过Karma / Jasmine运行我的单元测试,并且正在使用Karma进行代码覆盖,但是,代码覆盖率始终是100%,因为它似乎找不到文件,这是一些输出:

PhantomJS 1.9.8 (Mac OS X) INFO: 'Starting Tests ...' PhantomJS 1.9.8(Mac OS X)信息:“正在开始测试...”

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. 我认为这是因为我使用grunt-contrib-connect而不是内置的Karma Web服务器。 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: 这是我的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? 我认为在代码覆盖范围开始之前,grunt-contrib-connect服务器正在退出。有人知道为什么或如何停止此操作吗? 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.... 等等....

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. 您的问题可能是由于文件设置在coverage插件期望的位置没有可用的文件所致,例如, 包括在内:错误的设置需要从脚本加载器(如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'
    ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM