You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Realm的Expo应用在GitHub Actions中测试超时失败如何解决?

问题:Expo + Realm应用在GitHub Actions中测试超时失败

我用JavaScript开发了一款Expo应用,集成了Realm数据库,本地编写的快照测试和Realm数据库测试用例都能正常运行,几秒就能通过。但推送到GitHub后,配置了.github/workflows/testing.yml想让每次推送自动执行测试,却出现测试超时失败的问题。试过基础配置(如下),也尝试过配置Android模拟器后运行测试,都失败了。请问该如何正确配置?

我尝试过的基础配置:

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

可行的配置方案

1. 补充Realm所需的系统依赖

Ubuntu-latest默认缺少Realm运行需要的部分系统库,先安装这些依赖能避免测试启动时的隐性阻塞:

- name: Install system dependencies for Realm
  run: |
    sudo apt-get update
    sudo apt-get install -y libgconf-2-4 libnotify4 libnss3 libxss1 libasound2

2. 优化测试运行参数

CI环境的资源有限,调整Jest的运行参数并禁用Realm的分析请求,减少耗时:

- name: Run tests
  env:
    CI: true
    REALM_DISABLE_ANALYTICS: "true" # 禁用Realm的网络分析请求
  run: |
    npx jest --maxWorkers=2 --detectOpenHandles --forceExit

--maxWorkers=2限制测试并发数,避免CI资源过载导致超时。

3. 优先使用无模拟器的测试环境

启动Android模拟器会消耗大量时间和资源,极容易触发超时。如果测试不需要真实设备环境,直接用Jest的模拟环境即可,无需配置模拟器。若确实需要设备测试,建议改用Expo EAS Build或macOS runner(支持iOS模拟器),但配置复杂度更高。

4. 升级并缓存依赖

使用最新兼容版本的expo、realm、jest-expo,同时启用npm缓存加速依赖安装:

- name: Set up Node.js
  uses: actions/setup-node@v4
  with:
    node-version: "20"
    cache: "npm" # 启用依赖缓存

5. 完整的配置示例

name: Run Expo + Realm Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
      - name: Install system dependencies for Realm
        run: |
          sudo apt-get update
          sudo apt-get install -y libgconf-2-4 libnotify4 libnss3 libxss1 libasound2
      - name: Install dependencies
        run: |
          npm install
      - name: Run tests
        env:
          CI: true
          REALM_DISABLE_ANALYTICS: "true"
        run: |
          npx jest --maxWorkers=2 --detectOpenHandles --forceExit

内容的提问来源于stack exchange,提问作者user27793975

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:02:28