0

I am new to CI/CD and Jenkins. I am working on setting up CI/CD for an Angular application. It uses the default test runner which is Karma. I have a step that runs the tests in ChromeHeadless mode. My Jenkins pipeline is as below

pipeline {
agent any
tools {nodejs "node"}

environment {
  NO_PROXY="localhost, 0.0.0.0/4201, 0.0.0.0/9876"
}

stages {
    stage('Install') {
        steps {
            sh 'rm -rf node_modules'
            sh 'rm package-lock.json'
            sh 'rm -rf dist'
            sh 'npm install --legacy-peer-deps'
        }
    }

    stage('Test') {
        steps {
            sh 'npm run test-headless'
        }
    }

}

}

And in the package.json I have a script as below:

"test-headless": "ng test --browsers ChromeHeadless"

The config is as below:

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/instone'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadless'],
    customLaunchers: {
      ChromeHeadless: {
      base: 'Chrome',
      flags: [
        '--headless',
        '--disable-gpu',
        '--no-sandbox',
        '--remote-debugging-port=9222'
      ]
      }
  },
    singleRun: true,
    restartOnFileChange: true
  });
};

However I am getting the below error

enter image description here

I'd appreciate any assistance.

Thabo
  • 1,303
  • 2
  • 19
  • 40
  • Do you get the same error on the local development machine as well, when you run the `test-headless` npm script? – Milan Tenk Dec 12 '20 at 16:42
  • No the error only happens on the Jenkins local server – Thabo Dec 12 '20 at 18:41
  • I wonder what could be the difference between the local server and the development machine. Did you do the same steps on the development machine as well? I mean doing `rm -rf node_modules`, `rm package-lock.json`, `rm -rf dist`, `npm install --legacy-peer-deps` steps. – Milan Tenk Dec 12 '20 at 20:40
  • @MilanTenk no I did not do the same steps, could using the flag --legacy-peer-deps be causing the problem? The reason I use that flag is because I got some error with the dependecy tree when installing the packages – Thabo Dec 13 '20 at 11:33
  • 1
    I don't know, I suggest trying the same steps locally on the development machine. And when the issue is reproduced there, it is easier to find the root cause. – Milan Tenk Dec 13 '20 at 11:44
  • Thank you so much @MilanTenk, I will try that – Thabo Dec 13 '20 at 11:47

1 Answers1

0

I have solved this issue by having a listener to the karma infrastructure errors by having the below code in my karma.conf.js file

process.on('infrastructure_error', (error) => {
   console.error('infrastructure_error', error);
});

After this I was able to see the error below:

[karma-server]: TypeError: Cannot read property 'range' of undefined

And I followed this issue.

Thus I was able to go to Jenkins and selected a version of the NodeJS installations plugin to be the same as in my local machine.

After that, my ng test was successful.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Thabo
  • 1,303
  • 2
  • 19
  • 40