Karma coverage does not fail the build when the coverage requirements are not met

568 views Asked by At

I'm using NX from NRWL with my Angular app and I run the unit tests in my CI using the following command:

nx affected:test --base=main --watch=false --browsers=ChromeHeadlessNoSandbox --codeCoverage --parallel=true --maxParallel=4

when the code coverage requirements are not met the build still passes despite I can clearly see the coverage error in the build log:

07 12 2022 16:06:14.750:ERROR [coverage]: Chrome Headless 106.0.5249.91 (Linux x86_64): Coverage for branches (99.68%) does not meet global threshold (100%)

how can I tell nx to make this command fail as soon as the coverage requirements are not met?

EDIT: even running just one single project's test has the same problem:

nx run one-single-project:test --watch=false --browsers=ChromeHeadlessNoSandbox --codeCoverage=true

If I now echo $? it will be 0 but it should be error instead.

1

There are 1 answers

1
Francesco Borzi On

The problem was unrelated to nx.

I figured out after a couple of hours of debugging that it was about karma-coverage and its config.

In my karma.config.js:

    coverageReporter: {
      dir: join(__dirname, 'coverage'),
      subdir: '.',
      fixWebpackSourcePaths: true,
      reporters: [{ type: 'text-summary'}, { type: 'html' }, { type: 'lcovonly', subdir: './' }],
      check: {
        global: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100,
        },
      },
    },

More specifically, inside reporters I had { type: 'text-summary' } which was missing the file property and for some reasons it failed the coverage check. So the correct way is: { type: 'text-summary', file: 'coverage.txt' }

The full coverageReporter config now looks like this:

    coverageReporter: {
      dir: join(__dirname, 'coverage'),
      subdir: '.',
      fixWebpackSourcePaths: true,
      reporters: [{ type: 'text-summary', file: 'coverage.txt' }, { type: 'html' }, { type: 'lcovonly', subdir: './' }],
      check: {
        global: {
          statements: 100,
          lines: 100,
          branches: 100,
          functions: 100,
        },
      },
    },

I believe this is a bug of karma-coverage and I have submitted a fix for it.