无法加载记者 "coverage-istanbul"

Can not load reporter "coverage-istanbul"

我正在尝试 运行 使用 https://webpack.js.org/loaders/istanbul-instrumenter-loader/

的代码覆盖率

这是karma.conf.js

var testWebpackCfg = require('../webpack/webpack.config.test.js');

module.exports = function(config) {
  config.set({
    basePath: '../../',
    frameworks: ['jasmine'],
    plugins: [ 'karma-webpack', 'karma-jasmine-jquery', 'karma-jasmine', 'karma-chrome-launcher','karma-firefox-launcher', 'karma-coverage','karma-spec-reporter', 'karma-jasmine-html-reporter'],

    preprocessors: {
      'client/test/index.js': ['webpack']
    },

    reporters: [ 'spec', 'coverage-istanbul'],

    coverageIstanbulReporter: {
      reports: [ 'text-summary' ],
      dir: './coverage',
      fixWebpackSourcePaths: true
    },

    files: [
      'client/test/index.js'
    ],

    webpack:  testWebpackCfg,

    // web server port
    port: 9876,
    runnerPort: 9100,
    urlRoot: '/',

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


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

    browsers: ['Chrome'],
    singleRun: true
  });
};

Webpack 配置

{
            test: /\.js$/i,
            exclude: [
                paths.appNodeModules
            ],
            use: [
                {
                    loader: require.resolve('babel-loader'),
                    options: {
                        presets: ['@babel/preset-env']
                    }
                },
                {
                    loader: require.resolve('istanbul-instrumenter-loader'),
                    options: {
                        esModules: true
                    }
                }
            ]
        }

在 运行 宁 Karma 我看到这个错误 '无法加载记者"coverage-istanbul",它没有注册! 也许您缺少一些插件?'

通过在 karma 配置插件中添加 karma-coverage-istanbul-reporter 解决了错误。

npm i karma-coverage-istanbul-reporter --save-dev

karma.conf.js

{
    ...
    plugins: ['karma-coverage-istanbul-reporter']
    ...
}

Nishant 给出了基本答案,但我发现当我没有明确指定 karma 配置文件时我也收到了这条消息,按照这个答案: