istanbul-instrumenter-loader

Instrument JS files with istanbul-lib-instrument for subsequent code coverage reporting

安装

  1. npm i -D istanbul-instrumenter-loader

用法 用法” class=”icon-link” href=”#用法”>

#

结构

  1. ├─ src
  2. |– components
  3. | |– bar
  4. | |─ index.js
  5. | |– foo/
  6. |– index.js
  7. |– test
  8. | |– src
  9. | | |– components
  10. | | | |– foo
  11. | | | | |– index.js

为生成所有组件(包括你没写测试的那些)的代码覆盖率报告,你需要 require 所有业务测试的代码。相关内容在 karma-webpack 其他用法中有涉及

test/index.js

  1. // requires 所有在 `project/test/src/components/**/index.js` 中的测试
  2. const tests = require.context('./src/components/', true, /index\.js$/);
  3. tests.keys().forEach(tests);
  4. // requires 所有在 `project/src/components/**/index.js` 中的组件
  5. const components = require.context('../src/components/', true, /index\.js$/);
  6. components.keys().forEach(components);

ℹ️ 以下为 karma 的唯一入口起点文件

karma.conf.js

  1. config.set({
  2. ...
  3. files: [
  4. 'test/index.js'
  5. ],
  6. preprocessors: {
  7. 'test/index.js': 'webpack'
  8. },
  9. webpack: {
  10. ...
  11. module: {
  12. rules: [
  13. // 用 Istanbul 只监测业务代码
  14. {
  15. test: /\.js$/,
  16. use: { loader: 'istanbul-instrumenter-loader' },
  17. include: path.resolve('src/components/')
  18. }
  19. ]
  20. }
  21. ...
  22. },
  23. reporters: [ 'progress', 'coverage-istanbul' ],
  24. coverageIstanbulReporter: {
  25. reports: [ 'text-summary' ],
  26. fixWebpackSourcePaths: true
  27. }
  28. ...
  29. });

使用 Babel

You must run the instrumentation as a post step

webpack.config.js

  1. {
  2. test: /\.js$|\.jsx$/,
  3. use: {
  4. loader: 'istanbul-instrumenter-loader',
  5. options: { esModules: true }
  6. },
  7. enforce: 'post',
  8. exclude: /node_modules|\.spec\.js$/,
  9. }

Options Options” class=”icon-link” href=”#options”>

此 loader 支持 istanbul-lib-instrument 的所有配置选项

Name

Type

Default

Description

Name

debug

Type

{Boolean}

Default

false

Description

Turn on debugging mode

Name

compact

Type

{Boolean}

Default

true

Description

Generate compact code

Name

autoWrap

Type

{Boolean}

Default

false

Description

Set to true to allow return statements outside of functions

Name

esModules

Type

{Boolean}

Default

false

Description

Set to true to instrument ES2015 Modules

Name

coverageVariable

Type

{String}

Default

__coverage__

Description

Name of global coverage variable

Name

preserveComments

Type

{Boolean}

Default

false

Description

Preserve comments in output

Name

produceSourceMap

Type

{Boolean}

Default

false

Description

Set to true to produce a source map for the instrumented code

Name

sourceMapUrlCallback

Type

{Function}

Default

null

Description

A callback function that is called when a source map URL is found in the original code. This function is called with the source filename and the source map URL

webpack.config.js

  1. {
  2. test: /\.js$/,
  3. use: {
  4. loader: 'istanbul-instrumenter-loader',
  5. options: {...options}
  6. }
  7. }

Maintainers

istanbul-instrumenter-loader - 图1


Kir Belevich

istanbul-instrumenter-loader - 图2


  1. Juho Vepsäläinen

istanbul-instrumenter-loader - 图3


  1. Joshua Wiens

istanbul-instrumenter-loader - 图4


  1. Michael Ciniawsky

istanbul-instrumenter-loader - 图5


  1. Matt Lewis