angular / angular/angular-cli

Angular 21 new unit test builder with Karma and zone.js

未關閉
#32,905 3 則留言 4 個 reaction 已指派 0 人 在 GitHub 檢視
area: @angular/cli gemini-triaged
主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
14 小時 23 分鐘
30 天內合併 PR
162

描述

### Command

test

### Is this a regression?

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

### The previous version in which this bug was not present was

20

### Description

After upgrading from Angular 20 to 21, I ran into NGXXXX errors when running unit tests still with Karma and zone.js.
I found a [closed issue](https://github.com/angular/angular-cli/issues/32047) and a supposed fix for it, but that did not resolve the issue.
Others experienced the same and implemented workarounds which actually do work: [StackOverflow](https://stackoverflow.com/a/79837997), [Reddit](https://www.reddit.com/r/angular/comments/1p8suok/upgrading_from_angular_20_21_app_works_but_tests/).

I found that the solution could be that the `angular.json` has to be modified to be in line with the new project template like this:
- change `"builder": "@angular/build:karma`" to `"builder": "@angular/build:unit-test"`
- add `"runner": "karma"`
- remove the rest of the properties (according to [documentation](https://angular.dev/guide/testing/migrating-to-vitest#:~:text=does%20not%20support%20this) these derive from the development configuration)

I did not find anything regarding the necessity of this change in the documentation, but if that is the case, it would be wise to mention it and maybe even add a migration schematic.

Also its not clear what is the alternative for `"main": "src/test.ts"` which contains global test setup. My guess is that the `"setupFiles"` property would be the successor (like `"karmaConfig"` became `"runnerConfig"`), but that is [disabled for Karma](https://github.com/angular/angular-cli/blob/v21.2.5/packages/angular/build/src/builders/unit-test/runners/karma/executor.ts#L40).

So my question is: Where should I put global test setup with the new `"unit-test"` builder when using Karma?

Thanks in advance.

### Minimal Reproduction

npm test

### Exception or Error

```text

```

### Your Environment

```text
Angular CLI : 21.2.5
Node.js : 24.14.0
Package Manager : npm 11.9.0
Operating System : win32 x64
```

### Anything else relevant?

_No response_

貢獻指南

開啟貢獻指南

研究方向

從 packages/angular/build/src/builders/unit-test/runners/karma/executor.ts 開始,比較 angular.json 中 Angular 20 和 21 的 unit-test builder 設定。閱讀報告中連結的測試遷移文件,並追蹤 Karma 如何處理全域測試設定。當預期的 Karma 設定建立完成,且文件或遷移行為涵蓋所回報的升級路徑時,即視為完成。

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

評估

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

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

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