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

Angular 13中Jest配置transform失效问题求助

Angular 13 + Jest 28: 解决 configSet.processWithEsbuild is not a function 错误

问题原因

这个错误本质是ts-jest尝试调用esbuild的方法但环境不兼容,常见于Angular项目未正确配置Jest专用预设,或ts-jest与Angular的构建工具链版本不匹配。Angular 13 + Jest 28的组合需要特定版本的jest-preset-angular来协调ts-jest与Angular的TypeScript转换逻辑,单纯依赖ts-jest会导致esbuild相关调用失败。

解决步骤

1. 安装匹配版本的jest-preset-angular

Jest 28对应jest-preset-angular的12.x.x版本,执行安装命令:

# npm
npm install --save-dev jest-preset-angular@12.x.x

# yarn
yarn add -D jest-preset-angular@12.x.x

2. 修正jest.config.js配置

替换原有的transform配置,改用Angular专用预设来处理转换:

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/src/test.ts'], // 确保存在这个测试初始化文件
  transform: {
    '^.+\\.(ts|js|html)$': 'jest-preset-angular',
  },
  testPathIgnorePatterns: ['/node_modules/', '/dist/'],
  moduleFileExtensions: ['ts', 'html', 'js', 'json'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.html$', // 处理Angular模板
    },
  },
};

如果坚持单独配置ts-jest,可添加useEsbuild: false禁用esbuild转换:

transform: {
  '^.+\\.ts$': ['ts-jest', { 
    tsconfig: 'tsconfig.spec.json',
    useEsbuild: false
  }],
}

3. 验证tsconfig.spec.json配置

确保测试专用的tsconfig包含必要配置:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": ["jest", "node"]
  },
  "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

4. 再次清除Jest缓存

npx jest --clearCache

5. 重新运行测试

npx jest

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 18:13:08