angular / angular/angular-cli

Using overrideComponent breaks code coverage when testing with aot: true

未關閉
#30,127 9 則留言 12 個 reaction 已指派 0 人 在 GitHub 檢視
area: @angular/build devkit/build-angular:karma freq1: low severity3: broken type: bug/fix
主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
14 小時 23 分鐘
30 天內合併 PR
162

描述

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

貢獻指南

開啟貢獻指南

研究方向

使用 angular.json、app.spec.ts、TestBed.overrideComponent 以及提供的 ng test --no-watch --code-coverage 指令,在產生的應用程式中重現問題。比較 override 前後的涵蓋率,然後追蹤 Angular CLI 測試和 AOT 涵蓋率的進入點。當 override 不再導致元件範本涵蓋率不準確時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
angular, typescript
領域
build-system, testing
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
活躍
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。