Skip to content

fix(core): warn when overrideComponent is called with layout/imports/schema overrides in AOT mode#69730

Open
clydin wants to merge 1 commit into
angular:mainfrom
clydin:testing-warn-aot-override
Open

fix(core): warn when overrideComponent is called with layout/imports/schema overrides in AOT mode#69730
clydin wants to merge 1 commit into
angular:mainfrom
clydin:testing-warn-aot-override

Conversation

@clydin

@clydin clydin commented Jul 10, 2026

Copy link
Copy Markdown
Member

TestBed.overrideComponent relies on runtime decorator metadata to dynamically recompile components. When tests are compiled in AOT mode, 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 with template, import, schema, or layout overrides in AOT mode. A warning is printed to the console suggesting to disable AOT (aot: false) in the test build configuration as a workaround.

Overriding provider/viewProvider metadata remains fully supported without warnings.

@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Jul 10, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jul 10, 2026
@clydin clydin force-pushed the testing-warn-aot-override branch 2 times, most recently from 476ced6 to 6800b62 Compare July 10, 2026 12:46
@clydin clydin added the target: patch This PR is targeted for the next patch release label Jul 10, 2026
@clydin clydin requested a review from dgp1130 July 10, 2026 13:41
@clydin clydin marked this pull request as ready for review July 10, 2026 13:41
@pullapprove pullapprove Bot requested a review from JeanMeche July 10, 2026 13:41
);
});

it('should NOT warn when overrideComponent is called on an AOT component with safe overrides (like providers)', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider: Should we also test not warning on a JIT component?

console.warn(
`[Angular] WARNING: 'TestBed.overrideComponent' was called on '${component.name}' with template/import/schema overrides in AOT mode. ` +
`This is not fully supported and may cause NG0304/NG0303 element resolution errors. \n` +
`👉 Workaround: Disable AOT compilation in the build configuration used for tests (e.g. set 'aot: false' or equivalent).`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider: Should we point out @Component({jit: true}) as an option? You probably don't want that for production components, but might be useful for test components without opting-out the whole test suite into JIT? Maybe this is too easy to misuse to be worth calling out, but figured I'd mention it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps we could also create an entry in the documentation for this case?

…schema overrides in AOT mode

TestBed.overrideComponent relies on runtime decorator metadata to dynamically recompile components. When tests are compiled in AOT mode, 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 with template, import, schema, or layout overrides in AOT mode. A warning is printed to the console suggesting to disable AOT (aot: false) in the test build configuration as a workaround.

Overriding provider/viewProvider metadata remains fully supported without warnings.
@clydin clydin force-pushed the testing-warn-aot-override branch from 6800b62 to 0de2838 Compare July 10, 2026 17:34
@JoostK

JoostK commented Jul 12, 2026

Copy link
Copy Markdown
Member

Unless ngJitMode: false is set, AOT-compiled components do support TestBed override interop as the original metadata is stored for ad-hoc JIT compilation; its presence should probably not trigger this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants