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

使用Detox v20.1.1测试Expo TS应用遇DetoxRuntimeError求助

解决Detox v20.1.1测试Expo TypeScript应用时的DetoxRuntimeError

问题重现

测试启动后触发如下错误:

DetoxRuntimeError: Detox worker instance has not been installed in this context (DetoxSecondaryContext).

HINT: If you are using Detox with Jest according to the latest guide, please report this issue on our GitHub tracker:
https://github.com/wix/Detox/issues
Otherwise, make sure you call detox.installWorker() beforehand.

  3 | describe('Example', () => {
  4 |   beforeAll(async () => {
> 5 |     await device.launchApp()
    |                  ^
  6 |   })
  7 |
  8 |   beforeEach(async () => {

原detox.init()方法已移除,疑惑是否由jest.config.js中的globalSetup处理初始化流程。

当前配置文件

detoxrc.js

/** @type {Detox.DetoxConfig} */
module.exports = {
  testRunner: {
    args: {
      $0: 'jest',
      args: {
        config: 'test/e2e/jest.config.js',
      },
    },
    jest: {
      setupTimeout: 120000,
    },
  },
  apps: {
    ios: {
      type: 'ios.app',
      binaryPath: 'bin/Exponent.app',
      build: 'npm run ios',
    },
  },
  devices: {
    simulator: {
      type: 'ios.simulator',
      device: {
        type: 'iPhone SE (3rd generation)',
      },
    },
  },
  configurations: {
    'ios.sim': {
      device: 'simulator',
      app: 'ios',
    },
  },
}

test/e2e/jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  rootDir: '..',
  testMatch: ['<rootDir>/**/*.test.ts'],
  testTimeout: 120000,
  maxWorkers: 1,
  reporters: ['detox/runners/jest/reporter'],
  globalSetup: 'detox/runners/jest/globalSetup',
  globalTeardown: 'detox/runners/jest/globalTeardown',
  verbose: true,
}

解决方案

1. 补充Jest测试初始化配置

在test/e2e/jest.config.js中添加setupFilesAfterEnv,让Detox自动完成worker安装:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  rootDir: '..',
  testMatch: ['<rootDir>/**/*.test.ts'],
  testTimeout: 120000,
  maxWorkers: 1,
  reporters: ['detox/runners/jest/reporter'],
  globalSetup: 'detox/runners/jest/globalSetup',
  globalTeardown: 'detox/runners/jest/globalTeardown',
  // 新增该行,自动处理worker初始化
  setupFilesAfterEnv: ['detox/runners/jest/setup'],
  verbose: true,
}

该配置会在每个测试文件执行前加载Detox的Jest绑定,自动调用detox.installWorker(),解决上下文缺失问题。

2. 修正Expo应用的binaryPath配置

Expo项目的iOS应用路径通常不是bin/Exponent.app,需根据构建方式调整:

  • 若使用expo prebuild,路径应为ios/build/Build/Products/Debug-iphonesimulator/[你的应用名称].app
  • 若使用EAS Build,需将binaryPath指向下载后的构建产物路径
  • 若使用Expo Go测试,需将type改为ios.expo并调整binaryPath为Expo Go的安装路径(不推荐用于正式测试)

修改后的ios配置示例(以prebuild为例):

ios: {
  type: 'ios.app',
  binaryPath: 'ios/build/Build/Products/Debug-iphonesimulator/MyExpoApp.app',
  build: 'xcodebuild -workspace ios/MyExpoApp.xcworkspace -scheme MyExpoApp -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build',
},

3. 简化测试文件代码

测试文件中无需手动调用detox.init()或detox.installWorker(),保持原有结构即可:

describe('Example', () => {
  beforeAll(async () => {
    await device.launchApp()
  })

  beforeEach(async () => {
    await device.reloadReactNative()
  })

  it('should display welcome screen', async () => {
    await expect(element(by.text('Welcome'))).toBeVisible()
  })
})

4. 锁定依赖版本兼容性

确保detox、ts-jest、jest版本与Detox v20.1.1兼容,推荐在package.json中锁定版本:

{
  "devDependencies": {
    "detox": "20.1.1",
    "jest": "^29.0.0",
    "ts-jest": "^29.0.0",
    "typescript": "^5.0.0"
  }
}

内容的提问来源于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.05 15:25:15