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

如何使用当前版本ts-jest生成正确的覆盖率映射?

新版本ts-jest覆盖率映射错误修复方案

  • 检查tsconfig.json的compilerOptions配置,确保开启了sourcemap支持:
{
  "compilerOptions": {
    "sourceMap": true,
    // 也可使用 inlineSourceMap: true
  }
}

ts-jest的覆盖率映射依赖TypeScript生成的sourcemap完成,未开启该配置会直接导致映射错位。

  • 确认jest.config.js配置正确性:
    优先使用官方预设,避免自定义transform配置出错:
    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node',
      collectCoverageFrom: [
        'src/**/*.{ts,tsx}',
        '!src/**/*.d.ts',
        // 按需排除测试文件、类型声明文件等不需要统计覆盖率的文件
      ]
    }
    
    若手动配置transform,需保证ts/tsx文件匹配到ts-jest转译器:
    transform: {
      '^.+\\.tsx?$': 'ts-jest'
    }
    
  • 确认jest与ts-jest大版本匹配:jest 28对应ts-jest 28,jest 29对应ts-jest 29,跨大版本混用会出现各类兼容性问题。
  • 清理jest缓存后重新执行覆盖率命令:
npx jest --coverage --no-cache --coverageDirectory='coverage'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 05:39:04