[Webpack 2] Ensure all source files are included in test coverage reports with Webpack
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Webpack 2] Ensure all source files are included in test coverage reports with Webpack相关的知识,希望对你有一定的参考价值。
If you’re only instrumenting the files in your project that are under test then your code coverage report will be misleading and it will be difficult for you to track or enforce improvements to application coverage over time. In this lesson we’ll learn how to ensure all source files are included in coverage reports and how to enforce a specific threshold so you can work toward improving application code coverage.
Install:
npm i -D istanbul
Include all the src code not only test code:
const webpackEnv = {test: true} const webpackConfig = require(‘./webpack.config‘)(webpackEnv) process.env.BABEL_ENV = ‘test‘ // so we load the correct babel plugins const testGlob = ‘src/js/**/*.test.js‘ const srcGlob = ‘src/js/**/*!(test|stub).js‘ module.exports = function setKarmaConfig(config) { config.set({ basePath: ‘‘, frameworks: [‘mocha‘, ‘chai‘], files: [testGlob, srcGlob], preprocessors: { [testGlob]: [‘webpack‘], [srcGlob]: [‘webpack‘], }, webpack: webpackConfig, webpackMiddleware: {noInfo: true}, reporters: [‘progress‘, ‘coverage‘], coverageReporter: { reporters: [ {type: ‘lcov‘, dir: ‘coverage/‘, subdir: ‘.‘}, {type: ‘json‘, dir: ‘coverage/‘, subdir: ‘.‘}, {type: ‘text-summary‘}, ], }, port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: false, browsers: [‘Chrome‘], singleRun: true, concurrency: Infinity }) }
Use istanbul cli to check code coverage not below cetain number:
"check-coverage": "istanbul check-coverage --statements 23 --branches 5 --functions 9 --lines 24",
Add to validator:
"validate": "npm-run-all --parallel validate-webpack:* lint test --serial check-coverage",
Because it checkout coverage should run after test, so add ‘--serial‘ flag
以上是关于[Webpack 2] Ensure all source files are included in test coverage reports with Webpack的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Typescript 中使用 Webpack 'require' 和 'require.ensure'
vue按需加载组件-webpack require.ensure
vue项目优化之按需加载组件-使用webpack require.ensure
无法让 webpack require.ensure 分块方法与 react-router 一起使用并生成单独的捆绑文件