react-nativetestingexpogithub-actionsrealm

Testing Expo app using Realm on GitHub fails


I have created an Expo app using JavaScript. I have installed Realm in this Expo app. I have written snapshot tests for this app. I have also written tests for the Realm database. These tests work as expected.

The tests pass when I run them on my local machine.

I have pushed this app and the tests to GitHub. I have written a .github/workflows/testing.yml file to test this app each time I push to GitHub. These tests fail due to the fact that they take too long to run on GitHub. However, they run within seconds on my desktop PC, which is not a powerful machine.

How can I correctly run the tests each time I push to GitHub?

One example of the .github/workflows/testing.yml file I attempted to use:

name: Run Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: "20"
      - name: Install dependencies
        run: |
          npm install
      - name: Run tests
        env:
          CI: true
        run: |
          npm test --detectOpenHandles --forceExit

I have also attempted to create larger .github/workflows/testing.yml files which download Android Command Line Tools, install an Android Emulator and run the app on the Android Emulator before testing the code. However, this also fails.


Solution

  • Instead of using the npm command, test the code using the correct testing method found in your package.json file.

    For example, if the command in the package.json file is: "test": "jest --watchAll --runInBand --detectOpenHandles", the correct .github/workflows/testing.yml file would be:

    name: Run Tests
    on: [push, pull_request]
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
          - name: Install Dependencies
            run: npm install
          - name: Run tests
            run: npx jest --runInBand --detectOpenHandles --forceExit
    
    

    Don't use the --watchAll command as this could lead to the issue you were facing. Use the --forceExit command in the .github/workflows/testing.yml file to ensure the testing is exited.

    This way, you can keep the command in the package.json file the same and can continue testing the app on your local machine in the same way.