You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Bazel构建的Karma+Jasmine TypeScript项目生成测试覆盖率报告

解决方案

1. 先解决编译错误

1.1 启用downlevelIteration编译选项

karma-typescript自带TypeScript编译能力,无tsconfig.json时可直接在karma.conf.js的karmaTypescriptConfig中配置编译选项:

karmaTypescriptConfig: {
    compilerOptions: {
        downlevelIteration: true,
        target: "ES6",
        module: "CommonJS",
        sourceMap: true // 生成源码映射,确保覆盖率报告关联到TS源码
    },
    bundlerOptions: {
        entrypoints: /\.spec\.ts$/
    }
}

1.2 修复模块找不到问题

  • 安装缺失的依赖包(建议安装到开发依赖):
npm install --save-dev snowflake-promise figlet gradient-string yargs commander
  • 替换硬编码的node_modules路径引用:将../node_modules/snowflake-promise/prod/types/ConnectionOptions改为直接引用包类型,比如import { ConnectionOptions } from "snowflake-promise";,适配Bazel构建的路径结构。

1.3 修复commander类型错误

将代码中option改为首字母大写的Option,匹配commander库的类型定义规范。

2. 配置测试覆盖率输出

调整karma.conf.js,过滤非业务代码,指定覆盖率报告格式:

module.exports = function(config) {
    config.set({
        frameworks: ["jasmine", "karma-typescript"],
        // 仅包含业务代码和测试文件,排除工具类脚本
        files: [
            "app/**/*.ts",
            "!app/LTools/performance/snowrunner/**/*.ts",
            "!app/LTools/module-checker/**/*.ts",
            "!app/LTools/misc-node-scripts/**/*.ts"
        ],
        preprocessors: {
            "app/**/*.ts": "karma-typescript"
        },
        reporters: ["progress", "karma-typescript"],
        karmaTypescriptConfig: {
            compilerOptions: {
                downlevelIteration: true,
                target: "ES6",
                module: "CommonJS",
                sourceMap: true
            },
            bundlerOptions: {
                entrypoints: /\.spec\.ts$/
            },
            // 配置覆盖率排除规则
            coverageOptions: {
                exclude: [
                    /\.spec\.ts$/, // 排除测试文件
                    /app\/LTools\/.*\.ts$/ // 排除工具类脚本
                ]
            },
            // 指定覆盖率报告输出格式与目录
            reports: {
                html: "coverage", // HTML报告输出到coverage目录
                lcovonly: "coverage/lcov.info", // LCOV格式用于CI集成
                text: "" // 控制台输出覆盖率摘要
            }
        },
        browsers: ["ChromeHeadless"], // 无头Chrome适配CI环境
        singleRun: true // 测试完成后自动退出
    });
};

3. 调整package.json配置

补充测试依赖并优化test命令:

"scripts": {
    "postinstall": "./postinstall.sh",
    "test": "karma start --single-run"
},
"devDependencies": {
    "karma": "^6.4.0",
    "karma-chrome-launcher": "^3.2.0",
    "karma-jasmine": "^5.1.0",
    "karma-typescript": "^5.5.3",
    "jasmine-core": "^4.5.0",
    "typescript": "^4.9.0",
    "snowflake-promise": "^10.0.0",
    "figlet": "^1.5.2",
    "gradient-string": "^2.0.2",
    "yargs": "^17.6.2",
    "commander": "^10.0.0"
}

4. 验证运行

执行npm test,编译错误修复后会自动运行测试,生成的coverage目录中打开index.html即可查看详细覆盖率报告。


内容的提问来源于stack exchange,提问作者Mauricio Gracia Gutierrez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 09:15:33