如何将生成的文件从webpack的覆盖范围中排除

如何将生成的文件从webpack的覆盖范围中排除,webpack,code-coverage,webpack-2,istanbul,Webpack,Code Coverage,Webpack 2,Istanbul,接下来,我将Istanbundle instrumenter添加到我的测试配置中,以获得一个显示其余文件的“未绑定”覆盖率报告。但是,捆绑测试文件仍然显示在build/中: ----------------------------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines

接下来,我将Istanbundle instrumenter添加到我的测试配置中,以获得一个显示其余文件的“未绑定”覆盖率报告。但是,捆绑测试文件仍然显示在
build/
中:

----------------------------|----------|----------|----------|----------|----------------|
File                        |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------------------------|----------|----------|----------|----------|----------------|
All files                   |    70.78 |    36.36 |    69.23 |    60.95 |                |
 build                      |    67.39 |    31.03 |    66.67 |    53.93 |                |
  tests.js                  |    67.39 |    31.03 |    66.67 |    53.93 |... 208,209,211 |
 src/routes/Home            |      100 |       75 |      100 |      100 |                |
  index.js                  |      100 |       75 |      100 |      100 |             11 |
 src/routes/Home/components |      100 |       75 |      100 |      100 |                |
  HomeView.js               |      100 |       75 |      100 |      100 |             18 |
----------------------------|----------|----------|----------|----------|----------------|
✨  Done in 14.19s.
这是到目前为止我的测试配置。我认为在伊斯坦布尔instrumenter loader子句中排除
build
就可以了,但它似乎没有:

var nodeExternals = require('webpack-node-externals');
var path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

var project = require('../project.config');
const inProject = path.resolve.bind(path, project.basePath);
const inProjectSrc = (file) => inProject(project.srcDir, file);
const __DEV__ = project.env === 'development';

const extractStyles = new ExtractTextPlugin({
  filename: 'styles/[name].[contenthash].css',
  allChunks: true,
  disable: __DEV__
});

module.exports = {
  entry: {
    main: ['./tests/test-bundler.js']
  },
  target: 'node',
  output: {
    path: path.resolve('./build', project.basePath),
    filename: 'build/tests.js'
  },
  externals: [nodeExternals()],
  module: {
    rules: [            
      {
        test: /\.js?$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            cacheDirectory: true,
                            plugins: [
                                'babel-plugin-transform-class-properties',
                                'babel-plugin-syntax-dynamic-import',
                                [
                                    'babel-plugin-transform-runtime',
                                    {
                                        helpers: true,
                                        polyfill: false, // we polyfill needed features in src/normalize.js
                                        regenerator: true
                                    },
                                ],
                                [
                                    'babel-plugin-transform-object-rest-spread',
                                    {
                                        useBuiltIns: true // we polyfill Object.assign in src/normalize.js
                                    },
                                ],
                            ],
                            presets: [
                                'babel-preset-react',
                                ['babel-preset-env', {
                                    targets: {
                                        ie9: true,
                                        uglify: true,
                                        modules: false
                                    }
                                }],
                            ]
                        }
                    }
                ],
        exclude: /node_modules/
      },
        {
                test: /\.(sass|scss)$/,
                use: [
          'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]__[hash:base64:5]',
          'sass-loader',
                ]
            },
            {
                test: /\.(gif|png|jpe?g|svg)$/i,
                loaders: [
                    'file-loader',
                    {
                        loader: 'image-webpack-loader',
                        query: {
                            progressive: true,
                            optipng: {
                                optimizationLevel: 7
                            },
                            gifsicle: {
                                interlaced: false
                            },
                            pngquant: {
                                quality: '65-90',
                                speed: 4
                            }
                        }
                    }
                ]
            },
            {
                // delays coverage til after tests are run, fixing transpiled source
                // coverage error
                enforce: 'post', 
                test: /\.js$/,
                exclude: /(tests|node_modules|bower_components|build)\//,
                loader: 'istanbul-instrumenter-loader'
            }
    ]
    },
  resolve: {
    extensions: ['.js', '.jsx']
  }
};

如何确保在计算覆盖率时不使用入口点
build/tests.js

我怀疑您的
exclude
正则表达式没有按预期工作。您在哪个环境中运行项目

无论如何,因为您只想在
src
文件夹上运行覆盖率套件,所以可以反转排除/包含逻辑并使用(记住需要节点的
路径
):


我也有同样的问题,在下面尝试添加folderNames,并成功地排除了 覆盖率报告中这些文件夹下的所有文件

{
   enforce: 'post', 
   loader: 'istanbul-instrumenter-loader'
   exclude: [ /(<folderName1>|<folderName2>|node_modules|\.spec\.ts$)/, ],
}
{
执行:"岗位",,
装载机:“伊斯坦布尔仪表装载机”
排除:[/(| |节点|模块| \.spec\.ts$)/,],
}

注意:我使用的组合karma+webpack+istanbul

这是
include
属性的一个优点。然而,结果是一样的。(这是在OSX上运行的。)
{
   enforce: 'post', 
   loader: 'istanbul-instrumenter-loader'
   exclude: [ /(<folderName1>|<folderName2>|node_modules|\.spec\.ts$)/, ],
}