【问题标题】:karma-coverage includes node_modules业力覆盖包括 node_modules
【发布时间】:2019-02-08 06:40:24
【问题描述】:

我有一个 karma 配置,用于我的单元测试和 code-cov。我的项目目录如下所示

RootFOlder
-karma.config.js
-webpack.test.config.js
-src/
--test.ts
--components
---ButonComponent
----Buttoncomponent.spec.ts

我的 karma.config 在下面

// Karma configuration

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: [
      'src/test.ts',
      'src/components/**/*.component.ts',
    ],

    // list of files / patterns to exclude
    exclude: [
      'node_modules',
      './src/tsconfig.spec.json'
    ],
    plugins: [
      require('karma-jasmine'),
      require("karma-coverage"),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-webpack'),
      require('karma-sourcemap-loader'),
      require('ts-loader')
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'src/components/**/*.ts': ['coverage'],
      'src/components/**/*.component.ts': ['webpack', 'sourcemap'],
      'src/test.ts': ['webpack', 'sourcemap'],

    },

    webpack: require('./webpack.test.config'),

    coverageReporter: {
      reporters: [
        { type: 'text', subdir: 'text' },
        { type: 'html', subdir: 'report-html' },
      ]
    },

    ...
    ...
  });
}

还有我的 webpack。 模块.exports = { devtool: '内联源地图', 模式:'发展', 目标:'节点', 解决: { 扩展名:['.ts', '.js'] },

  module: {
    rules: [
      {
        test: /\.ts$/,
        loaders: ['ts-loader', 'angular-router-loader', 'angular2-template-loader']
      }
     ....
     ....

    ]
  },

  plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'test')
    }),
    // Removes warnings regarding Critical dependency
    new webpack.ContextReplacementPlugin(
      /\@angular(\\|\/)core(\\|\/)f?esm5/, path.join(__dirname, './src')
    )
  ],
  node: {
    console: false,
    global: true,
    process: true,
    Buffer: false,
    setImmediate: false
  }
}

问题是在我运行测试后,我的覆盖范围涵盖了捆绑的 node_modules。文件覆盖率最终以 MBytes 运行,而我的覆盖率很低。请问我如何从我的覆盖范围中排除 node_modules?任何帮助表示赞赏。

【问题讨论】:

  • 嗨.. 答案有帮助吗?

标签: karma-jasmine karma-coverage karma-webpack


【解决方案1】:

默认情况下,您甚至不需要在 exclude 路径中提及 node_modules。尝试删除它,看看覆盖范围是否得到纠正?

如果没有,

尝试将此添加到preprocessors

'!node_modules/**/*.*': ['coverage']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2017-01-07
    • 2014-08-17
    • 2015-10-06
    • 1970-01-01
    • 2016-03-21
    • 2015-08-25
    • 2016-04-05
    相关资源
    最近更新 更多