fix(core): preserve change detection strategy when overriding component#69731
Open
bethge wants to merge 1 commit into
Open
fix(core): preserve change detection strategy when overriding component#69731bethge wants to merge 1 commit into
bethge wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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?
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.overrideComponentis called on a component, the component is added toTestBed'spendingComponentsqueue, forcing runtime JIT recompilation (compileTypesSync()). During this JIT recompilation,TestBedreconstructs the component metadata. Previously, the metadata decorator resolution defaultedchangeDetectiontoChangeDetectionStrategy.Eager, overwriting the component's existing (ɵcmp) Change Detection Strategy.As a result, an overridden component whose pre-compiled definition used
ChangeDetectionStrategy.OnPushwould unexpectedly run withChangeDetectionStrategy.Default(Eager) in tests.Issue Number: N/A
What is the new behavior?
When compiling component overrides in
TestBed, the implicitChangeDetectionStrategy(onPushproperty on the pre-compiledɵcmpdefinition) is preserved.Does this PR introduce a breaking change?
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.overrideComponentsilently reverting anOnPushcomponent toEager(Default) change detection. No such issue was found across our codebase.Other information
changeDetection: ChangeDetectionStrategy.OnPushdeclarations across our codebase. Removing the explicit declaration caused existing tests usingTestBed.overrideComponentto fail, revealing that the JIT override path was silently altering the component's runtime change detection behavior.Eagerafter the OnPush-as-default migration on purpose or accidentally.