Angular 在 Travis 上 运行 测试失败,错误 "Found 1 load error"

Angular test fail to run on Travis with error "Found 1 load error"

为什么我的 travis conf 无法执行 Angular 测试?

.travis.yml

language: node_js
node_js:
  - "16"

addons:
  chrome: stable

cache:
  directories:
    - ./node_modules
    - ./.angular

install:
  - npm install

script:
  - npm run test -- --code-coverage --no-watch --no-progress --browsers=ChromeHeadlessCI

after_script:
  - cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js

错误

ERROR [karma-server]: [39mError: Found 1 load error at Server. (/home/travis/build/node_modules/karma/lib/server.js:239:26) at Object.onceWrapper (node:events:509:28) at Server.emit (node:events:402:35) at Server.emit (node:domain:475:12) at emitListeningNT (node:net:1368:10) at processTicksAndRejections (node:internal/process/task_queues:82:21)

完整日志

> ng test "ng-simple-state" "--code-coverage" "--no-watch" "--no-progress" "--browsers=ChromeHeadlessCI"

[91m13 11 2021 19:17:19.850:ERROR [reporter]: [39mCan not load reporter "coverage", it is not registered!
  Perhaps you are missing some plugin?
[32m13 11 2021 19:17:19.872:INFO [karma-server]: [39mKarma v6.3.8 server started at http://localhost:9876/
[32m13 11 2021 19:17:19.873:INFO [launcher]: [39mLaunching browsers ChromeHeadlessCI with concurrency unlimited
[91m13 11 2021 19:17:19.873:ERROR [karma-server]: [39mError: Found 1 load error
    at Server.<anonymous> (/home/travis/build/node_modules/karma/lib/server.js:239:26)
    at Object.onceWrapper (node:events:509:28)
    at Server.emit (node:events:402:35)
    at Server.emit (node:domain:475:12)
    at emitListeningNT (node:net:1368:10)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
travis_time:end:19df8806:start=1636831036436372127,finish=1636831039910258223,duration=3473886096,event=script
[0K[31;1mThe command "npm run test -- --code-coverage --no-watch --no-progress --browsers=ChromeHeadlessCI" exited with 1.[0m
travis_fold:start:cache.2
[0Kstore build cache
travis_time:start:26903718
[0Ktravis_time:end:26903718:start=1636831039914334654,finish=1636831039917048564,duration=2713910,event=cache
[0Ktravis_time:start:13baeaab
[0K[32;1mchanges detected, packing new archive[0m
[32;1muploading main/cache--linux-xenial-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855--node-16.tgz[0m
[32;1mcache uploaded[0m
travis_time:end:13baeaab:start=1636831039920798859,finish=1636831054568641990,duration=14647843131,event=cache
[0Ktravis_fold:end:cache.2
[0K
travis_fold:start:after_script
[0Ktravis_time:start:09c7f64c
[0K$ cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js
cat: './coverage/*/lcov.info': No such file or directory
[error] "2021-11-13T19:17:34.746Z"  'error from lcovParse: ' 'Failed to parse string'
[error] "2021-11-13T19:17:34.747Z"  'input: ' ''
[error] "2021-11-13T19:17:34.748Z"  'error from convertLcovToCoveralls'

/home/travis/build/node_modules/coveralls/bin/coveralls.js:19
      throw err;
      ^
Failed to parse string
(Use `node --trace-uncaught ...` to show where the exception was thrown)
travis_time:end:09c7f64c:start=1636831054573043048,finish=1636831054753122792,duration=180079744,event=after_script
[0Ktravis_fold:end:after_script
[0K
Done. Your build exited with 1.

我假设您已将项目升级到 Angular13。我将 karma-coverage-istanbul-reporter 替换为 karma-coverage 并且工作正常。

karma.conf.js 中,您应该将 coverageIstanbulReporter 替换为 coverageReporter,如下所示

coverageReporter: {
  dir: require('path').join(__dirname, './coverage/<your-project-name>'),
  subdir: '.',
  reports: [
    { type: 'html' },
    { type: 'lcovonly' },
    { type: 'text-summary' }
  ]
},

有关更多详细信息,请查看此内容。 https://angular.io/guide/testing-code-coverage