0

I inherited some front-end project (angular) that I'm willing to put on our CI environment (Jenkins + Sonar).

The test has almost complete code coverage with unit test. After some modification I'm able to build and run test (ng test) locally (Windows) and on Jenkins (Docker). However I can only run tests with code coverage (ng test --code-coverage) locally. When running 'ng test --code-coverage' on Jenkins it produces the infamous message:

��� Browser application bundle generation complete.
10 10 2022 08:08:36.973:ERROR [reporter]: Can not load reporter "coverage", it is not registered!
  Perhaps you are missing some plugin?

Please note, that I'm running all the angular build/test on Jenkins inside a docker instance (the Jenkins itself is not on Docker).

pipeline {
    agent any
    stages {
        stage('Update workspace') {
            agent any
            steps {
                git credentialsId: 'my-cred', url: 'ssh://....git'
            }
        }
        stage('Build') {
            agent {
                dockerfile {
                    filename 'Dockerfile'
                    args '-u 0:104'
                    reuseNode true
                }
            }
            steps {
                sh '''
                  npm install --legacy-peer-deps
                  npm install -g @angular/cli --force
                  ng build
                  ng test --code-coverage
                '''
            }
        }
    }
}

I tried adding npm install -g for karma and karma-coverage like suggested in other topics but without success.

Here is my karma.conf.js:

// 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-coverage'),
      require('karma-chrome-launcher'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageReporter: {
        includeAllSources: true,
        dir: 'coverage/xx',
        reporters: [
            { type: "lcov", subdir: "lcov-report" }
        ]
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },
    singleRun: true,
    restartOnFileChange: true
  });
};

Note that I'm not specifically bound to the usage of jasmine/karma so I'm open to suggestion for other framework that you would have tested successfully on Jenkins. I'm only targeting to get my tests running locally and on Jenkins (preferably a solution that is well maintained).

Here is the extract of my package.json with the devDependencies:

  "devDependencies": {
    "@angular-devkit/build-angular": "^13.2.5",
    "@angular/cli": "~13.2.5",
    "@angular/compiler-cli": "~13.2.4",
    "@angular/language-service": "~13.2.4",
    "@types/datatables.net": "^1.10.18",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.33",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "husky": "^4.2.3",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "^6.4.1",
    "karma-chrome-launcher": "^3.1.1",
    "karma-coverage": "^2.2.0",
    "karma-jasmine": "^5.1.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.5.5"
  },

For the reference here is the Dockerfile that I use:

# Dockerfile for Continuous integration (Jenkins), node + chromium
FROM node:16.17.1-alpine3.15

RUN apk update && apk add --no-cache nmap && \
    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 update && \
    apk add --no-cache \
      chromium \
      harfbuzz \
      "freetype>2.8" \
      ttf-freefont \
      nss

ENV CHROME_BIN=/usr/bin/chromium-browser
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

Once again, the only thing that is not working with my current solution is the code-coverage in the docker agent. I tried to active debug log, but while outputing a lot of stuff locally, it didn't provide any new information on Jenkins.

Any idea where this error come from?

B. Bri
  • 546
  • 2
  • 7
  • 23
  • I would try this: https://stackoverflow.com/a/70068218/7365461 and the second answer there. I think the second answer should work. – AliF50 Oct 11 '22 at 12:55

0 Answers0