Skip to Content
DocumentationEcosystem@canyonjs/babel-plugin

@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 插件之后 };

它做了两件事:

  1. 检测 CI 流水线变量
  2. 收集参与编译的文件覆盖率初始数据

配置

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
shaGit 提交 SHA一般无需手动配置(自动检测 CI Provider)None
branchGit 仓库分支一般无需手动配置(自动检测 CI Provider)None
reportID用于区分不同测试用例可选None
compareTarget比较目标,作为当前 SHA 的基准,用于计算变更行覆盖率可选None
keepMap保留覆盖率地图,可选,默认为 true,当为 false 时,将生成 .canyon_output 文件可选true
instrumentCwd插桩工作目录,在多仓库模式下可能需要手动配置可选process.cwd()
provider源代码提供商(可选),默认为 gitlab可选gitlab
oneByOne配置代理服务器,可选,默认为 false。当为 true 时,会在编译过程中逐个报告每个文件的初始覆盖率数据。也可以是代理服务器配置可选false