为什么我的 karma-config.js 触发多次 karma 启动,只有第一次启动?

Why does my karma-config.js triggers multiple launches of karma, with only the first working?

CircleCI CI/CD 管道处理 Angular 7 应用程序在测试步骤中失败,CircleCI 日志提示浏览器未注册,尽管它运行一次并且有效!

我正在为 Angular 7 应用程序构建一个 CircleCI CI/CD 管道,其中包含一个 test 步骤,但在奇怪的方式。我尝试更改 karma.config.js 参数以使用 Puppeteer,但无济于事。

这是 WUF 开源项目的一部分,我正在 WUF-41-CI 分支上工作。详情如下:

圈CIconfig.yml文件:

version: 2.0

# Acceptance Criteria
#
# Given a feature branch of WUF
# When it changes on GitHub
# Then its code is built and tested but not published
#
# Given origin/master of WUF
# When it changes on GitHub
# Then its code is built, tested, tagged and the packages pushed to NPM, and the project’s Git pages

jobs:
  build:
    docker:
      - image: wasvic/node_dev:1.0.0-FM-188-SNAPSHOT
        entrypoint: /bin/sh
    working_directory: ~/wuf
    environment:
      - DEPLOY_BRANCH: WUF-41-CI
      - TARGET_BRANCH: gh-pages
      - GH_EMAIL: anvil.open.source.machine.user@gmail.com
      - GH_NAME: anvil-open-source-machine-user
      - CHROME_BIN: /usr/lib/chromium/chrome
    # branches:
    #   only:
    #     - master
    #     - WUF-41-CI
    steps:
      - run:
          name: Install OS tools
          # TODO: replace with install_os_pkgs.sh script
          command: |
            apk update && apk add git
            apk add openssh
            apk add tar
            apk add ca-certificates
            apk update && apk upgrade && \
            echo @edge http://nl.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories
            echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories
            apk add --no-cache \
               chromium@edge \
               nss@edge \
               freetype@edge \
               harfbuzz@edge \
               ttf-freefont@edge
            apk add --no-cache ttf-freefont

      #  chekout projectt
      - checkout
      - restore_cache:
          keys:
            # when lock file changes, use increasingly general patterns to restore cache
            - yarn-v1-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - yarn-v1-{{ .Branch }}-
            - node-v1-
      - run:
          name: Install WUF
          command: |
            yarn bootstrap
      - save_cache:
          paths:
            - ./node_modules  # location depends on npm version
          key: yarn-v1-{{ .Branch }}-{{ checksum "yarn.lock" }}
      - run:
          name: Build WUF
          command: |
            ng build --aot --base-href /wuf/ --deploy-url /wuf/
      - run:
          name: Test WUF
          command: |
            # ng test --browsers CircleCI_ChromeHeadless --watch=false
            ng test --browsers CircleCI_ChromeHeadless
            # ng test
      - run:
          name: Deploy WUF
          # TODO: replace with a deploy.sh script
          command: |
            if [ $CIRCLE_BRANCH == $DEPLOY_BRANCH ]; then
              git config --global user.email $GH_EMAIL
              git config --global user.name $GH_NAME

              git clone $CIRCLE_REPOSITORY_URL out

              cd out
              git checkout $TARGET_BRANCH || git checkout --orphan $TARGET_BRANCH
              git rm -rf .
              cd ..

              cp -a dist/. out/.

              mkdir -p out/.circleci && cp -a .circleci/. out/.circleci/.
              cd out

              git add -A
              git commit -m "Automated deployment to GitHub Pages: ${CIRCLE_SHA1}" --allow-empty

              git push origin $TARGET_BRANCH
            fi

karma.config.js文件:

/*
 * Copyright (c) 2018 Dematic, Corp.
 * Licensed under the MIT Open Source: https://opensource.org/licenses/MIT
 */

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

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'),
            reports: ['html', 'lcovonly'],
            fixWebpackSourcePaths: true
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['Chrome', 'CircleCI_ChromeHeadless'],
        customLaunchers: {
          CircleCI_ChromeHeadless: {
            base: 'ChromeHeadless',
            flags: [
              '--headless',
              '--disable-gpu',
              '--disable-translate',
              '--disable-extensions',
              '--no-sandbox',  // Added to fix an issue where of Failed to connect to chrome browser
             '--remote-debugging-port=9222',
            ],
          }
        },
        singleRun: true
    });
};

Circle-CI 日志 表明第一个 Chrome 启动成功:

...
1 05 2019 00:10:47.700:INFO [launcher]: Launching browsers CircleCI_ChromeHeadless with concurrency unlimited
 11% building 9/12 modules 3 active .../root/wuf/src/app/app.component.spec.ts 11% building 10/12 modules 2 active ...assets/dummydata/branding/branding.scss11 05 2019 00:10:47.732:INFO [launcher]: Starting browser ChromeHeadless
...

Circle-CI日志表明第一次执行的测试通过:

...
HeadlessChrome 73.0.3683 (Linux 0.0.0): Executed 69 of 69 SUCCESS (0 secs / 12.127 secs)
HeadlessChrome 73.0.3683 (Linux 0.0.0): Executed 69 of 69 SUCCESS (12.186 secs / 12.127 secs)
TOTAL: 69 SUCCESS
TOTAL: 69 SUCCESS
...

Circle-CIlog表示第二次Chrome启动FAILED:

...
11 05 2019 00:11:29.320:INFO [launcher]: Launching browsers CircleCI_ChromeHeadless with concurrency unlimited
11 05 2019 00:11:29.320:ERROR [launcher]: Cannot load browser "CircleCI_ChromeHeadless": it is not registered! Perhaps you are missing some plugin?
11 05 2019 00:11:29.320:ERROR [karma-server]: Error: Found 1 load error

这个问题和Chrome 73 stop supporting headless mode in background scheduled task?

是同一个问题吗

如果是,应该可以降级到 Chrome72。

问题的根本原因是 Angular 单元测试失败;他们的失败被 CircleCI 输出掩盖了。我通过导出 CircleCI 日志并仔细阅读发现了这一点。现在,开始修复在正常 Chrome 模式下工作而在无头模式下失败的单元测试错误……这些是测试中的错误,因为我已经发现并修复了一些错误。

如果您的 angular.json 上有多个项目,例如一个主项目和一个 angular 库(也被视为项目),就会发生此行为。

每个项目都有自己的 karma.conf.js,所以您可能已经更新了主项目的 karma.conf.js 而不是 Angular 库。然后在所有项目上测试 运行(=> 多次启动业力)并且对于 angular lib CircleCI_ChromeHeadless 未注册。