angular / angular/angular-cli

Using overrideComponent breaks code coverage when testing with aot: true

Aperta
#30,127 9 commenti 12 reazioni 0 assegnatari Vedi su GitHub
area: @angular/build devkit/build-angular:karma freq1: low severity3: broken type: bug/fix
Lingua principale
TypeScript
Stelle
27k
Fork
11.8k
Merge medio
14h 23m
PR unite (30g)
162

Descrizione

### 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

When using `TestBed.overrideComponent` to stub a child component, the test code coverage is no longer accurate

### Minimal Reproduction

Generate a new app with latest CLI and add a component:

```
npx @angular/cli@20.0.0-next.6 new aot-coverage --defaults --no-routing
cd aot-coverage
ng g c user
```

Use app-user in app.html:
```html

Hello, aot-coverage

```

Edit angular.json to add "aot": true in the test section and run:
```
ng test --no-watch --code-coverage
```

Coverage is 100%
```
Statements : 100% ( 12/12 )
Branches : 100% ( 11/11 )
Functions : 100% ( 3/3 )
Lines : 100% ( 6/6 )
```

Now, in the app.spec.ts, add in `beforeEach`:

```ts
TestBed.overrideComponent(App, {
remove: {
imports: [User],
},
add: {
imports: [User],
}
});
```

(this happens with a proper stub of course but even removing the import and adding it back works to reproduce the issue)

Run the tests again:

```
ng test --no-watch --code-coverage
```

Coverage is now inaccurate because the template is not properly covered:
```
Statements : 58.33% ( 7/12 )
Branches : 90.9% ( 10/11 )
Functions : 66.66% ( 2/3 )
Lines : 66.66% ( 4/6 )
```

### Exception or Error

```text

```

### Your Environment

```text
Angular CLI: 20.0.0-next.6
Node: 22.14.0
Package Manager: npm 10.9.0
OS: darwin arm64

Angular: 20.0.0-next.7
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package Version
------------------------------------------------------
@angular-devkit/architect 0.2000.0-next.6
@angular-devkit/core 20.0.0-next.6
@angular-devkit/schematics 20.0.0-next.6
@angular/build 20.0.0-next.6
@angular/cli 20.0.0-next.6
@schematics/angular 20.0.0-next.6
rxjs 7.8.2
typescript 5.8.3
zone.js 0.15.0
```

### Anything else relevant?

_No response_

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Riproduci il problema nell’app generata usando angular.json, app.spec.ts, TestBed.overrideComponent e i comandi forniti ng test --no-watch --code-coverage. Confronta la coverage prima e dopo l’override, quindi traccia i punti di ingresso dei test di Angular CLI e della coverage AOT. Il lavoro è completato quando l’override non rende più inaccurata la coverage del template del componente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
angular, typescript
Ambito
build-system, testing
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.