karma-coverage包括node_modules

问题描述 投票:0回答:1

我有一个karma配置,我的单元测试和代码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' },
      ]
    },

    ...
    ...
  });
}

还有我的网络包。 module.exports = {devtool:'inline-source-map',mode:'development',target:'node',resolve:{extensions:['.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运行并且我的覆盖率低的hude。请问如何从我的coverage中排除node_modules?任何帮助表示赞赏。

karma-jasmine karma-coverage karma-webpack
1个回答
1
投票

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

如果不,

尝试将此添加到preprocessors

'!node_modules/**/*.*': ['coverage']
© www.soinside.com 2019 - 2024. All rights reserved.