Joseph Francis
Joseph Francis

Reputation: 1231

Running firebase emulator with Github Actions - CI iOS project

I'm trying to run my tests on Github actions with the firebase emulator. The emulator runs, but the test cases fail. However, if I run locally, the test cases pass. When run locally I execute firebase emulators:start --only firestore on the iOS project directory before running the tests.

The following is the yml file I have. The problem seems to be for the job "unit_test" (firebase emulators:exec --only firestore 'xcodebuild test-without-building -xctestrun $(find . -type f -name $XC_TEST_RUN) -destination "platform=iOS Simulator,name=$DEVICE" -derivedDataPath $DERIVED_DATA_PATH -enableCodeCoverage YES -only-testing:$UNIT_TEST_TARGET')

name: Feature Pipeline

on: [push]

jobs:
  build:
    name: Build
    env:
      SCHEME: "Hest"
      DEVICE: "iPhone 11"
      DERIVED_DATA_PATH: "DerivedData"
    runs-on: macOS-latest
    steps:
    - name: Checkout project
      uses: actions/checkout@v1
    - name: Build application
      run: |
        set -o pipefail && xcodebuild clean -scheme $SCHEME -destination "platform=iOS Simulator,name=$DEVICE" -derivedDataPath $DERIVED_DATA_PATH build-for-testing | xcpretty --color --simple
    - name: Upload products
      uses: actions/upload-artifact@v1
      with:
        name: Products
        path: DerivedData/Build/Products

  unit_test:
    name: Unit test
    env:
      DEVICE: "iPhone 11"
      DERIVED_DATA_PATH: "DerivedData"
      UNIT_TEST_TARGET: "HestTests"
      XC_TEST_RUN: "*.xctestrun"
    runs-on: macOS-latest
    needs: build
    steps:
    - name: Checkout project
      uses: actions/checkout@v1
    - name: Download products
      uses: actions/download-artifact@v1
      with:
        name: Products
    - name: Setting up NodeJS
      uses: actions/setup-node@v1
    - name: Install Firebase CLI tools
      run: npm install -g firebase-tools
    - name: Run unit/integration tests (Execute w/ Firebase emulators)
      run: firebase emulators:exec --only firestore 'xcodebuild test-without-building -xctestrun $(find . -type f -name $XC_TEST_RUN) -destination "platform=iOS Simulator,name=$DEVICE" -derivedDataPath $DERIVED_DATA_PATH -enableCodeCoverage YES -only-testing:$UNIT_TEST_TARGET'
    - name: Upload test logs
      uses: actions/upload-artifact@v1
      with:
        name: TestLogs
        path: DerivedData/Logs/Test

  ui_test:
    name: UI test
    env:
      DEVICE: "iPhone 11"
      DERIVED_DATA_PATH: "DerivedData"
      UI_TEST_TARGET: "HestUITests"
    runs-on: macOS-latest
    needs: build
    steps:
    - name: Checkout project 
      uses: actions/checkout@v1
    - name: Download products
      uses: actions/download-artifact@v1
      with:
        name: Products
    - name: Run UI tests
      run: |
        set -o pipefail && xcodebuild test-without-building -xctestrun $(find . -type f -name "*.xctestrun") -destination "platform=iOS Simulator,name=$DEVICE" -derivedDataPath $DERIVED_DATA_PATH -enableCodeCoverage YES -only-testing:$UI_TEST_TARGET | xcpretty  --color --simple

An example of a test case I ran is the following (failed with CI, but passed locally)

let expect = XCTestExpectation(description: "Something")
Firestore.firestore().collection("Something").addDocument(data: ["Test": "That"]) { error in
    XCTAssertNil(error)
    expect.fulfill()
}
wait(for: [expect], timeout: 4)

Upvotes: 0

Views: 837

Answers (1)

Joseph Francis
Joseph Francis

Reputation: 1231

It was difficult to resolve, but you have to specify a port as well as a host in the firebase.json file.

{
  "emulators": {
    "auth": {
      "port": 9099,
      "host": "localhost"
    },
    "functions": {
      "port": 5001,
      "host": "localhost"
    },
    "firestore": {
      "port": 8080,
      "host": "localhost"
    },
    "database": {
      "port": 9000,
      "host": "localhost"
    },
    "hosting": {
      "port": 5000,
      "host": "localhost"
    },
    "pubsub": {
      "port": 8085,
      "host": "localhost"
    },
    "storage": {
      "port": 9199,
      "host": "localhost"
    },
    "ui": {
      "enabled": true
    }
  }
}

Upvotes: 1

Related Questions