Skip to content

fix(@angular/build): warn when TestBed.overrideComponent is used in AOT mode#33542

Closed
clydin wants to merge 1 commit into
angular:mainfrom
clydin:fix-aot-override-warning
Closed

fix(@angular/build): warn when TestBed.overrideComponent is used in AOT mode#33542
clydin wants to merge 1 commit into
angular:mainfrom
clydin:fix-aot-override-warning

Conversation

@clydin

@clydin clydin commented Jul 9, 2026

Copy link
Copy Markdown
Member

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.

@clydin clydin added the target: patch This PR is targeted for the next patch release label Jul 9, 2026
@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Jul 10, 2026
@alan-agius4

Copy link
Copy Markdown
Collaborator

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +231 to +232
`getTestBed().overrideComponent = function (component, override) {`,
` const isAotComponent = !!component.ɵcmp;`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
`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;`,

Comment on lines +137 to +138
getTestBed().overrideComponent = function (component, override) {
const isAotComponent = !!component.ɵcmp;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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.
@clydin clydin force-pushed the fix-aot-override-warning branch from d07894c to 40adb11 Compare July 10, 2026 12:21
@clydin

clydin commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

Upstreaming into TestBed directly here: angular/angular#69730

@clydin clydin closed this Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants