Javascript 无法使用业力运行覆盖

Javascript 无法使用业力运行覆盖,javascript,code-coverage,karma-runner,Javascript,Code Coverage,Karma Runner,我正在尝试使用karma运行覆盖,我得到警告:WARN[预处理]:无法加载“覆盖”,它未注册 当我运行“npm安装-g karma coverage--save dev”时,我以为我安装了覆盖 这是我的配置文件: module.exports = function(config) { config.set({ // base path, that will be used to resolve files and exclude basePath: '

我正在尝试使用karma运行覆盖,我得到警告:WARN[预处理]:无法加载“覆盖”,它未注册

当我运行“npm安装-g karma coverage--save dev”时,我以为我安装了覆盖

这是我的配置文件:

module.exports = function(config) {
      config.set({
        // base path, that will be used to resolve files and exclude
        basePath: '',

        frameworks: ['jasmine'],

        // list of files / patterns to load in the browser
        files: [
                bunch of files..
        ],

        // list of files to exclude
        exclude: [],

        // use dots reporter, as travis terminal does not support escaping sequences
        // possible values: 'dots', 'progress'
        // CLI --reporters progress
        reporters: ['progress', 'coverage'],

        junitReporter: {
          // will be resolved to basePath (in the same way as files/exclude patterns)
          outputFile: 'test-results.xml'
        },

        // web server port
        // CLI --port 9876
        port: 9876,

        // enable / disable colors in the output (reporters and logs)
        // CLI --colors --no-colors
        colors: true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        // CLI --log-level debug
        logLevel: config.LOG_INFO,

        // enable / disable watching file and executing tests whenever any file changes
        // CLI --auto-watch --no-auto-watch
        autoWatch: true,

        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari (only Mac)
        // - PhantomJS
        // - IE (only Windows)
        // CLI --browsers Chrome,Firefox,Safari
        browsers: ['ChromeCanary'],

        // If browser does not capture in given timeout [ms], kill it
        // CLI --capture-timeout 5000
        captureTimeout: 20000,

        // Auto run tests on start (when browsers are captured) and exit
        // CLI --single-run --no-single-run
        singleRun: true,

        // report which specs are slower than 500ms
        // CLI --report-slower-than 500
        reportSlowerThan: 500,

        // compile coffee scripts
        preprocessors: {
            'someFileName': ['coverage'],
        },

        plugins: [
          'karma-jasmine',
          'karma-chrome-launcher',
          'karma-firefox-launcher',
        ],

    coverageReporter: {
        'type' : 'cobertura',
        'dir': 'coverage/'
    }

  });
};

无论如何,这对我来说很好。安装有:

npm install -g karma
npm install -g karma-coverage
karma.Config.js中配置:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: ['app.js','tests.js'],
    preprocessors: { 'app.js': 'coverage' },
    reporters: ['dots', 'coverage'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

运行
karma start karma.config.js

我得到了相同的[WARN],因为插件'karma coverage'没有在配置的插件中定义,请尝试查看添加它是否修复了警告,不确定它是否会修复您的全部问题

plugins: [
  'karma-jasmine',
  'karma-coverage',
  'karma-chrome-launcher',
  'karma-firefox-launcher',
],
更新:
我也有一个不同的问题,当运行的报道,由伊斯坦布尔造成的,我的错误是

[coverage]:[TypeError:无法设置未定义的属性“covered”]

在查看了伊斯坦布尔正在做的事情之后,发现我的一些js单元文件的路径在预处理器中已经过时了

它正在做一些覆盖率报告,但没有为所有文件生成深度覆盖率报告,因此出现了错误。一旦我修好了路,一切都很好

    preprocessors : {
        '**/app/js/*/*.js' : 'coverage',
        '**/app/js/modules/*/*.js' : 'coverage',
        '**/app/js/services/*/*.js' : 'coverage'
    }, 

对于那些正在使用grunt测试来运行karma测试,并且存在覆盖率插件未加载问题的人。请将
插件设置添加到Gruntiles.js karama任务中,即

// Test settings
karma: {
  unit: {
    configFile: 'test/karma.conf.js',
    singleRun: true,
    plugins:[
      'karma-jasmine',
      'karma-coverage',
      'karma-phantomjs-launcher'
    ],
  }
}

我也有同样的问题,直到我将karma.conf.js移动到与package.json相同的目录中,它才起作用。

我在全球范围内安装了karma coverage,它对我起作用:-)


中描述了此问题

当使用全局安装的karma时,它不会加载本地安装的插件。使用
node\u modules/.bin/karma
启动测试应该可以解决这个问题


在全局“名称空间”中安装覆盖模块也可以,但可能不是您想要的。

我认为正确的解决方案是

不要在全球范围内安装karma

全局安装karma cli,本地安装karma

npm i-g karma cli

这就是问题所在,您应该在全球范围内使用karma cli, 如果您在全球范围内安装karma,它不会使用本地安装


参考:

fwiw这不会为我生成覆盖率输出。通常
/usr/local/bin/
/usr/local/lib/node\u模块
我想。@FutuToad我相信如果你以root用户身份运行npm install-g,它们将转到/usr/local/。否则,它们应该转到/home/user/,即~/。建议的运行node、npm、nvm、ruby和rvm的方法都是以非root用户的身份运行的。为什么使用app.js作为预处理器?这是针对express framework的吗?不,只是碰巧在这个示例中,我有一个“生产”文件,它恰好被称为“app.js”。预处理器行表示在“app.js”文件上使用“coverage”预处理器。通常你会在那里提供一个类似“*spec.js”的模式,而不是一个文件。你有没有解决过这个问题?我注意到的一点是,有时会为循环抛出一些东西,即全局安装与本地安装。我经常不得不使用“npm链接”来让事情正常进行。在这个例子中,npm链接业力覆盖率。另外,我认为你真的需要在你的插件列表中有“karma coverage”。如果你正在运行一个全局安装的karma node包,那么所有其他karma插件也必须全局安装;如果您在本地karma运行项目,则需要确保在本地为您的项目安装其他karma插件。拥有本地karma插件并启动一个全球安装的karma将导致“karma coverage”-未注册的错误类型。在我的情况下,我所要做的就是运行“npm install karma coverage”(甚至不需要插件部分),这对我来说很有效。。。为什么?我不想全局安装。我把它们放在同一个目录中,但我仍然得到错误
npm install -g karma-coverage