为什么我的 Karma Jasmine 代码覆盖率总是 100%

Why is my Karma Jasmine code coverage always 100%

我正在使用 G运行t 运行 通过 Karma / Jasmine 进行单元测试,并且我正在使用 Karma 进行代码覆盖,但是,代码覆盖率始终是 100%,因为它可以似乎找不到文件,这里是一些输出:

PhantomJS 1.9.8(Mac OS X)信息:'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.

如您所见,单元测试可以找到要测试的正确文件,但代码覆盖率插件找不到相同的文件。我认为这是因为我使用的是 g运行t-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
    });
};

我认为 g运行t-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)

等....

您的问题可能是由于您的文件设置没有覆盖率插件期望的可用文件,因为 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'
    ],