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

升级到Angular 13后执行带--code-coverage参数的测试失败如何解决

问题原因
  • Angular 13 对测试工具链做了破坏性调整,移除了对karma-coverage、karma-coverage-istanbul-reporter的内置依赖,低版本Angular默认携带的相关插件,升级后需要手动维护
  • 旧版karma-coverage-istanbul-reporter已被废弃,Angular 13+ 统一使用@angular-devkit/build-angular内置的istanbul能力,不再兼容独立的istanbul reporter配置项
修复步骤
  1. 卸载废弃的旧依赖
# npm 环境
npm uninstall karma-coverage-istanbul-reporter
# yarn 环境
yarn remove karma-coverage-istanbul-reporter
  1. 安装新版本所需的覆盖率插件
# npm 环境
npm install karma-coverage --save-dev
# yarn 环境
yarn add karma-coverage --dev
  1. 调整karma.conf.js配置
  • 在plugins数组中补充注册karma-coverage插件:
plugins: [
  require('karma-coverage'),
  // 保留原有其他插件配置
]
  • 移除配置中所有coverageIstanbulReporter相关节点,reporters数组中保留coverage项即可
  1. (可选)在angular.json中配置覆盖率输出规则
    找到对应项目下的test节点,补充如下配置可自定义输出格式和路径:
"test": {
  "options": {
    "codeCoverage": true,
    "coverageReporter": {
      "dir": "coverage/waxion",
      "reports": ["html", "lcovonly", "text-summary", "cobertura"]
    }
  }
}
  1. 重新执行测试命令即可
ng test waxion --no-watch --code-coverage --browsers ChromeHeadless

内容的提问来源于stack exchange,提问作者Sudharshan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:54:07