如何从webpack中的覆盖范围中排除构建的文件

时间:2017-06-09 16:12:25

标签: webpack code-coverage webpack-2 istanbul

关注this guide后,我已将istanbul-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.

到目前为止,这是我的测试配置。我认为在istanbul-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

2 个答案:

答案 0 :(得分:0)

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

无论如何,因为你只想在src文件夹上运行覆盖套件,你可能会颠倒排除/包含逻辑并继续使用(记得要求节点path):

{
   enforce: 'post', 
   test: /\.js$/,
   include: path.resolve(__dirname, 'src'),
   loader: 'istanbul-instrumenter-loader'
}

答案 1 :(得分:0)

我遇到了同样的问题,并在下面通过添加folderNames进行了尝试,并成功排除了 覆盖率报告

中这些文件夹下的所有文件
{
   enforce: 'post', 
   loader: 'istanbul-instrumenter-loader'
   exclude: [ /(<folderName1>|<folderName2>|node_modules|\.spec\.ts$)/, ],
}

注意:我使用的是业力+ Webpack +伊斯坦布尔