首页 > 解决方案 > Angular 单元测试卡在 GitHub Action CI 上

问题描述

我正在尝试使用 GitHub Actions 在默认 Angular 项目上设置 CI 管道。我可以在本地成功运行单元测试,但遇到 GitHub Actions 的问题。我需要更改什么才能使其成功运行?

name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x, 12.x, 14.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

这是此步骤的日志输出

> ng test

Compiling @angular/core/testing : es2015 as esm2015
Compiling @angular/platform-browser/testing : es2015 as esm2015
Compiling @angular/compiler/testing : es2015 as esm2015
Compiling @angular/platform-browser-dynamic/testing : es2015 as esm2015
Compiling @angular/common/testing : es2015 as esm2015
Compiling @angular/router/testing : es2015 as esm2015
- Generating browser application bundles...
17 12 2020 20:14:02.566:WARN [karma]: No captured browser, open http://localhost:9876/
17 12 2020 20:14:02.708:INFO [karma-server]: Karma v5.1.1 server started at http://localhost:9876/
17 12 2020 20:14:02.709:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
17 12 2020 20:14:02.713:INFO [launcher]: Starting browser Chrome
✔ Browser application bundle generation complete.
- Generating browser application bundles...
✔ Browser application bundle generation complete.
17 12 2020 20:14:07.847:WARN [karma]: No captured browser, open http://localhost:9876/
17 12 2020 20:14:08.707:ERROR [launcher]: Cannot start Chrome
    [2646:2646:1217/201408.612463:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:08.707:ERROR [launcher]: Chrome stdout: 
17 12 2020 20:14:08.707:ERROR [launcher]: Chrome stderr: [2646:2646:1217/201408.612463:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:08.712:INFO [launcher]: Trying to start Chrome again (1/2).
17 12 2020 20:14:08.860:ERROR [launcher]: Cannot start Chrome
    [2682:2682:1217/201408.854964:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:08.861:ERROR [launcher]: Chrome stdout: 
17 12 2020 20:14:08.861:ERROR [launcher]: Chrome stderr: [2682:2682:1217/201408.854964:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:08.863:INFO [launcher]: Trying to start Chrome again (2/2).
17 12 2020 20:14:09.006:ERROR [launcher]: Cannot start Chrome
    [2711:2711:1217/201409.000191:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:09.006:ERROR [launcher]: Chrome stdout: 
17 12 2020 20:14:09.007:ERROR [launcher]: Chrome stderr: [2711:2711:1217/201409.000191:ERROR:browser_main_loop.cc(1434)] Unable to open X display.

17 12 2020 20:14:09.008:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.

标签: node.jscontinuous-integrationangular-cligithub-actions

解决方案


你需要安装 puppeteer 等等一些额外的配置

npm install puppeteer --save-dev

在 karma.conf.js 下restartOnFileChange: true,添加

customLaunchers: {
  ChromeHeadlessCustom: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox', '--disable-gpu']
  }
}

在 package.json 中更新测试脚本为 "test": "ng test --watch=false --browsers=ChromeHeadlessCustom",


推荐阅读