2

We an angular app that is tested like this:

ng test --code-coverage --watch=false --karma-config karma.conf.ci.js

With the karma.conf.ci.js:

module.exports = function (config) {
  const puppeteer = require('puppeteer');
  process.env.CHROME_BIN = puppeteer.executablePath();

  let testResultFolder = require('path').join(__dirname, '../../../angular-test');
  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'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-junit-reporter'),
    ],
    client: {
      jasmine: {
      },
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true, // removes the duplicated traces
    },
    coverageReporter: {
      dir: require('path').join(testResultFolder, './coverage'),
      subdir: '.',
      reporters: [{ type: 'html' }, { type: 'text-summary' }, { type: 'cobertura' }],
    },
    junitReporter: {
      outputDir: require('path').join(testResultFolder, './results'),
    },
    reporters: ['progress', 'kjhtml', 'junit'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadless'],
    singleRun: true,
    restartOnFileChange: true,
  });
};

I would like to exclude some kind of classes from the code coverage, to only watch the code that we want to test. Typically, we would like to exclude all the Modules(*.module.ts) because there is no logic in this.

The goal is to enforce an high % of code coverage in the PR, not inlfuenced by code we don't want to test.

J4N
  • 19,480
  • 39
  • 187
  • 340
  • I think you can do it like so: https://stackoverflow.com/a/50323088/7365461 and for you it would be maybe `"codeCoverageExclude": ["src/**/*.module.ts"],` – AliF50 May 10 '22 at 12:43
  • yeah, I found out something simlar 2 hours ago, I ended by setting the exclusion as "**/*.module.ts" since I've module on different levels. Thanks for your answer! Regarding the % of code coverage required to pass, I saw that azure provide an `azurepipelines-coverage.yml` – J4N May 10 '22 at 12:47
  • I would do this for enforcement: https://angular.io/guide/testing-code-coverage#code-coverage-enforcement. It will basically fail the unit test stage if the thresholds are not met. – AliF50 May 10 '22 at 12:52
  • @AliF50 I'm not convinced, that will prevent the publish of the test results on azure. If the code coverage is published on azure, you can browse exactly what was the code coverage for each class and check what was not covered. It also allow the reviewer to check how new code has been covered. Also, using angular would not allow you to know what % of the changed code is covered I guess? – J4N May 10 '22 at 14:55
  • You're correct, maybe azurepipelines-coverage.yml has some features that I am not aware of. Basically, if the task is to fail the pipeline (CI/CD) if coverage thresholds of the whole codebase are not met, the document I linked is the way I would do it. Even if all the tests pass but the thresholds do not pass, the test will respond with a failure (fail status). – AliF50 May 10 '22 at 15:15

0 Answers0