在我的 Angular 项目中,我尝试使用 Google Chrome 在 GitHub Action 中使用 Karma 和 Jasmine 进行测试。
Google Chrome 启动时出现多个错误,然后在经过一些测试后死机。我在这个主题中尝试了 StackOverflow 的一些提示,但没有任何帮助。
我使用这个版本:
我有这个
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 Action 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://[email protected]/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! [email protected] test:ci: `ng test --watch=false --code-coverage --source-map true`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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.
正如你所看到的,测试开始了,在两次测试之后它就死了。但写了 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",
对于 GitHub Actions,自 2022 年 8 月 27 日起,命令再次更改!
"test:ci": "ng test --no-watch --no-progress --code-coverage --browsers=ChromeHeadlessCI",
还要确保在
karma.conf.js
中正确设置 karma conf:
browsers: ['ChromeHeadlessCI'],
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
}
这是文档的链接。
根据最新的 Angular 文档,该命令再次与 Angular 17
略有不同ng test --no-watch --no-progress --browsers=ChromeHeadless