fix(@angular/build): warn when TestBed.overrideComponent is used in AOT mode#33542
fix(@angular/build): warn when TestBed.overrideComponent is used in AOT mode#33542clydin wants to merge 1 commit into
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a warning mechanism in both Karma and Vitest builders to alert developers when 'TestBed.overrideComponent' is used in AOT mode, as this is not fully supported and can lead to resolution errors. I have reviewed the implementation and suggest adding guard clauses to handle potential null or undefined component arguments, preventing TypeErrors in the override logic.
| `getTestBed().overrideComponent = function (component, override) {`, | ||
| ` const isAotComponent = !!component.ɵcmp;`, |
There was a problem hiding this comment.
If component is null or undefined, accessing component.ɵcmp or component.name will throw a TypeError before the original overrideComponent method can be called. Adding a guard clause to check if component is truthy ensures defensive programming and delegates invalid arguments safely to the original method.
| `getTestBed().overrideComponent = function (component, override) {`, | |
| ` const isAotComponent = !!component.ɵcmp;`, | |
| `getTestBed().overrideComponent = function (component, override) {`, | |
| ` if (!component) {`, | |
| ` return originalOverrideComponent.call(this, component, override);`, | |
| ` }`, | |
| ` const isAotComponent = !!component.ɵcmp;`, |
| getTestBed().overrideComponent = function (component, override) { | ||
| const isAotComponent = !!component.ɵcmp; |
There was a problem hiding this comment.
If component is null or undefined, accessing component.ɵcmp or component.name will throw a TypeError before the original overrideComponent method can be called. Adding a guard clause to check if component is truthy ensures defensive programming and delegates invalid arguments safely to the original method.
| getTestBed().overrideComponent = function (component, override) { | |
| const isAotComponent = !!component.ɵcmp; | |
| getTestBed().overrideComponent = function (component, override) { | |
| if (!component) { | |
| return originalOverrideComponent.call(this, component, override); | |
| } | |
| const isAotComponent = !!component.ɵcmp; |
…OT mode TestBed.overrideComponent is a JIT-first API that relies on runtime decorator metadata to dynamically recompile components. When tests are compiled in AOT mode (which is the default for vitest and some karma configurations), decorator metadata is compiled away, meaning template or imports overrides can fail silently or cause confusing NG0304/NG0303 errors. To improve developer experience, this adds a runtime warning when TestBed.overrideComponent is called on an AOT-compiled component in AOT mode. A warning is printed to the console suggesting to disable AOT (aot: false) in the test build configuration as a workaround.
d07894c to
40adb11
Compare
|
Upstreaming into TestBed directly here: angular/angular#69730 |
TestBed.overrideComponent is a JIT-first API that relies on runtime decorator metadata to dynamically recompile components. When tests are compiled in AOT mode (which is the default for vitest and some karma configurations), decorator metadata is compiled away, meaning template or imports overrides can fail silently or cause confusing NG0304/NG0303 errors.
To improve developer experience, this adds a runtime warning when TestBed.overrideComponent is called on an AOT-compiled component in AOT mode. A warning is printed to the console suggesting to disable AOT (aot: false) in the test build configuration as a workaround.