@canyonjs/babel-plugin
Source一个用于检测 CI 环境变量的 Babel 插件。与 istanbuljs 配合使用,完成代码插桩。
使用方法
安装:
npm install --save-dev @canyonjs/babel-plugin在 babel.config.js 中添加这些配置:
module.exports = {
plugins:
process.env.CI_COMMIT_REF_NAME === "test-coverage"
? ["istanbul", "canyon"]
: [],
// 注意插件顺序:canyon 插件应在 istanbul 插件之后
};它做了两件事:
- 检测 CI 流水线变量
- 收集参与编译的文件覆盖率初始数据
配置
babel.config.js
module.exports = {
plugins: [
"istanbul",
[
"canyon",
{
// #region == Step 2: CI Provider auto-detection, generally no manual configuration needed, see Support Provider documentation for details
repoID: "230614", // Repository ID
sha: "xxxxxxxxx", // Git Commit SHA
// #endregion
// #region == Step 4: Separate hit and map data (optional)
keepMap: false, // Keep coverage map, optional, default is false.
// #endregion
// #region == Step 5: Other configuration (optional)
instrumentCwd: "/path/to", // Instrumentation working directory, may need manual configuration in multi-repo mode
provider: "gitlab", // Source code provider (optional), default is gitlab
// #endregion
},
],
],
};| 配置项 | 描述 | 是否必填 | 默认值 |
|---|---|---|---|
| dsn | 覆盖率报告地址,CI 流水线变量 KEY 为 DSN | 是(根据需要在 CI 变量配置中填写或手动显式配置) | None |
| reporter | 用于区分不同用户的用户令牌,CI 流水线变量 KEY 为 REPORTER | 是(根据需要在 CI 变量配置中填写或手动显式配置) | None |
| repoID | 仓库 ID | 一般无需手动配置(自动检测 CI Provider) | None |
| sha | Git 提交 SHA | 一般无需手动配置(自动检测 CI Provider) | None |
| branch | Git 仓库分支 | 一般无需手动配置(自动检测 CI Provider) | None |
| reportID | 用于区分不同测试用例 | 可选 | None |
| compareTarget | 比较目标,作为当前 SHA 的基准,用于计算变更行覆盖率 | 可选 | None |
| keepMap | 保留覆盖率地图,可选,默认为 true,当为 false 时,将生成 .canyon_output 文件 | 可选 | true |
| instrumentCwd | 插桩工作目录,在多仓库模式下可能需要手动配置 | 可选 | process.cwd() |
| provider | 源代码提供商(可选),默认为 gitlab | 可选 | gitlab |
| oneByOne | 配置代理服务器,可选,默认为 false。当为 true 时,会在编译过程中逐个报告每个文件的初始覆盖率数据。也可以是代理服务器配置 | 可选 | false |