首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Webpack和巴别塔从Karma的代码覆盖率中排除Spec文件?

如何使用Webpack和巴别塔从Karma的代码覆盖率中排除Spec文件?
EN

Stack Overflow用户
提问于 2016-03-04 20:52:36
回答 2查看 6.5K关注 0票数 2

问题

我正在做一个使用webpackbabelkarma配置的小型react-redux项目。我向karma添加了代码覆盖率,但我找不到从覆盖率中排除测试文件的方法。所以我的代码覆盖率有spec文件。

如何将这些spec文件排除在覆盖范围之外?

我尝试使用正则表达式来排除spec文件,但是由于它是由webpack加载的,所以不起作用。

tests.webpack.js

代码语言:javascript
复制
const context = require.context('./src', true, /.+\Spec\.js$/);
context.keys().forEach(context);
module.exports = context;

webpack.config.js

代码语言:javascript
复制
module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname,
    filename: 'dist/bundle.js'
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['', '.js', '.scss'],
    modulesDirectories: [
      'node_modules',
      'src'
    ]
  },
  module: {
    preLoaders: [
      {
        test: /\.js$/,
        loader: 'eslint-loader',
        exclude: /node_modules/
      }
    ],
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
    ],
  },
};

karma.config.js

代码语言:javascript
复制
var path = require('path');

module.exports = function (config) {
  config.set({
    browsers: ['PhantomJS'],
    singleRun: true,
    frameworks: ['mocha', 'sinon-chai'],
    files: [
      'tests.webpack.js'
    ],

    preprocessors: {
      'tests.webpack.js': ['webpack', 'sourcemap']
    },
    reporters: ['mocha', 'osx', 'coverage'],
    webpack: {
      module: {
        preLoaders: [
          {
            test: /\.js$/,
            exclude: [
              path.resolve('src/'),
              path.resolve('node_modules/')
            ],
            loader: 'babel'
          },
          {
            test: /\.js$/,
            include: path.resolve('src/'),
            loader: 'isparta'
          }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    },
    coverageReporter: {
      type: 'html',
      dir: 'coverage/'
    }
  });
};
EN

回答 2

Stack Overflow用户

发布于 2016-03-18 05:56:34

这就是我在我的项目中的方法,我将所有的测试放在每个组件附带的__test__文件夹中。您应该能够将其更改为正则表达式,如下面的/\.spec.js$/所示。

代码语言:javascript
复制
karmaConfig.webpack.module.preLoaders = [{
  test    : /\.(js|jsx)$/,
  include : new RegExp(config.dir_client),
  loader  : 'isparta',
  exclude : [
    /node_modules/,
    /__test__/,
  ],
}];

在您的情况下,您需要将exclude添加到配置的这一位。

代码语言:javascript
复制
{
    test: /\.js$/,
    include: path.resolve('src/'),
    loader: 'isparta'
}
票数 2
EN

Stack Overflow用户

发布于 2016-03-18 01:17:12

我通过在.forEach(context)之前放置一个.filter()来过滤掉我不想要的结果,从而解决了这个问题。

代码语言:javascript
复制
const includes = require('lodash/includes');
const context = require.context('./src', true, /\.test\.js$/);

context.keys()
  .filter(file => includes(file, './api/') === false)
  .forEach(context);

您也可以将其直接写入.forEach()。

代码语言:javascript
复制
const includes = require('lodash/includes');
const context = require.context('./src', true, /\.test\.js$/);

context.keys()
  .forEach(file => {
    if (includes(file, './api/') === false) {
      context(file);
    }
  });

如果不使用Lodash,可以使用:

代码语言:javascript
复制
file.indexOf('./api/') === -1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35796592

复制
相关文章

相似问题

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