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

升级React Native至0.71.0后Jest测试报错:找不到typeorm模块

Expo SDK 48升级后Jest测试找不到typeorm模块的问题

升级Expo SDK 48(包含React Native从0.70.13升级至0.71.0)后,原本可正常运行的Jest测试套件报错:

Cannot find module 'typeorm' from '.ts'

应用构建可正常运行,已确认问题仅由React Native依赖升级导致,所有Expo相关依赖均为48.x版本。

我的Jest配置如下:

"jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@sentry/react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|i18n-js)"
    ],
    "setupFiles": [
      "./jest.setup.js"
    ]
  },

已尝试的解决方法:

  • 更新transformIgnorePatterns规则,将typeorm加入白名单
  • 配置Jest的moduleNameMapper
  • 升级至Expo SDK 49测试,问题仍存在

可能的解决方向

  1. 修正transformIgnorePatterns的白名单规则
    React Native 0.71对Jest的模块转换逻辑有调整,需确保typeorm被正确排除在忽略转换的列表外,修改后的规则可加入|typeorm:

    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@sentry/react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|i18n-js|typeorm)"
    ]
    
  2. 清除Jest缓存
    执行命令清除Jest的旧缓存,避免模块解析缓存导致异常:

    npx jest --clearCache
    
  3. 检查typeorm版本兼容性
    尝试升级typeorm至适配React Native 0.71的最新版本,同时确认代码中导入typeorm的路径、方式无错误。

  4. 添加明确的moduleNameMapper映射
    如果上述方法无效,可在Jest配置中添加精确的模块映射:

    "moduleNameMapper": {
      "^typeorm$": "<rootDir>/node_modules/typeorm"
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:15:14