angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

@angular/build:karma ignores overrides in karma.conf.js

Open fynnfeldpausch opened this issue 5 months ago • 3 comments

Command

test

Is this a regression?

  • [ ] Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Using the new @angular/build:karma builder, coverage overrides defined in the karma.conf.js are ignored.

Minimal Reproduction

  • clone https://github.com/fynnfeldpausch/karma-coverage
  • take a look at app.ts - there is an additional method
  • take a look at karma.conf.js and the defined coverage
    • global coverage is set to 80%
    • local coverage is set to 0% for app.ts
  • run ng test -- --no-watch --no-progress --browsers=ChromeHeadless --code-coverage

Exception or Error

Test run fails with coverage errors:


19 08 2025 15:57:46.079:ERROR [coverage]: Chrome Headless 139.0.0.0 (Mac OS 10.15.7): Coverage for statements (75%) does not meet per-file (/repro-app/src/app/app.ts)  threshold (80%)
19 08 2025 15:57:46.079:ERROR [coverage]: Chrome Headless 139.0.0.0 (Mac OS 10.15.7): Coverage for lines (66.66%) does not meet per-file (/repro-app/src/app/app.ts)  threshold (80%)
19 08 2025 15:57:46.079:ERROR [coverage]: Chrome Headless 139.0.0.0 (Mac OS 10.15.7): Coverage for functions (0%) does not meet per-file (/repro-app/src/app/app.ts)  threshold (80%)

Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 20.1.6
Node: 24.2.0
Package Manager: npm 11.3.0
OS: darwin arm64

Angular: 20.1.7
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2001.6
@angular-devkit/core         20.1.6
@angular-devkit/schematics   20.1.6
@angular/build               20.1.6
@angular/cli                 20.1.6
@schematics/angular          20.1.6
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else relevant?

No response

fynnfeldpausch avatar Aug 19 '25 14:08 fynnfeldpausch

The karmaConfig option is not configured in the reproduction provided.

https://github.com/fynnfeldpausch/karma-coverage/blob/523b8e0199e6b861a69885edb5842ff2d68f94e1/angular.json#L77

alan-agius4 avatar Aug 19 '25 14:08 alan-agius4

@alan-agius4 Sorry for that, I missed to push the changes to the branch. It should now be there. Please reopen the issue.

fynnfeldpausch avatar Aug 19 '25 14:08 fynnfeldpausch

From a quick investigation:

  1. overrides should be nested under each in karma.conf.js. Afaict the current config shouldn't have worked in other setups either.
  2. After fixing the config, it looks like the config basePath is the issue. It's trying to match path relative to the temporary directory where it builds the test.
  key: '/private/tmp/coverage-override/karma-coverage/src/app/app.ts',
  basePath: '/private/tmp/coverage-override/karma-coverage/dist/test-out/eb35e755-a642-41aa-8352-b172486ebaba',
  normalized: '../../../src/app/app.ts',
  pattern: 'src/app/app.ts',

To verify, I tried and the following currently works (but shouldn't):

      check: {
        each: {
          statements: 80,
          branches: 80,
          functions: 80,
          lines: 80,
          overrides: {
            '../../../src/app/app.ts': {
              statements: 0,
              branches: 0,
              functions: 0,
              lines: 0,
            },
          },
        },
      },

hybrist avatar Aug 19 '25 16:08 hybrist