NG 测试不能使用 Karma 自定义启动器

NG Test cannot use Karma custom launchers

我正尝试通过 Angular CLI 为 CI 运行 Karma,但我无法让 Karma 使用我的自定义启动器。

根据 this page on the Angular website,我在我的 Karma 配置中使用 customLauncher 属性。但是当我尝试 运行 我的测试 ng test --no-watch --no-progress --browsers=ChromeHeadlessCI 时,我收到一条错误消息,指出我的自定义启动器未注册。

    01 01 2021 14:10:10.121:INFO [karma-server]: Karma v5.2.3 server started at http://localhost:9876/
    01 01 2021 14:10:10.126:INFO [launcher]: Launching browsers ChromeHeadlessCI with concurrency unlimited
    01 01 2021 14:10:10.127:ERROR [launcher]: Cannot load browser "ChromeHeadlessCI": it is not registered! Perhaps you are missing some plugin?
    01 01 2021 14:10:10.128:ERROR [karma-server]: Error: Found 1 load error
     at Server.<anonymous> (C:\Users\trimb\Dropbox_Workspaces\weather\node_modules\karma\lib\server.js:213:27)
     at Object.onceWrapper (events.js:421:28)
     at Server.emit (events.js:327:22)
     at Server.EventEmitter.emit (domain.js:467:12)
     at emitListeningNT (net.js:1352:10)
     at processTicksAndRejections (internal/process/task_queues.js:79:21)
    npm ERR! code ELIFECYCLE

这是我的 Karma 配置:

module.exports = function (config) {
    config.set({
      basePath: '',
      frameworks: ['jasmine', '@angular-devkit/build-angular'],
      plugins: [
        require('karma-jasmine'),
        require('karma-chrome-launcher'),
        require('karma-jasmine-html-reporter'),
        require('karma-coverage-istanbul-reporter'),
        require('@angular-devkit/build-angular/plugins/karma')
      ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, './coverage/weather'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', 'ChromeHeadlessCI'],
    browserDisconnectTolerance: 3,
    browserDisconnectTimeout: 10000,
    browserNoActivityTimeout: 60000,
    customLaunchers: {
      ChromeHeadlessCI: {
        base: 'ChromeHeadless',
        flags: [
          '--no-sandbox'
        ]
      }
    },
    singleRun: true,
    restartOnFileChange: true
    });
  };

这是我 运行ning 从我的 package.json 通过 npm 发出的命令:

"test::cci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessCI",

我不确定从这里到哪里去。这是业力的错误?我的配置错误(更有可能是罪魁祸首)?我不知道。所有这些“代码”几乎都是样板文件。

发现问题。当然,这是一个配置问题。 angular.json文件中有属性指定自定义karmaConfig路径。

我不确定它是怎么发生的,但是当我 运行 ng test 因为 (as per Angualr.io)

The karma.conf.js file is a partial Karma configuration file. The CLI constructs the full runtime configuration in memory, based on application structure specified in the angular.json file, supplemented by karma.conf.js.

我得到的关于 customLoader being missing 的错误在技术上是正确的,因为 Karma 没有加载我的配置。因此,如果未找到 angular.json 文件中指定的 Karma 配置文件,CLI 将继续并为 Karma 创建一个“默认”配置 运行.

我的二分钱: 控制台应该报错找不到指定的karma config,可惜没有。检查你的路径人!