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



按照本指南,我将伊斯坦布尔仪器器添加到我的测试配置中,以获得显示其余文件的"非捆绑"覆盖率报告。但是,捆绑的测试文件仍以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.

这是我到目前为止的测试配置。我以为在伊斯坦布尔乐器装载机条款中排除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文件夹上运行覆盖套件,因此您可以反转排除/包含逻辑并继续使用(请记住需要 Node 的path(:

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

我遇到了同样的问题,并通过添加文件夹名称在下面尝试,并成功排除覆盖范围报告中这些文件夹下的所有文件

{
   enforce: 'post', 
   loader: 'istanbul-instrumenter-loader'
   exclude: [ /(<folderName1>|<folderName2>|node_modules|.spec.ts$)/, ],
}

注意:我正在使用业力+网络包+伊斯坦布尔的组合

最新更新