首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >karma-coverage包括node_modules

karma-coverage包括node_modules
EN

Stack Overflow用户
提问于 2018-09-03 16:21:37
回答 1查看 1.5K关注 0票数 0

我有一个karma配置,用于我的单元测试和代码覆盖。我的项目目录如下所示

代码语言:javascript
复制
RootFOlder
-karma.config.js
-webpack.test.config.js
-src/
--test.ts
--components
---ButonComponent
----Buttoncomponent.spec.ts

我的karma.config在下面

代码语言:javascript
复制
// 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。module.exports ={ devtool:'inline-source-map',模式:‘开发’,目标:‘节点’,解析:{扩展名:'.ts','.js‘},

代码语言:javascript
复制
  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排除在我的保险范围之外?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2018-09-05 02:15:20

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

如果不是,

尝试将以下内容添加到preprocessors

代码语言:javascript
复制
'!node_modules/**/*.*': ['coverage']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52145807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档