npm node deps tests coverage chat

1. Istanbul Instrumenter Loader

为随后的代码覆盖率报告的工具JS文件,它使用istanbul-lib-instrument

1.1. 安装

npm i -D istanbul-instrumenter-loader

1.2. 用法

1.2.1. 参考文献

1.2.2. 结构

├─ src
│ |– components
│ | |– bar
│ | │ |─ index.js
│ | |– foo/
│     |– index.js
|– test
| |– src
| | |– components
| | | |– foo
| | | | |– index.js

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

test/index.js

// requires 所有在 `project/test/src/components/**/index.js` 中的测试
const tests = require.context('./src/components/', true, /index\.js$/);

tests.keys().forEach(tests);

// requires 所有在 `project/src/components/**/index.js` 中的组件
const components = require.context('../src/components/', true, /index\.js$/);

components.keys().forEach(components);

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

karma.conf.js

config.set({
  ...
  files: [
    'test/index.js'
  ],
  preprocessors: {
    'test/index.js': 'webpack'
  },
  webpack: {
    ...
    module: {
      rules: [
        // 用 Istanbul 只监测业务代码
        {
          test: /\.js$/,
          use: { loader: 'istanbul-instrumenter-loader' },
          include: path.resolve('src/components/')
        }
      ]
    }
    ...
  },
  reporters: [ 'progress', 'coverage-istanbul' ],
  coverageIstanbulReporter: {
    reports: [ 'text-summary' ],
    fixWebpackSourcePaths: true
  }
  ...
});

1.2.3. 使用 Babel

你必须将instrumentation作为后置步骤(post step)运行

webpack.config.js

{
  test: /\.js$|\.jsx$/,
  use: {
    loader: 'istanbul-instrumenter-loader',
    options: { esModules: true }
  },
  enforce: 'post',
  exclude: /node_modules|\.spec\.js$/,
}

1.3. 选项

这个loader支持所有istanbul-lib-instrument支持的选项。

Name Type Default Description
debug {Boolean} false 打开调试模式
compact {Boolean} true 生成紧凑的代码
autoWrap {Boolean} false 设置为true ,允许返回函数之外的语句
esModules {Boolean} false 设置为true ,监测 ES2015 模块
coverageVariable {String} __coverage__ 全局覆盖变量的名称
preserveComments {Boolean} false output中保留注释
produceSourceMap {Boolean} false 将其设置为true,以生成用于测试的代码的源映射
sourceMapUrlCallback {Function} null 在原始代码中找到一个源映射URL时调用的回调函数。这个函数使用源文件名和源映射URL来调用

webpack.config.js

{
  test: /\.js$/,
  use: {
    loader: 'istanbul-instrumenter-loader',
    options: {...options}
  }
}

1.4. 维护者


Kir Belevich

Juho Vepsäläinen

Joshua Wiens

Michael Ciniawsky

Matt Lewis
Copyright © tuzhu008 2017 all right reserved,powered by Gitbook该文件修订时间: 2017-11-25 11:38:22

results matching ""

    No results matching ""