TestBed.overrideComponent doesn't work with AOT and webpack

未关闭
#32,628 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
angular, typescript

调研方向

从最小复现开始:使用 TestBed.overrideComponent 或 TestBed.overrideModule 编写组件测试,使用 Karma、browser mode 下的 webpack builder,并启用 AOT。将该失败与 application builder mode 或禁用 AOT 时的情况进行比较,然后验证在受影响的配置下,override 测试能够运行且不会出现缺少 @Component 元数据的错误。

由索引模型根据 Issue 内容生成。

描述

area: @angular-devkit/build-angular devkit/build-angular:karma freq1: low severity3: broken type: bug/fix
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

TestBed.overrideComponent and TestBed.overrideModule don't seem to work with karma, webpack builder (builderMode: browser) and aot enabled. Changing any of these settings, so builderMode: application or disabling aot "fixes" the problem. The same issue was reported here https://github.com/angular/angular-cli/issues/30017 but i guess it was only fixed for esbuild? Is there any workaround for this? or can it be fixed for angular20?

Minimal Reproduction

Create a simple valid component, turn on AOT for Karma, and make a test where you use TestBed.overrideComponent to reset metadata. You don't even need to specify an actual override in the second parameter to trigger the issue: TestBed.overrideComponent(YourComponent, {}) triggers the failure.

builderMode: browser needs to be set

Exception or Error
Error: App class doesn't have @Component decorator or is missing metadata.
            at invalidTypeError (node_modules/@angular/core/fesm2022/testing.mjs:1661:12)
            at forEach (node_modules/@angular/core/fesm2022/testing.mjs:1084:23)
            at asyncGeneratorStep (node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
            at apply (node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:17:1)
            at _ZoneDelegate.invoke (node_modules/zone.js/fesm2015/zone.js:398:28)
            at ProxyZoneSpec.onInvoke (node_modules/zone.js/fesm2015/zone-testing.js:2132:39)
Your Environment
_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    


Angular CLI: 20.3.18
Node: 22.22.0
Package Manager: npm 10.9.4
OS: win32 x64


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

Package                         Version
---------------------------------------
@angular-devkit/architect       0.2003.18
@angular-devkit/build-angular   20.3.16
@angular-devkit/core            20.3.18
@angular-devkit/schematics      20.3.18
@angular/build                  20.3.18
@angular/cli                    20.3.18
@schematics/angular             20.3.18
rxjs                            7.8.2
typescript                      5.9.3
zone.js                         0.15.1
Anything else relevant?

No response

主要语言
TypeScript
星标
27k
派生
11.8k
平均合并
16 小时 21 分钟
30 天内合并 PR
170

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。