首页 > 解决方案 > 在 Azure DevOps Pipeline 中运行 Angular 测试时出现问题

问题描述

我有一个 Angular 7.2 的项目,在该项目中我创建了 3 个 Angular 库并使用 jasmin-karma 配置我的单元测试。我一个一个地运行所有测试。在我的本地环境中,一切都执行得很好,但是当我尝试在 azure devops 管道中运行时,测试不起作用。它向我显示了这样的错误“ Chrome 75.0.3770 (Windows 10.0.0) ERROR Disconnectedreconnect failed before timeout of 2000ms (ping timeout) ”。有谁知道这是怎么回事?总之谢谢大家。

这是我每个库的业力配置文件

// 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-phantomjs-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-junit-reporter'),
    ],
    files:["../../node_modules/jquery/dist/jquery.min.js",
    "../../node_modules/jquery-migrate/dist/jquery-migrate.min.js",
    "../../node_modules/lodash/lodash.min.js",
    "../../node_modules/moment/min/moment.min.js",
    "../../node_modules/app-resources/bits/Framework/scripts/app.js",],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in 
      browser
    },
   coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../../coverage'),
      reports: ['html', 'lcov', 'cobertura'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml', 'junit'/* 'sonarqube' */],
    sonarqubeReporter: {
      basePath: 'src/lib', // test files folder
      filePattern: '**/*spec.ts', // test files glob pattern
      encoding: 'utf-8', // test files encoding
      outputFolder: 'reports', // report destination
      legacyMode: false, // report for Sonarqube < 6.2 (disabled)
      reportName: (metadata) => { // report name callback
        /**
         * Report metadata array:
         * - metadata[0] = browser name
         * - metadata[1] = browser version
         * - metadata[2] = plataform name
         * - metadata[3] = plataform version
         */
        metadata[4] = 'lib';
        return metadata.concat('xml').join('.');
      }
    },
    junitReporter: {
      outputDir: '../../reports', // results will be saved as 
   $outputDir/$browserName.xml
    outputFile: undefined, // if included, results will be saved as 
   $outputDir/$browserName/$outputFile
   suite: '', // suite will become the package name attribute in xml 
   testsuite element
    useBrowserName: true, // add browser name to report and classes names
   nameFormatter: undefined, // function (browser, result) to customize the 
    name attribute in xml testcase element
   classNameFormatter: undefined, // function (browser, result) to customize 
   the classname attribute in xml testcase element
   properties: {} // key value pair of properties to add to the <properties> 
  section of the report
  },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', ],
    browserDisconnectTimeout: 10000,
    browserDisconnectTolerance: 3,
    browserNoActivityTimeout: 60000,
    flags: [
      '--disable-web-security',
      '--disable-gpu',
      '--no-sandbox'
    ],
    singleRun: true,
  });
};

标签: azure-devopskarma-jasminekarma-runnerangular7

解决方案


我建议您切换到 HeadlessChrome。2018 年 3 月 8 日,PhantomJS 的创建者宣布该项目将被归档。

https://semaphoreci.com/blog/2018/03/27/phantomjs-is-dead-use-chrome-headless-in-continuous-integration.html

我有同样的问题。PhantomJS 也需要大约 60 秒才能完成。HeadlessChrome 运行测试只需要大约 6 秒 =)


推荐阅读