【问题标题】:Unable to run Coverage with Karma无法使用 Karma 运行 Coverage
【发布时间】:2013-08-06 00:14:57
【问题描述】:

我正在尝试使用 karma 运行覆盖,我收到警告:WARN [preprocess]: Can not load "coverage",它没有注册!

我以为我在运行“npm install -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 链接”来让事情正常工作。即:在这种情况下,npm link karma-coverage。另外,我确实认为您确实需要在插件列表中包含“业力覆盖”。

标签: javascript code-coverage karma-runner


【解决方案1】:

我得到了相同的 [WARN],因为插件 'karma-coverage' 未在配置的插件中定义,尝试添加它是否可以解决您的警告,不确定它是否会解决您的全部问题。

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

更新:
我在运行覆盖时也遇到了另一个问题,由伊斯坦布尔引起,我的错误是

[coverage]: [TypeError: Cannot set property 'covered' of undefined]

在查看了 istanbul 的操作后,发现我的一些 js 单元文件的路径在 预处理器 中已过时。

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

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

【讨论】:

  • 如果你运行的是全局安装的 karma 节点包,那么所有其他的 karma 插件也必须全局安装;如果您正在运行项目本地 karma,则需要确保为您的项目本地安装了其他 karma 插件。拥有本地 karma 插件并启动全局安装的 karma 也会导致 'karma-coverage'-not-registred 类型的错误。
  • 就我而言,我所要做的就是运行“npm install karma-coverage”(甚至不需要插件部分)
【解决方案2】:

对于它的价值,这对我来说很好。安装:

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 运行。

【讨论】:

  • fwiw 这不会为我产生覆盖输出。
  • 我认为通常是 /usr/local/bin//usr/local/lib/node_modules
  • @FutuToad 我相信如果您以 root 身份运行 npm install -g,他们将转到 /usr/local/。否则他们应该去 /home/user/,也就是所谓的 ~/.建议运行node、npm、nvm、ruby、rvm的方法都是非root用户。
  • 为什么 app.js 用于预处理器?这是快递框架吗?
  • 不,只是碰巧在这个例子中我有一个“生产”文件,它恰好被称为“app.js”。预处理器行说要在“app.js”文件上使用“覆盖”预处理器。通常你会在那里提供像“*spec.js”这样的模式,而不是只提供一个文件。
【解决方案3】:

对于那些使用 grunt test 运行 karma 测试,并遇到覆盖插件未加载问题的人。请将plugins 设置添加到您的 Gruntfiles.js karama 任务中,即

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 我将它们放在同一个目录中,但我仍然收到错误
    【解决方案5】:

    没有全局安装的解决方案

    安装karma-coverage:

    npm install --saved-dev karma-coverage
    

    然后编辑karma.conf.js并将require('karma-coverage')添加到plugins的数组中。

    module.exports = function (config) {
      config.set({
        // ...
        plugins: [
          require('karma-coverage'), // ADD THIS
          // ...
        ],
      });
    }
    

    【讨论】:

      【解决方案6】:

      this answer 中描述了这个问题。

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

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

      【讨论】:

        【解决方案7】:

        我认为正确的解决方案是

        不要在全球范围内安装业力

        全局安装 karma-cli 并在本地安装 karma

        npm i -g karma-cli

        那就是问题所在,你应该全局使用 karma-cli,http://karma-runner.github.io/0.12/intro/installation.html 如果您在全局安装 karma,则不会使用本地安装。

        参考:github

        【讨论】:

          【解决方案8】:

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

          npm install -g karma-coverage
          

          【讨论】:

          • 这对我有用...为什么?我不想全局安装。
          【解决方案9】:

          如果您在使用 Angular 13 时遇到此问题。请安装 karma-coverage,这是完整的配置:

          
          module.exports = function (config) {
            config.set({
              basePath: '',
              frameworks: ['jasmine', '@angular-devkit/build-angular'],
              plugins: [
                require('karma-jasmine'),
                require('karma-chrome-launcher'),
                require('karma-jasmine-html-reporter'),
                require('karma-coverage'),
                require('@angular-devkit/build-angular/plugins/karma'),
              ],
              client: {
                clearContext: false, // leave Jasmine Spec Runner output visible in browser
              },
              coverageReporter: {
                dir: require('path').join(__dirname, './coverage/project-name'),
                reporters: [
                  {
                    type: 'html',
                  },
                  {
                    type: 'lcov',
                  },
                  {
                    type: 'text-summary',
                  },
                ],
              },
              reporters: ['progress', 'kjhtml'],
              port: 9876,
              colors: true,
              logLevel: config.LOG_INFO,
              autoWatch: true,
              browsers: ['Chrome', 'ChromeHeadlessNoSandbox'],
              singleRun: false,
              customLaunchers: {
                ChromeHeadlessNoSandbox: {
                  base: 'ChromeHeadless',
                  flags: ['--no-sandbox'],
                },
              },
            });
          };
          

          【讨论】:

            猜你喜欢
            • 2019-04-12
            • 2016-09-16
            • 1970-01-01
            • 2017-07-27
            • 1970-01-01
            • 2018-01-27
            • 2016-10-13
            • 2016-04-04
            • 1970-01-01
            相关资源
            最近更新 更多