0

A CircleCI CI/CD pipeline handling an Angular 7 application fails during the test step, with the CircleCI log suggesting the browser is not registered, although it runs once and it works!

I'm building a CircleCI CI/CD pipeline for an Angular 7 application that includes a test step which fails in a strange way. I've tried to change the karma.config.js parameters, to use Puppeteer, to no avail.

This is part of the WUF open source project, I'm working on the WUF-41-CI branch. Below are the details:

The CircleCI config.yml file:

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

The karma.config.js file:

/*
 * 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
    });
};

The Circle-CI log indicating that the first Chrome launch worked:

...
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
...

The Circle-CI log indicating that the first execution's tests passed:

...
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
...

The Circle-CI log indicating that the second Chrome launch 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
Rodrigo Silveira
  • 141
  • 2
  • 15
  • I believe I found the source of the problem. This application is quite complex; it consists of a library of Angular Components, and a Living Style Guide application that showcases the components. The problem seems to be that karma is also running the Angular Components' test files. Now, I'm working on preventing it. – Rodrigo Silveira May 11 '19 at 23:55
  • I'm one step closer! The last time I worked on this project it was prior to its migration to the new Angular Workspace / Project architecture. All indications are the standard `ng test` command triggers the tests for the Living Style Guide, as well as for the WUF components, with the later failing. – Rodrigo Silveira May 12 '19 at 03:43
  • [Duplicated on the CircleCI forum](https://discuss.circleci.com/t/circleci-ci-cd-pipeline-fails-on-chrome-headless-test-step/30332). – halfer May 12 '19 at 08:31
  • I have now ascertained that CircleCI is not part of the problem since I was able to reproduce it on my dev machine. – Rodrigo Silveira May 12 '19 at 16:37

3 Answers3

0

Is this the same issue as Chrome 73 stop supporting headless mode in background scheduled task?

If it is, it should work to downgrade to Chrome 72.

  • Does not seem like, but I'll check it out and report back – Rodrigo Silveira May 11 '19 at 18:12
  • I installed chromium@72.0.3626.121-r0 and observed the same error. Since I'm new to Docker and CircleCI, although the problem was not fixed, I learned a lot! – Rodrigo Silveira May 11 '19 at 20:41
  • 1
    @RodrigoSilveira: if this is not the correct answer, please do not accept it. You can upvote it instead if you think it was helpful. Ticking/checking it generally means "thanks, this worked for me". – halfer May 12 '19 at 08:30
0

The root cause of the problem are failing Angular unit tests; their failure was masked by the CircleCI output. I discovered this by exporting the CircleCI log and reading it carefully. Now, on to fixing unit test errors that work in normal Chrome mode and fail in headless mode ... these are bugs on the tests since I found and fixed a few already.

Rodrigo Silveira
  • 141
  • 2
  • 15
0

This behavior should occurs if you have various projects on your angular.json, for example a main project and an angular lib (considered as project also).

Each project has its own karma.conf.js, so probably you've updated the karma.conf.js of main project but not of Angular lib. Then tests run on all projects (=> multiple launches of karma) AND for angular lib the CircleCI_ChromeHeadless is not registered.

Victor Dias
  • 605
  • 6
  • 14