Skip to content

fix(core): preserve change detection strategy when overriding component#69731

Open
bethge wants to merge 1 commit into
angular:mainfrom
bethge:fix-component-override
Open

fix(core): preserve change detection strategy when overriding component#69731
bethge wants to merge 1 commit into
angular:mainfrom
bethge:fix-component-override

Conversation

@bethge

@bethge bethge commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

TestBed.overrideComponent previously caused JIT recompilation to overwrite a component's original ChangeDetectionStrategy. This ensures the component's existing ChangeDetectionStrategy is preserved when metadata overrides are applied.

Disclaimer: Note that the root cause analysis of the bug and the solution were identified with the help of AI. I implemented the change, tests, and ran it against our codebase. The problem is in my opinion very much genuine but a different solution may be more appropriate.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When a component without explicit changeDetection strategy is overridden with TestBed.overrideComponent, the change detection strategy changes from OnPush (default) to Eager. This causes a mismatch between prod and test component behavior.

AI's explanation: When TestBed.overrideComponent is called on a component, the component is added to TestBed's pendingComponents queue, forcing runtime JIT recompilation (compileTypesSync()). During this JIT recompilation, TestBed reconstructs the component metadata. Previously, the metadata decorator resolution defaulted changeDetection to ChangeDetectionStrategy.Eager, overwriting the component's existing (ɵcmp) Change Detection Strategy.
As a result, an overridden component whose pre-compiled definition used ChangeDetectionStrategy.OnPush would unexpectedly run with ChangeDetectionStrategy.Default (Eager) in tests.

Issue Number: N/A

What is the new behavior?

When compiling component overrides in TestBed, the implicit ChangeDetectionStrategy (onPush property on the pre-compiled ɵcmp definition) is preserved.

Does this PR introduce a breaking change?

  • Yes
  • No

Note on potential behavioral impact: While this is a bugfix restoring expected component behavior during testing, it could affect existing tests if consumers unknowingly relied on TestBed.overrideComponent silently reverting an OnPush component to Eager (Default) change detection. No such issue was found across our codebase.

Other information

  1. Revealed during migration: This issue was discovered when removing redundant changeDetection: ChangeDetectionStrategy.OnPush declarations across our codebase. Removing the explicit declaration caused existing tests using TestBed.overrideComponent to fail, revealing that the JIT override path was silently altering the component's runtime change detection behavior.
  2. Why eager by default: I don't know if the Component decorator definition was left as Eager after the OnPush-as-default migration on purpose or accidentally.

TestBed.overrideComponent previously caused JIT recompilation to overwrite a component's original ChangeDetectionStrategy. This ensures the component's existing ChangeDetectionStrategy is preserved when metadata overrides are applied.
@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
@bethge bethge marked this pull request as ready for review July 10, 2026 15:03
@pullapprove pullapprove Bot requested a review from crisbeto July 10, 2026 15:04
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant