Karma 不是 运行 对 Jenkins 的测试

Karma not running tests on Jenkins

我是 CI/CD 和 Jenkins 的新手。 我正在为 Angular 应用程序设置 CI/CD。 它使用默认的测试运行器 Karma。 我有一个在 ChromeHeadless 模式下运行测试的步骤。 我的詹金斯管道如下

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'
        }
    }

}

}

在 package.json 我有一个脚本如下:

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

配置如下:

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

但是我收到以下错误

如有任何帮助,我将不胜感激。

我已经解决了这个问题,方法是在我的 karma.conf.js 文件中加入以下代码,让一个侦听器来监听 karma 基础设施错误

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

在此之后我可以看到以下错误:

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

然后我关注了 issue。

因此我可以去 Jenkins 并选择一个版本的 NodeJS 安装插件 和我的本地机器一样。

之后,我的ng test就成功了。