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

1

5

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
  });
};
Varela answered 22/3, 2017 at 17:15 Comment(0)
V
10

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.
Varela answered 27/3, 2017 at 13:11 Comment(2)
Hi, In which directory would you find the output of these reports?Many
The code coverage report is a set of HTML files in a directory called 'coverage' under the root of the project. The TeamCity report (outcomes of the unit tests) goes to standard output and is only useful if the tests are being run on a TeamCity build server. When running builds on my local machine, I turn the TeamCity report off to reduce noise in the terminal.Varela

© 2022 - 2024 — McMap. All rights reserved.