1

我使用本指南http://karma-runner.github.io/0.8/config/coverage.html来设置我的 Karma-coverage 插件。此外,它通过 package.json 文件在本地安装,如下所示:

{
    "name": "myApp",
    "version": "0.1.0",
    "devDependencies": {
        "karma-jasmine": "latest",
        "karma-coverage": "latest",
        "karma-coffee-preprocessor": "latest",
        "karma-chrome-launcher": "latest",
        "coffee-script": "latest"
    }
}

我的 Karma.conf 看起来像这样:

// Karma configuration
// Generated on Sun Dec 07 2014 15:51:07 GMT+0100 (CET)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
        'javascripts/vendor/jquery_211.js',
        'javascripts/vendor/angular.js',
        'javascripts/vendor/angular-route.js',
        'javascripts/vendor/angular-mocks.js',
        '../app/assets/javascripts/app.coffee',
        'javascripts/*.js',
        'test/controllers/*.js'
    ],


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


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        '../app/assets/javascripts/app.coffee' : ['coffee'],
        '**/javascripts/*.js': 'coverage'
        //'**/*.coffee': ['coffee']
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'coverage'],

    // web server port
    port: 9876,


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


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


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

现在,在运行业力时,我收到错误

WARN [reporter]: Can not load "coverage", it is not registered!
  Perhaps you are missing some plugin?
INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [preprocess]: Can not load "coverage", it is not registered!
  Perhaps you are missing some plugin?

没关系,如果我在 karma.conf 文件的插件部分中包含 karma-coverage,问题仍然存在(因为插件是在本地加载的,所以无论如何我都不应该需要插件部分......)。我希望你们中的某个人有一个想法......谢谢!

4

2 回答 2

4

npm install -g karma-coverage安装业力覆盖,它会正常工作,但我不知道为什么。

// Karma configuration
// Generated on Tue Oct 13 2015 11:14:07 GMT+0800 (CST)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      '*.js'
    ],


    // list of files to exclude
    exclude: [
      'karma.config.js'
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      '*.js': 'coverage'
    },

    coverageReporter:{
      type: 'html',
      dir: './coverage'
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress','coverage'],


    // web server port
    port: 9876,


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


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


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  })
}

于 2015-10-13T03:38:36.750 回答
3

这个问题的解决方案是我在我的用户主文件夹中安装了几个 node_modules。通过 npm 卸载这些模块和本地安装的模块后,我通过 npm 链接进行了全新的重新安装。这为我解决了问题!

于 2015-02-26T17:56:15.040 回答