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

Jest ^29.2.1 运行单元测试出现ts-jest配置弃用警告求解决

解决ts-jest弃用警告的配置修改方案

针对你遇到的三个弃用警告,直接修改jest.config.ts即可解决,具体修改点如下:

  1. 移除globals中的ts-jest配置块
    原配置里globals.ts-jest的写法已被废弃,需要把这些配置移到transform规则的参数中。

  2. 合并重复的transform规则
    ^.+\\.tsx?$正则已经能匹配.ts和.tsx文件,无需单独写.ts的规则,直接保留一条即可。

  3. 修正tsConfig为小写tsconfig
    原配置里的tsConfig命名已被废弃,需改为小写的tsconfig。

修改后的完整配置:

const config = {
  roots: ['<rootDir>/src'],
  verbose: true,
  globalSetup: './globalSetup.ts',
  testEnvironmentOptions: {
    url: 'http://localhost/',
  },
  setupFilesAfterEnv: [],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  preset: 'ts-jest',
  transform: {
    '^.+\\.tsx?$': ['ts-jest', {
      babel: true,
      tsconfig: 'tsconfig.json'
    }],
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testMatch: [
    '<rootDir>/**/(*.)test.(js|jsx|ts|tsx)',
    '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
    '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
  ],
}

module.exports = config

每个修改对应的警告解决说明:

  • 把ts-jest配置从globals移到transform中:解决Define 'ts-jest' config under 'globals' is deprecated警告
  • 将tsConfig改为tsconfig:解决"[jest-config].globals.ts-jest.tsConfig" is deprecated警告
  • 调整后的配置符合ts-jest最新规范:解决Your Jest configuration is outdated警告

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:42:38