willieloea / e2e_exploration

This repository serves as a laboratory for me to learn about and implement end to end testing for React Native apps.
0 stars 0 forks source link

Running detox tests within Docker #1

Open louwjlabuschagne opened 2 weeks ago

louwjlabuschagne commented 2 weeks ago

@willieloea would be great if you could run this in a docker container, I quickly Chat GPTs something and it looks possible, not sure how far you'll get with these suggestions.

Yes, you can run end-to-end (E2E) tests with Detox and Jest in a Docker container. Here is a step-by-step guide to set it up:

Step 1: Set up your React Native project

Ensure you have a React Native project set up with Detox and Jest configured for E2E testing.

Step 2: Create a Dockerfile

Create a Dockerfile in the root of your project with the following content:

# Use a base image with Node.js and JDK (required for Detox)
FROM node:16-slim

# Install Android SDK and other dependencies
RUN apt-get update && apt-get install -y \
    openjdk-11-jdk \
    wget \
    unzip \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables for Android SDK
ENV ANDROID_SDK_ROOT /usr/local/android-sdk-linux
ENV PATH ${PATH}:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools

# Download and install Android Command Line Tools
RUN mkdir -p ${ANDROID_SDK_ROOT}/cmdline-tools && \
    cd ${ANDROID_SDK_ROOT}/cmdline-tools && \
    wget https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip -O cmdline-tools.zip && \
    unzip cmdline-tools.zip -d ${ANDROID_SDK_ROOT}/cmdline-tools && \
    rm cmdline-tools.zip && \
    mv ${ANDROID_SDK_ROOT}/cmdline-tools/cmdline-tools ${ANDROID_SDK_ROOT}/cmdline-tools/latest

# Accept licenses
RUN yes | sdkmanager --licenses

# Install necessary Android SDK packages
RUN sdkmanager "platform-tools" "platforms;android-30" "build-tools;30.0.3"

# Set up Detox dependencies
RUN npm install -g detox-cli

# Create a working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the project for Android
RUN detox build --configuration android.emu.release

# Start the Android emulator (headless)
RUN echo "no" | avdmanager create avd -n test -k "system-images;android-30;default;x86_64" && \
    ${ANDROID_SDK_ROOT}/emulator/emulator -avd test -no-audio -no-window -gpu off &

# Run Detox tests
CMD ["detox", "test", "--configuration", "android.emu.release"]

Step 3: Create a Docker Compose file (optional)

If you want to use Docker Compose to manage your services, create a docker-compose.yml file:

version: '3.7'

services:
  detox:
    build: .
    volumes:
      - .:/app
    environment:
      - ANDROID_SDK_ROOT=/usr/local/android-sdk-linux
    ports:
      - "8081:8081"

Step 4: Build and run the Docker container

Build the Docker image and run the container:

docker-compose build
docker-compose up

Alternatively, if you are not using Docker Compose, you can build and run the Docker container directly:

docker build -t react-native-detox .
docker run -it --rm -v $(pwd):/app react-native-detox

This setup will build your React Native project, start an Android emulator in headless mode, and run your Detox E2E tests.

Step 5: Troubleshoot

With this setup, you should be able to run your Detox and Jest E2E tests within a Docker container.

willieloea commented 2 weeks ago

Thank you. I am now learning about docker, with the goal of running tests in a container. I will let you know when I have something working.