Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ng test" can't do coverage report and TeamCity reporting at the same time

I'm trying to run unit tests for an Angular CLI project on a TeamCity build server.

If I use the command:

ng test --single-run --code-coverage

then, as expected, I get a test coverage report but the individual test results don't show up in TeamCity.

If I turn on TeamCity reporting:

ng test --single-run --code-coverage --reporters teamcity

then the tests pass and TeamCity reporting works perfectly, but it silently fails to produce a code coverage report. This behaviour is reproducible on my local machine, so it's nothing to do with how the build server is configured.

Is there any reason why these two options should be mutually exclusive?

Note - this isn't the same problem as Configuring code coverage report for Karma on TeamCity. If the report is there then TeamCity shows it correctly, but if I turn on the TeamCity reporting flag then the coverage folder simply doesn't exist.

Additional info:

  • Angular-CLI version 1.0.0-rc.4 (problem first discovered using 1.0.0-rc.1)
  • Karma version 1.4.1
  • Node version 6.9.4
  • Windows 7 Enterprise

Karma configuration:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma'),
      require('karma-teamcity-reporter')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['@angular/cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'coverage-istanbul']
              : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
like image 793
Alistair Green Avatar asked Mar 22 '17 17:03

Alistair Green


1 Answers

I raised this as an issue on GitHub and the Angular-CLI team pointed out the solution:

ng test --single-run --code-coverage --reporters=teamcity,coverage-istanbul
  • The --code-coverage option only works if the reporters list includes a coverage tool. If no coverage reporter is present, it fails silently.
  • The --reporters switch replaces the default reporters list, so coverage-istanbul has to be explicitly re-added.
  • The coverage-istanbul reporter does not produce a report on its own; the --code-coverage switch is still required.
like image 92
Alistair Green Avatar answered Sep 27 '22 19:09

Alistair Green