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

使用detox与expo-updates时遇Jest意外token错误,如何解决?

解决Detox + Jest中使用expo-updates时的"Unexpected token 'export'"错误

问题说明

在Detox和Jest环境中引入expo-updates包后,测试套件无法运行,抛出以下语法错误:

FAIL test/e2e/starter.test.ts..
  ● Test suite failed to run

    Jest encountered an unexpected token

    ...
    
    Details:

    <Redacted>/node_modules/expo-updates/build/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './Updates';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import { expect, device, by } from 'detox'
    > 2 | import * as Updates from 'expo-updates'
        | ^
      3 |
      4 | describe('Example', () => {
      5 |   beforeAll(async () => {

当前使用的配置文件及版本:

jest.config.js

module.exports = {
  preset: 'ts-jest',
  rootDir: '..',
  testMatch: ['<rootDir>/**/*.test.ts'],
  testTimeout: 120000,
  maxWorkers: 1,
  reporters: ['detox/runners/jest/reporter'],
  globalSetup: 'detox/runners/jest/globalSetup',
  globalTeardown: 'detox/runners/jest/globalTeardown',
  testEnvironment: 'detox/runners/jest/testEnvironment',
  transformIgnorePatterns: ['node_modules/(?!expo-updates)'],
  transform: {
    '^.+\.ts?$': 'ts-jest',
    '^.+\.(js|jsx)$': 'babel-jest',
  },
  verbose: true,
}

babel.config.js

presets: [
  ['@babel/preset-env', { targets: { node: 'current' } }],
  '@babel/preset-typescript',
]

使用版本:

  • ts-jest: 29.0.3
  • babel: 29.3.1
  • expo-updates: 0.14.7

可行解决措施

  • 调整transformIgnorePatterns匹配规则
    当前规则可能未覆盖expo-updates的子模块,替换为更全面的匹配逻辑,确保所有Expo相关包都能被Babel转换:

    transformIgnorePatterns: [
      'node_modules/(?!((jest-)?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|expo-updates))'
    ]
    
  • 补全Babel配置文件
    确保babel.config.js是完整的CommonJS格式,若项目基于Expo,需添加babel-preset-expo:

    module.exports = {
      presets: [
        'babel-preset-expo',
        ['@babel/preset-env', { targets: { node: 'current' } }],
        '@babel/preset-typescript',
      ],
    };
    
  • 优化ts-jest与Babel的协作配置
    在jest.config.js中明确ts-jest启用Babel配置,并确保JS文件被正确转换:

    module.exports = {
      // ...其他配置
      preset: 'ts-jest',
      ts-jest: {
        useESM: false,
        babelConfig: true,
      },
      transform: {
        '^.+\\.(ts|tsx)$': 'ts-jest',
        '^.+\\.(js|jsx)$': 'babel-jest',
      },
    };
    
  • 清理缓存并重装依赖
    执行以下步骤重置依赖与缓存:

    1. 清除Jest缓存:npx jest --clearCache
    2. 删除依赖目录与锁文件:rm -rf node_modules package-lock.json yarn.lock
    3. 重新安装依赖:npm install 或 yarn install
  • 模拟expo-updates模块(非必要场景)
    如果E2E测试不需要实际调用更新功能,可在测试文件中模拟该模块:

    import * as Updates from 'expo-updates';
    
    jest.mock('expo-updates', () => ({
      __esModule: true,
      checkForUpdateAsync: jest.fn().mockResolvedValue(null),
      // 根据测试需求添加其他需模拟的方法
    }));
    

内容的提问来源于stack exchange,提问作者Milan Smolík

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 03:41:32