配置 test-runner 让其在 CI 中并行 4 个浏览器实例

解读

这道题表面问“并行 4 个浏览器”,实则考察候选人是否真正落地过 Storybook Test-Runner 的 CI 集成 与 资源调度 能力。国内一线厂普遍用 GitLab-CI / GitHub Actions + Docker 缓存 + 并发矩阵 跑组件自动化,面试官想确认你能否在 有限容器配额 下把 4 个浏览器进程 稳定并行、不踩端口冲突、不重复构建、失败重跑 也考虑进去。答不到“并发模型 + 资源隔离 + 产物缓存”三层,基本会被判“只玩过本地”。

知识点

  1. @storybook/test-runner 并发模型:底层是 Jest + Playwright,通过 jest --maxWorkers 与 PLAYWRIGHT_WORKERS 两层变量控制。
  2. 浏览器实例 ≠ 容器实例:4 个浏览器可跑在 单容器 4 核 上(进程级并发),也可拆成 4 个容器(矩阵级并发),国内厂为省时间多用后者。
  3. CI 资源配额:GitHub Actions 免费版 单 job 2 核,GitLab SaaS 单 job 4 核,必须 拆矩阵 才能真并行 4 浏览器。
  4. 端口隔离:Storybook 本地 dev-server 默认 6006,并发时需 动态端口 或 统一静态资源服务;官方推荐 build-storybook 后启 http-server,端口冲突问题归零。
  5. 缓存策略:国内网络拉 Chrome 慢,CI 里必须缓存 ~/.cache/ms-playwright 与 node_modules/.cache/storybook,否则每次重装浏览器会拖垮 2 min+。
  6. 失败重跑:test-runner 原生支持 jest.retryTimes,但 CI 层最好再加 action retry 与 artifacts 上传,方便回滚截图。

答案

下面给出一套 GitHub Actions 矩阵方案,可在 4 个容器 里各跑 1 个浏览器实例,总并发度 4,完全规避端口冲突 与 重复构建,可直接搬进国内项目。

# .github/workflows/test.yml
name: storybook-test

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: 'npm'
      - run: npm ci
      - run: npm run build-storybook -- --output-dir storybook-static
      - uses: actions/upload-artifact@v4
        with:
          name: storybook-static
          path: storybook-static

  test:
    needs: build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        shard: [1, 2, 3, 4]   # 4 个浏览器实例
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: 'npm'
      - run: npm ci
      - uses: actions/download-artifact@v4
        with:
          name: storybook-static
          path: storybook-static
      # 缓存浏览器二进制
      - name: Cache Playwright
        uses: actions/cache@v4
        with:
          path: ~/.cache/ms-playwright
          key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}
      - name: Install browsers
        run: npx playwright install chromium
      # 启动静态服务器,端口 8080,所有 shard 共用
      - name: Serve Storybook
        run: |
          npx http-server storybook-static -p 8080 -s &
          npx wait-on http://localhost:8080
      # 按 shard 切分用例,4 并发
      - name: Test
        run: |
          npx test-storybook \
            --url http://localhost:8080 \
            --maxWorkers=1 \
            --shard=${{ matrix.shard }}/4
        env:
          # 强制单浏览器实例,防止超配
          PLAYWRIGHT_WORKERS: 1
      # 失败截图上传
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report-${{ matrix.shard }}
          path: playwright-report

要点说明

  • 矩阵 shard 把用例平均切成 4 份,每份跑在独立容器,天然 4 浏览器并行。
  • build 一次,产物通过 artifact 共享,不重复执行 build-storybook。
  • http-server 托管静态资源,端口 8080 固定,无需担心 6006 冲突。
  • PLAYWRIGHT_WORKERS=1 + maxWorkers=1 双保险,每容器只启 1 个浏览器进程,符合“4 个浏览器实例”要求。
  • 缓存 ~/.cache/ms-playwright,国内镜像 下可再提速 30 秒。

若公司用 GitLab-CI,可把 parallel: 4 与 artifacts 语法等价替换,思路完全一致。

拓展思考

  1. 容器配额不足时 如何 单容器 4 浏览器?
    把矩阵换成 单 job,runs-on: ubuntu-latest-4core(企业自托管),PLAYWRIGHT_WORKERS=4 即可;但需 动态端口 或 统一静态服务,否则 Jest 多 worker 会抢 6006。
  2. 组件量级过万 后,4 并发仍跑 20 min+,可引入 Rerun failed tests only 与 Story 级别缓存哈希,把 无代码变更的 Story 直接跳过,阿里 B 站 已落地该方案,节省 60% 时间。
  3. 可视化报告聚合:多 shard 后报告分散,可在 CI 尾 job 统一 npx playwright merge-reports,再上传到 内部 Storybook 站点,设计 QA 一键回朔,面试时提及此闭环 可瞬间拉高段位。