Lesson 4 of 10
Detox - End-to-End Testing
Detox is Wix's gray-box E2E testing framework specifically designed for React Native. It runs the real app on a simulator or emulator and controls it programmatically. Unlike Appium (which taps at pixel coordinates), Detox uses element matchers that stay reliable across device sizes.
Installation
npm install --save-dev detox @types/detox
npx detox init
# .detoxrc.js
module.exports = {
testRunner: { args: { '$0': 'jest', config: 'e2e/jest.config.js' }, jest: { setupTimeout: 120000 } },
apps: {
'ios.debug': { type: 'ios.app', binaryPath: 'ios/build/Build/Products/Debug-iphonesimulator/MyApp.app', build: 'xcodebuild -workspace ios/MyApp.xcworkspace -scheme MyApp -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build' },
'android.debug': { type: 'android.apk', binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk', build: 'cd android && ./gradlew assembleDebug && cd ..' },
},
devices: {
simulator: { type: 'ios.simulator', device: { type: 'iPhone 15' } },
emulator: { type: 'android.emulator', device: { avdName: 'Pixel_6_API_34' } },
},
configurations: {
'ios.sim.debug': { device: 'simulator', app: 'ios.debug' },
'android.emu.debug': { device: 'emulator', app: 'android.debug' },
},
};E2E Test: Login Flow
// e2e/auth.test.ts
import { device, element, by, expect as detoxExpect, waitFor } from 'detox';
describe('Authentication flow', () => {
beforeAll(async () => {
await device.launchApp({ newInstance: true }); // fresh state
});
beforeEach(async () => {
await device.reloadReactNative();
});
it('shows login screen on launch', async () => {
await detoxExpect(element(by.id('login-screen'))).toBeVisible();
await detoxExpect(element(by.id('email-input'))).toBeVisible();
await detoxExpect(element(by.id('password-input'))).toBeVisible();
});
it('shows error for invalid credentials', async () => {
await element(by.id('email-input')).typeText('invalid@test.com');
await element(by.id('password-input')).typeText('wrongpass');
await element(by.id('sign-in-button')).tap();
await waitFor(element(by.id('error-message')))
.toBeVisible()
.withTimeout(5000);
await detoxExpect(element(by.text(/invalid credentials/i))).toBeVisible();
});
it('navigates to home screen after successful login', async () => {
await element(by.id('email-input')).clearText();
await element(by.id('email-input')).typeText('test@example.com');
await element(by.id('password-input')).clearText();
await element(by.id('password-input')).typeText('correctpassword');
await element(by.id('sign-in-button')).tap();
await waitFor(element(by.id('home-screen')))
.toBeVisible()
.withTimeout(8000);
});
it('persists session after app restart', async () => {
await device.sendToHome();
await device.launchApp({ newInstance: false }); // warm start
await waitFor(element(by.id('home-screen')))
.toBeVisible()
.withTimeout(5000);
// Should skip login screen - token restored from SecureStore
});
});
// Run: npx detox test --configuration ios.sim.debugTip
Tip
Practice Detox EndtoEnd Testing in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Automated user journeys on real iOS/Android simulators with automatic synchronization
Practice Task
Note
Practice Task - (1) Write a working example of Detox EndtoEnd Testing from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Detox EndtoEnd Testing is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?