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

时间:2015-04-12 19:59:25

标签: javascript jasmine karma-runner karma-jasmine

我正在使用Grunt通过Karma / Jasmine运行我的单元测试,而我使用Karma进行代码覆盖,但是,代码覆盖率总是100%,因为它似乎无法找到文件,这里有一些输出:

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.

如您所见,单元测试可以找到要测试的正确文件,但代码覆盖率插件找不到相同的文件。我认为这样做的原因是我使用的是grunt-contrib-connect而不是内置的Karma Web服务器。当我使用内置的业力服务器时,单元测试也无法工作,因为他们也找不到文件。这是我的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
    });
};

我认为grunt-contrib-connect服务器在代码覆盖率开始之前就已退出。有谁知道为什么或如何阻止它?更好的是,有没有人知道为什么内置的业力服务器将无法工作。这是当我使用内置的业力服务器时会发生的事情:

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)

等...

1 个答案:

答案 0 :(得分:2)

您的问题可能是由于您的文件设置没有覆盖插件所期望的文件可用,因为included: false设置需要从脚本加载器(例如require.js)进行手动加载。

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

尝试没有手动加载器的文件设置,让karma加载文件,看看它是否适合你。

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