首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

istanbul-instrumenter-loader

使用 istanbul-lib-instrument 将仪器 JS 文件用于随后的代码覆盖率报告

安装

代码语言:javascript
复制
npm i -D istanbul-instrumenter-loader

用法

结构

代码语言:javascript
复制
├─ src
│ |– components
│ | |– bar
│ | │ |─ index.js
│ | |– foo/
│     |– index.js
|– test
| |– src
| | |– components
| | | |– foo
| | | | |– index.js

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

test/index.js

代码语言:javascript
复制
// requires all tests in `project/test/src/components/**/index.js`
const tests = require.context('./src/components/', true, /index\.js$/);

tests.keys().forEach(tests);

// requires all components in `project/src/components/**/index.js`
const components = require.context('../src/components/', true, /index\.js$/);

components.keys().forEach(components);

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

karma.conf.js

代码语言:javascript
复制
config.set({
  ...
  files: [
    'test/index.js'
  ],
  preprocessors: {
    'test/index.js': 'webpack'
  },
  webpack: {
    ...
    module: {
      rules: [
        // instrument only testing sources with Istanbul
        {
          test: /\.js$/,
          use: { loader: 'istanbul-instrumenter-loader' },
          include: path.resolve('src/components/')
        }
      ]
    }
    ...
  },
  reporters: [ 'progress', 'coverage-istanbul' ],
  coverageIstanbulReporter: {
    reports: [ 'text-summary' ],
    fixWebpackSourcePaths: true
  }
  ...
});

使用 Babel

您必须将该检测作为后续步骤运行

webpack.config.js

代码语言:javascript
复制
{
  test: /\.js$|\.jsx$/,
  use: {
    loader: 'istanbul-instrumenter-loader',
    options: { esModules: true }
  },
  enforce: 'post',
  exclude: /node_modules|\.spec\.js$/,
}

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

名称

类型

默认值

描述

debug

{Boolean}

false

打开调试模式

compact

{Boolean}

true

autoWrap

{Boolean}

false

生成紧凑的代码

esModules

{Boolean}

false

设置为true以检测ES2015模块

coverageVariable

{String}

__coverage__

全局覆盖变量的名称

preserveComments

{Boolean}

false

保留输出中的注释

produceSourceMap

{Boolean}

false

设置为true以生成已检测代码的source map

sourceMapUrlCallback

{Function}

null

在原始代码中找到源映射URL时调用的回调函数。 使用源文件名和源映射URL调用此函数

webpack.config.js

代码语言:javascript
复制
{
  test: /\.js$/,
  use: {
    loader: 'istanbul-instrumenter-loader',
    options: {...options}
  }
}

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com