Angular 项目 Karma/Jasmine 测试在 GitHub Google Chrome 下的操作失败 Ubuntu

Angular project Karma/Jasmine tests fails on GitHub Action under Google Chrome on Ubuntu

在我的 Angular 项目中,我尝试使用 Google Chrome 在 GitHub 动作中使用 Karma & Jasmine 进行测试。

Google Chrome 以多个错误开始,然后在一些测试后死亡。我在本主题中尝试了来自 Whosebug ansers 的一些提示,但没有任何帮助。

我使用这个版本:

我有这个 karma.conf.js 文件:

module.exports = function (config) {
  config.set({
    browserNoActivityTimeout: 500000,
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-coverage'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false
    },
    coverageReporter: {
      dir: 'reports/coverage',
      reporters: [
        { type: 'html', subdir: 'report-html' },
        { type: 'lcov', subdir: 'report-lcov' }
      ],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
    customLaunchers: {
        ChromeHeadlessCI: {
          base: 'ChromeHeadless',
          flags: ['--no-sandbox', '--disable-gpu']
        }
    },
    singleRun: false,
    restartOnFileChange: true
  });
};

这是我的 GitHub 操作 yml 文件:

name: Angular test
on: [push]
jobs:
  ng-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [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 }}
    - name: npm ci
      run: npm ci
    - name: Angular test in production mode
      run: npm run test:ci

package.json 这是脚本部分:

"test:ci": "ng test --watch=false --code-coverage --source-map true",

我收到以下错误:

> ng test --watch=false --code-coverage --source-map true

- Generating browser application bundles (phase: setup)...
- @auth0/angular-jwt [es2015/esm2015] (git+https://github.com/auth0/angular2-jwt)
- ngx-bootstrap/tabs [es2015/esm2015] (git+ssh://git@github.com/valor-software/ngx-bootstrap.git)
- @fortawesome/angular-fontawesome [es2015/esm2015] (https://github.com/FortAwesome/angular-fontawesome)
- ddata-ui-common [es2015/esm2015] ()
- ddata-ui-dialog [es2015/esm2015] ()
- @tinymce/tinymce-angular [es2015/esm2015] (https://github.com/tinymce/tinymce-angular.git)

- ngx-color-picker [es2015/esm2015] ()

- ngx-material-timepicker [es2015/esm2015] (https://github.com/Agranom/ngx-material-timepicker.git)
✔ Browser application bundle generation complete.
10 03 2022 14:44:41.942:INFO [karma-server]: Karma v6.3.16 server started at http://localhost:9876/
10 03 2022 14:44:41.953:INFO [launcher]: Launching browsers Chrome, ChromeHeadless, ChromeHeadlessCI with concurrency unlimited
10 03 2022 14:44:41.958:INFO [launcher]: Starting browser Chrome
10 03 2022 14:44:42.055:INFO [launcher]: Starting browser ChromeHeadless
10 03 2022 14:44:42.141:INFO [launcher]: Starting browser ChromeHeadless
10 03 2022 14:44:43.220:INFO [Chrome Headless 98.0.4758.102 (Linux x86_64)]: Connected on socket eilZCAlj3zsV4RbPAAAD with id 4879597
10 03 2022 14:44:43.239:INFO [Chrome Headless 98.0.4758.102 (Linux x86_64)]: Connected on socket 03Ia71912SRh3xC_AAAC with id 64299573
10 03 2022 14:44:44.357:ERROR [launcher]: Cannot start Chrome
    [0310/144442.711534:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144442.711623:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:44.357:ERROR [launcher]: Chrome stdout: 
10 03 2022 14:44:44.357:ERROR [launcher]: Chrome stderr: [0310/144442.711534:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144442.711623:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:44.394:INFO [launcher]: Trying to start Chrome again (1/2).
10 03 2022 14:44:44.819:ERROR [launcher]: Cannot start Chrome
    [0310/144444.585530:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144444.585587:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:44.819:ERROR [launcher]: Chrome stdout: 
10 03 2022 14:44:44.819:ERROR [launcher]: Chrome stderr: [0310/144444.585530:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144444.585587:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:44.823:INFO [launcher]: Trying to start Chrome again (2/2).
10 03 2022 14:44:45.296:ERROR [launcher]: Cannot start Chrome
    [0310/144445.046018:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144445.046104:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:45.297:ERROR [launcher]: Chrome stdout: 
10 03 2022 14:44:45.297:ERROR [launcher]: Chrome stderr: [0310/144445.046018:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[0310/144445.046104:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

10 03 2022 14:44:45.299:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 0 of 530 SUCCESS (0 secs / 0 secs)

Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 0 of 530 SUCCESS (0 secs / 0 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 0 of 530 SUCCESS (0 secs / 0 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 0 of 530 SUCCESS (0 secs / 0 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 SUCCESS (0 secs / 0.505 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 SUCCESS (0 secs / 0.503 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 SUCCESS (0 secs / 0.505 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 (skipped 509) SUCCESS (0 secs / 0.503 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 (skipped 529) SUCCESS (1.435 secs / 0.505 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 (skipped 529) SUCCESS (1.584 secs / 0.503 secs)
Chrome Headless 98.0.4758.102 (Linux x86_64): Executed 1 of 530 (skipped 529) SUCCESS (1.435 secs / 0.505 secs)
TOTAL: 2 SUCCESS
TOTAL: 2 SUCCESS
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-project@11.0.0 test:ci: `ng test --watch=false --code-coverage --source-map true`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the my-project@11.0.0 test:ci script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-03-10T14_44_49_345Z-debug.log
Error: Process completed with exit code 1.

如您所见,测试开始了,经过 2 次测试后它就死了。但是写了530个测试。

知道如何解决这个问题吗?

不知道 GitHub 操作是否与 Jenkins 相同。但是在 Jenkins 上你需要 运行 测试 --browsers=ChromeHeadless。错误看起来像 GitHub 正在尝试打开 Chrome.

所以尝试在 package.json

中更改您的 test:ci
"test:ci": "ng test --browsers=ChromeHeadless --progress=false --watch=false --code-coverage --source-map=false",