Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): ComponentFixture autodetect should detect changes within A… #54733

Closed
wants to merge 1 commit into from

Conversation

atscott
Copy link
Contributor

@atscott atscott commented Mar 6, 2024

…pplicationRef.tick

The current behavior of autoDetect in ComponentFixture does not match production very well. It has several flaws that make it an insufficient change detection mechanism:

  • It runs change detection for the component under test after views attached to the ApplicationRef. This can cause real behavior differences that break in production, because tests can observe view refreshes in the incorrect order (for example, a dialog refreshing before the component which opened it).
  • Because of the above ordering, render hooks registered during change detection of the fixture views will not execute at all because ApplicationRef.tick already happen.
  • It does not rerun change detection on the view tree if there are more dirty views to refresh after the render hooks complete.
  • It effectively hides/swallows errors from change detection inside the onMicrotaskEmpty subscription by not reporting them to the error handler. Instead, this error ends up being unhandled in the subscription and rxjs throws these in a setTimeout.

All of the above are problematic but this commit does not fix the final point. Ideally, we can land this in a future change but this requires additional internal fixes. In the meantime, we have to juggle special-case handling of the component fixture views within ApplicationRef.tick using some special events to retain current behavior and avoid errors from the fixture propagating to the ErrorHandler.

BREAKING CHANGE: The ComponentFixture.autoDetect feature now executes change detection for the fixture within ApplicationRef.tick. This more closely matches the behavior of how a component would refresh in production. The order of component refresh in tests may be slightly affected as a result, especially when dealing with additional components attached to the application, such as dialogs. Tests sensitive to this type of change (such as screenshot tests) may need to be updated. Concretely, this change means that the component will refresh before additional views attached to ApplicationRef (i.e. dialog components). Prior to this change, the fixture component would refresh after other views attached to the application.

…pplicationRef.tick

The current behavior of `autoDetect` in `ComponentFixture` does not
match production very well. It has several flaws that make it an
insufficient change detection mechanism:

* It runs change detection for the component under test _after_ views
  attached to the `ApplicationRef`. This can cause real behavior
  differences that break in production, because tests can observe view
  refreshes in the incorrect order (for example, a dialog refreshing
  before the component which opened it).
* Because of the above ordering, render hooks registered during change
  detection of the fixture views _will not execute at all_ because
  `ApplicationRef.tick` already happen.
* It does not rerun change detection on the view tree if there are more
  dirty views to refresh after the render hooks complete.
* It effectively hides/swallows errors from change detection inside the
  `onMicrotaskEmpty` subscription by not reporting them to the error
  handler. Instead, this error ends up being unhandled in the
  subscription and rxjs throws these in a `setTimeout`.

All of the above are problematic but this commit _does not_ fix the
final point. Ideally, we can land this in a future change but this
requires additional internal fixes. In the meantime, we have to juggle
special-case handling of the component fixture views within
`ApplicationRef.tick` using some special events to retain current
behavior and avoid errors from the fixture propagating to the `ErrorHandler`.

BREAKING CHANGE: The `ComponentFixture.autoDetect` feature now executes
change detection for the fixture within `ApplicationRef.tick`. This more
closely matches the behavior of how a component would refresh in
production. The order of component refresh in tests may be slightly
affected as a result, especially when dealing with additional components
attached to the application, such as dialogs. Tests sensitive to this
type of change (such as screenshot tests) may need to be updated.
Concretely, this change means that the component will refresh _before_
additional views attached to `ApplicationRef` (i.e. dialog components).
Prior to this change, the fixture component would refresh _after_ other
views attached to the application.
@atscott atscott added area: testing Issues related to Angular testing features, such as TestBed area: core Issues related to the framework runtime target: major This PR is targeted for the next major release labels Mar 6, 2024
@ngbot ngbot bot modified the milestone: Backlog Mar 6, 2024
@angular-robot angular-robot bot added the detected: breaking change PR contains a commit with a breaking change label Mar 6, 2024
@atscott atscott added the action: merge The PR is ready for merge by the caretaker label Mar 6, 2024
@atscott
Copy link
Contributor Author

atscott commented Mar 6, 2024

This PR was merged into the repository by commit 24bc0ed.

@atscott atscott closed this in 24bc0ed Mar 6, 2024
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime area: testing Issues related to Angular testing features, such as TestBed detected: breaking change PR contains a commit with a breaking change target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants