简体   繁体   English

Karma-Browserify + Karma-Coverage的问题

[英]Issue with Karma-Browserify + Karma-Coverage

I am having issues getting karma-browserify to work with karma-coverage . 我在使karma-browserifykarma-coverage一起工作时遇到问题。 I have spent a lot of time trying to figure out what is wrong, but I didn't find a solution. 我花了很多时间试图找出问题所在,但没有找到解决方案。

Here is my .js file (the functions don't do anything; they are just mocks to test code coverage): 这是我的.js文件(函数不执行任何操作;它们只是用于测试代码覆盖率的模拟对象):

// src/js/utilities/form-validation.js

let includedInTest = () => true;

let alsoIncludedInTest = () => true;

let notIncludedInTest = () => true;

let alsoNotIncludedInTest = () => true;

export default {
  includedInTest,
  alsoIncludedInTest
};

This is my test file: 这是我的测试文件:

// src/spec/utilities/form-validation.spec.js

import formUtilities from '../../js/utilities/form-validation';

describe('Form validation functions', function () {

  it('Should return "true"', function () {
    expect(formUtilities.includedInTest()).toBe(true);
  });

  it('Should return "true"', function () {
    expect(formUtilities.alsoIncludedInTest()).toBe(true);
  });

});

Finally, this is my karma.conf: 最后,这是我的karma.conf:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['browserify', 'jasmine-jquery', 'jasmine'],
    files: [
      'bower_components/jquery/dist/jquery.js',
      'bower_components/jquery-validation/dist/jquery.validate.js',
      'src/js/**/*.js',
      'src/spec/**/*.spec.js'
    ],
    exclude: [
      'src/js/index.js'
    ],
    preprocessors: {
      'src/js/**/*.js': ['browserify', 'coverage'],
      'src/spec/**/*.spec.js': ['browserify']
    },
    browserify: {
      debug: true,
      transform: [
        ['babelify', { presets: ['es2015'] }]
      ]
    },
    reporters: ['mocha', 'coverage'],
    mochaReporter: {
      colors: {
        success: 'green',
        info: 'bgBlue',
        warning: 'cyan',
        error: 'bgRed'
      },
      symbols: {
        success: '√',
        info: '#',
        warning: '!',
        error: 'x'
      }
    },
    coverageReporter: {
      instrumenters: { isparta: require('isparta') },
      instrumenter: {
        'src/**/*.js': 'isparta'
      },
      dir: 'coverage',
      subdir: '.',
      reporters: [
        { type: 'html', dir: 'coverage' },
        { type: 'text-summary' }
      ],
      check: {
        global: {
          statements: 90,
          branches: 90,
          functions: 90,
          lines: 90
        },
        each: {
          statements: 90,
          branches: 90,
          functions: 90,
          lines: 90
        }
      },
      watermarks: {
        statements: [50, 75],
        functions: [50, 75],
        branches: [50, 75],
        lines: [50, 75]
      }
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  });
};

This config yields this result: 此配置产生以下结果:

==== Coverage summary ====
Statements   : 100% ( 1/1 )
Branches     : 100% ( 2/2 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 1/1 )
=============

This is obviously wrong since I have four functions on "form-validation.js", and I am testing two of them. 这显然是错误的,因为我在“ form-validation.js”上有四个功能,并且我正在测试其中两个。 But according to the summary report, there are no functions to be tested. 但是根据摘要报告,没有要测试的功能。

This line from coverage/index.html reveals only one line is being parsed by karma-coverage : 来自coverage/index.html这一行显示karma-coverage仅解析了一行:

在此处输入图片说明

I also tried 'browserify-istanbul' in the transform array (and removed instrumenters from "coverageReport"): 我还在转换数组中尝试了“ browserify-istanbul”(并从“ coverageReport”中删除了检测器):

transform: [
  ['babelify', { presets: ['es2015'] }],
  'browserify-istanbul'
]

But this generates an error: 但这会产生一个错误:

18 08 2017 15:50:14.617:ERROR [karma]: TypeError: Cannot read property 'start' of undefined
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/object-utils.js:59:44
    at Array.forEach (native)
    at Object.addDerivedInfoForFile (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/object-utils.js:58:37)
    at Collector.fileCoverageFor (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:94:15)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:108:30
    at Array.forEach (native)
    at Collector.getFinalCoverage (/Users/gferraz/Sites/OAA-Refactor/node_modules/istanbul/lib/collector.js:107:22)
    at checkCoverage (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:148:33)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:257:32
    at Array.forEach (native)
    at Collection.forEach (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma/lib/browser_collection.js:93:21)
    at /Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:247:16
    at Array.forEach (native)
    at CoverageReporter.onRunComplete (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma-coverage/lib/reporter.js:246:15)
    at Server.<anonymous> (/Users/gferraz/Sites/OAA-Refactor/node_modules/karma/lib/events.js:13:22)
    at emitTwo (events.js:111:20)

Any suggestions on how to fix the config file? 关于如何修复配置文件的任何建议?

The config suggested on the correct answer of this post helped me: Karma/Istanbul Code Coverage does not find functions and always returns 100% 这篇文章正确答案上建议的配置对我有所帮助: Karma /伊斯坦布尔代码覆盖率未找到函数,并且始终返回100%

Now I am getting an error on the html report ERROR [coverage]: TypeError: Cannot read property 'text' of undefined (meaning the html file I want to generate for the report is not being generated), which seems to be related to istanbul. 现在,我在html报告ERROR [coverage]: TypeError: Cannot read property 'text' of undefined上遇到错误ERROR [coverage]: TypeError: Cannot read property 'text' of undefined (这意味着我要为该报告生成的html文件未生成),这似乎与伊斯坦布尔有关。 However, I am getting the right code coverage report on my terminal window: 但是,我在终端窗口上得到了正确的代码覆盖率报告:

在此处输入图片说明

Strangely enough, the error doesn't happen every time the tests run, so I am able to get the html file just fine sometimes. 奇怪的是,每次运行测试时都不会发生该错误,因此有时我可以很好地获取html文件。

Here is the karma.conf that solved the problem addressed on my question: 这是karma.conf ,可以解决我的问题:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['browserify', 'jasmine-jquery', 'jasmine'],
    files: [
      'bower_components/jquery/dist/jquery.js',
      'bower_components/jquery-validation/dist/jquery.validate.js',
      'src/js/**/*.js',
      'src/spec/**/*.spec.js'
    ],
    exclude: [
      'src/js/index.js'
    ],
    preprocessors: {
      'src/js/**/*.js': ['browserify'],
      'src/spec/**/*.spec.js': ['browserify']
    },
    browserify: {
      debug: true,
      extensions: ['.js'],
      configure: (bundle) => {
        bundle.transform('babelify', { presets: ['es2015'] });
        bundle.transform(require('browserify-istanbul')({
          ignore: ['**/spec/**']
        }));
      }     
    },
    reporters: ['mocha', 'coverage'],
    coverageReporter: {
      dir: 'coverage',
      subdir: '.',
      reporters: [
        { type: 'html', dir: 'coverage' },
        { type: 'text-summary' }
      ],

      etc...
    }
  });
};

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

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