Skip to content
Documentation
Esc
↑↓navigate↵open⌘Jpreview
On this page

Run Detox tests

Run Detox tests for a React Native or Expo app against a cloud iOS simulator, with Detox and Jest running on your own machine or CI runner.

Your Detox test files stay the same. Jest and the Detox tester run where they run today, on your laptop or a Linux CI runner, and the app runs on a Limrun iOS simulator. The @limrun/detox package supplies a Detox device driver for the remote simulator and helpers that wire the session together.

Detox on Limrun covers iOS simulator instances.

Before you start

  • LIM_API_KEY set up as in the Quickstart.
  • Node and a project with Detox 20 installed locally. @limrun/detox supports detox from 20.48.1 up to, but not including, 21.
  • The app under test on the instance. The runnable example uses Expo Go with an Expo project served from your machine; your own simulator build works too, installed as in Asset Storage.

How the integration works

Detox has three parts: the tester (Jest plus Detox, on your machine), a mediator server (detox run-server), and a native client that Detox loads into the app process. Limrun splits them across the network:

  • On your machine. prepareDetoxRun starts detox run-server on a free local port and opens a reverse tunnel, so the app on the simulator can reach that mediator.
  • On the simulator. Launching the app with the Detox runtime makes Limrun inject Detox’s native framework into the app process, pointed at the tunneled mediator URL and your session ID. The injected framework must match your Detox version exactly. Limrun ships the frameworks for Detox 20.48.1, 20.50.0, 20.50.1, 20.50.4, 20.51.0, and 20.51.1, and rejects other versions with the supported list.
  • In Detox. The @limrun/detox/driver device driver reads the instance ID from LIMRUN_IOS_ID instead of booting a local simulator, and takes device.takeScreenshot() images through the Limrun API.

Configure Detox

Point a Detox device at the Limrun driver and read the mediator URL and session ID from the environment, which runDetoxTest sets:

/** @type {Detox.DetoxConfig} */
module.exports = {
  testRunner: {
    args: { config: 'e2e/jest.config.cjs' },
    jest: { setupTimeout: 120000 },
  },
  session: {
    server: process.env.DETOX_SERVER || 'ws://localhost:8099',
    sessionId: process.env.DETOX_SESSION_ID || 'limrun-detox',
  },
  behavior: {
    init: { reinstallApp: false, exposeGlobals: true },
    cleanup: { shutdownDevice: false },
  },
  devices: {
    limrun: {
      type: '@limrun/detox/driver',
      device: { id: process.env.LIMRUN_IOS_ID || 'limrun-remote-ios' },
    },
  },
  configurations: {
    'ios.limrun': { device: 'limrun' },
  },
};

reinstallApp: false and shutdownDevice: false matter: the app is already on the instance, and the instance’s lifetime belongs to your script, not to Detox.

Run a test

The script below creates an instance with Expo Go installed, prepares the mediator and tunnel, runs detox test, and cleans up:

import { Ios, Limrun } from '@limrun/api';
import { prepareDetoxRun, runDetoxTest } from '@limrun/detox';

const limrun = new Limrun({ apiKey: process.env.LIM_API_KEY });

const instance = await limrun.iosInstances.create({
  wait: true,
  spec: {
    initialAssets: [{ kind: 'App', source: 'AssetName', assetName: 'appstore/Expo-Go-54.0.6.tar.gz' }],
  },
});
const client = await Ios.createInstanceClient({
  apiUrl: instance.status.apiUrl!,
  token: instance.status.token!,
});

const prepared = await prepareDetoxRun({ client });
try {
  const result = await runDetoxTest({
    configPath: './.detoxrc.cjs',
    configuration: 'ios.limrun',
    sessionId: prepared.sessionId,
    serverUrl: prepared.detoxServerUrl,
    iosId: instance.metadata.id,
    iosApiUrl: instance.status.apiUrl,
    iosToken: instance.status.token,
    artifactDirectory: prepared.artifactDirectory,
    extraEnv: {
      EXPO_URL: process.env.EXPO_URL!,
      LIMRUN_DETOX_SERVER_URL: prepared.remoteDetoxServerUrl,
      DETOX_VERSION: prepared.version,
    },
  });
  if (result.code !== 0) throw new Error(`Detox exited with ${result.code}`);
} finally {
  await prepared.cleanup();
  client.disconnect();
  await limrun.iosInstances.delete(instance.metadata.id);
}

prepareDetoxRun reads the Detox version from the detox package installed in the working directory unless you pass version. runDetoxTest runs your local node_modules/.bin/detox test and writes its output to detox.stdout.log, detox.stderr.log, and detox.summary.json in the artifact directory, with screenshots under detox-artifacts/.

The app launches from the test’s setup, so Detox’s native client connects before the first assertion. For Expo Go, launchExpoGoDetoxAppFromEnv launches Expo Go with the Detox runtime, opens EXPO_URL, and accepts Expo Go’s open prompt:

const { launchExpoGoDetoxAppFromEnv } = require('@limrun/detox');

describe('app', () => {
  beforeAll(async () => {
    process.env.DETOX_EXPECTED_TEXT = 'My App Home';
    await launchExpoGoDetoxAppFromEnv();
  }, 120000);

  it('shows the home screen', async () => {
    await expect(element(by.text('My App Home'))).toBeVisible();
  });
});

DETOX_EXPECTED_TEXT (or DETOX_EXPECTED_ELEMENT_ID) is what the helper waits for before it returns, so set it to something on your first screen.

For your own build instead of Expo Go, launch it with the Detox runtime from the test’s setup. runDetoxTest passes the instance URL, token, and session ID to Jest as LIMRUN_IOS_API_URL, LIMRUN_IOS_TOKEN, and DETOX_SESSION_ID; the mediator URL and version come from the extraEnv above:

const { Ios } = require('@limrun/api');

beforeAll(async () => {
  const client = await Ios.createInstanceClient({
    apiUrl: process.env.LIMRUN_IOS_API_URL,
    token: process.env.LIMRUN_IOS_TOKEN,
  });
  await client.launchApp('com.example.myapp', {
    mode: 'RelaunchIfRunning',
    runtime: {
      kind: 'detox',
      serverUrl: process.env.LIMRUN_DETOX_SERVER_URL,
      sessionId: process.env.DETOX_SESSION_ID,
      version: process.env.DETOX_VERSION,
    },
  });
  client.disconnect();
}, 120000);

The CLI exposes the same launch as lim ios launch-app <bundle-id> --runtime detox with --detox-server-url, --detox-session-id, and --detox-version.

The runnable example, which drives a sample Expo app through taps, text entry, navigation, and screenshots, is at typescript-sdk/examples/detox-ios.

Run from a Linux CI runner

No macOS host is needed. The CI job needs Node, your project’s dependencies including detox and @limrun/detox, and LIM_API_KEY, plus anything your app reads at launch, such as EXPO_URL:

name: Detox iOS
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22'

      - run: npm ci

      - name: Run Detox on Limrun
        env:
          LIM_API_KEY: ${{ secrets.LIM_API_KEY }}
        run: npx tsx run-detox.ts

Next steps

Last updated on