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

如何在NX monorepo中通过GitHub Actions并行运行Cypress测试?

NX Monorepo中实现Cypress E2E测试并行的解决方案

问题根源

你遇到的错误核心原因是:Cypress的并行机制是将同一套测试用例拆分到多台机器执行,要求所有参与并行的机器必须使用完全一致的测试集(specs)和环境参数。而你的App-A-e2e和App-B-e2e是两套独立的测试用例,强行放到同一个Cypress并行组里,就会出现specs不匹配的报错。

有效解决方案

方案一:为每个App创建独立的并行测试任务

这个方案让每个App的E2E测试单独组成一个Cypress并行组,各自拆分内部用例并行执行,互不干扰。

  1. 更新cypress-run.yaml工作流
    添加app_name输入参数,让工作流可以针对单个App执行测试:

    name: Run Cypress Tests
    
    on:
      workflow_call:
        inputs:
          test_environment:
            required: true
            type: string
          cypress_tag:
            required: true
            type: string
          app_name:
            required: true
            type: string
        secrets:
          MY_SECRET:
            required: true
    
    jobs:
      cypress-run:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout Repository
            uses: actions/checkout@v3
            with:
              fetch-depth: 0
    
          - name: Derive shas used for base and head required by nx affected commands
            uses: nrwl/nx-set-shas@v3
    
          - name: Setup Node
            uses: actions/setup-node@v3
            with:
              node-version: 16
    
          - name: Install dependencies
            run: yarn setup:ci
    
          - name: Cypress Run
            uses: cypress-io/github-action@v5
            with:
              # 直接指定单个App的E2E任务
              command: yarn nx run ${{ inputs.app_name }}-e2e:e2e --configuration=${{ inputs.test_environment }} --tag ${{ inputs.cypress_tag }}
              # 启用Cypress并行
              parallel: true
              record: true
              # 为每个App+环境创建独立的并行组
              group: "E2E - ${{ inputs.app_name }} - ${{ inputs.test_environment }}"
            env:
              CYPRESS_fileConfig: ${{ inputs.test_environment }}
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              MY_SECRET: ${{ secrets.MY_SECRET }}
              # 确保传入Cypress记录密钥
              CYPRESS_RECORD_KEY: "你的Cypress记录密钥"
    
  2. 修改main.yaml中的任务调用
    为每个App的每个环境创建独立的任务:

    # Staging环境测试
    cypress-staging-app-a-run:
      needs: deploy-staging
      uses: ./.github/workflows/cypress-run.yaml
      with:
        test_environment: staging
        cypress_tag: staging
        app_name: app-a
      secrets: inherit
    
    cypress-staging-app-b-run:
      needs: deploy-staging
      uses: ./.github/workflows/cypress-run.yaml
      with:
        test_environment: staging
        cypress_tag: staging
        app_name: app-b
      secrets: inherit
    
    # Production环境测试
    cypress-production-app-a-run:
      needs: deploy-production
      uses: ./.github/workflows/cypress-run.yaml
      with:
        test_environment: production
        cypress_tag: production
        app_name: app-a
      secrets: inherit
    
    cypress-production-app-b-run:
      needs: deploy-production
      uses: ./.github/workflows/cypress-run.yaml
      with:
        test_environment: production
        cypress_tag: production
        app_name: app-b
      secrets: inherit
    
  3. 验证单个App的project.json配置
    确保每个App的E2E任务已启用并行:

    "targets": {
      "e2e": {
        "executor": "@nrwl/cypress:cypress",
        "options": {
          "cypressConfig": "apps/app-a-e2e/cypress.config.ts",
          "testingType": "e2e",
          "browser": "chrome",
          "record": true,
          "key": "你的Cypress记录密钥",
          "parallel": true
        }
      }
    }
    

方案二:结合NX分布式任务执行(DTE)实现多层并行

如果想保留nx affected自动检测变更App的能力,同时实现:

  • NX层面并行运行多个App的E2E任务
  • 单个App内部的Cypress用例并行拆分
  1. 更新package.json脚本
    添加NX的并行参数,控制同时运行的App任务数量:

    "scripts": {
      "e2e:ci": "nx affected --target=e2e --parallel --max-parallel=2"
    }
    

    --max-parallel=2表示同时最多运行2个App的E2E任务,可根据你的CI资源调整。

  2. 调整cypress-run.yaml的Cypress步骤
    保留nx affected命令,同时启用Cypress并行:

    - name: Cypress Run
      uses: cypress-io/github-action@v5
      with:
        command: yarn e2e:ci --configuration=${{ inputs.test_environment }} --tag ${{ inputs.cypress_tag }}
        parallel: true
        record: true
        group: "E2E - Affected Apps - ${{ inputs.test_environment }}"
      env:
        CYPRESS_fileConfig: ${{ inputs.test_environment }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        MY_SECRET: ${{ secrets.MY_SECRET }}
        CYPRESS_RECORD_KEY: "你的Cypress记录密钥"
    

    这里NX会为每个受影响的App启动独立的Cypress进程,每个进程内部会自动拆分自己的测试用例并行执行,不会出现specs不匹配的问题。

关键注意事项

  • 确保每个Cypress并行组的测试集完全一致,要么是单个App的所有用例,要么是同一套共享用例
  • 必须启用Cypress的record模式(需要记录密钥),这是Cypress实现并行的前提
  • 如果使用NX的--parallel,要注意区分NX的任务并行(多App同时跑)和Cypress的用例并行(单个App的用例拆分到多机器)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 08:15:06