Skip to content

feat(core): introduce TestBed.tick()#60993

Closed
pkozlowski-opensource wants to merge 1 commit intoangular:mainfrom
pkozlowski-opensource:flush_effect_remove
Closed

feat(core): introduce TestBed.tick()#60993
pkozlowski-opensource wants to merge 1 commit intoangular:mainfrom
pkozlowski-opensource:flush_effect_remove

Conversation

@pkozlowski-opensource
Copy link
Copy Markdown
Member

This commit introduces the TestBed.tick() method that, similarly to the ApplicationRef.tick(), synchronizes state with the DOM. It can be used in unit tests to mimic framework's logic executed in production applications. The TestBed.tick() should be used instead of the removed TestBed.flushEffects().

BREAKING CHANGE: the TestBed.flushEffects() was removed - use the TestBed.tick() instead.

@angular-robot angular-robot bot added detected: breaking change PR contains a commit with a breaking change detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Apr 24, 2025
@ngbot ngbot bot added this to the Backlog milestone Apr 24, 2025
@pkozlowski-opensource
Copy link
Copy Markdown
Member Author

This is currently blocked on the pending G3 cleanup

@e-oz
Copy link
Copy Markdown

e-oz commented Apr 24, 2025

Will this work without zone.js?

@jnizet
Copy link
Copy Markdown
Contributor

jnizet commented Apr 24, 2025

Does it synchronize state with the DOM, or does it ask Angular to synchronize state with the DOM if and only if Angular thinks it should do it, the same way as it would decide in production, i.e. if it detects that an event listener has been executed, or that a signal read by a template has changed? Documenting this would be so helpful.

There are so many ways to do similar but slightly different things: fixture.detectChanges(), await fixture.whenStable(), await fixture.whenRenderingDone(), and now TestBed.tick().

@pkozlowski-opensource pkozlowski-opensource added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: blocked labels Apr 25, 2025
@pkozlowski-opensource
Copy link
Copy Markdown
Member Author

G3 cleanup is done, this PR is ready for review.

This commit introduces the `TestBed.tick()` method that,
similarly to the `ApplicationRef.tick()`, synchronizes state
with the DOM. It can be used in unit tests to mimic framework's
logic executed in production applications. The `TestBed.tick()`
should be used instead of the removed `TestBed.flushEffects()`.

BREAKING CHANGE: the `TestBed.flushEffects()` was removed - use
the `TestBed.tick()` instead.
Copy link
Copy Markdown
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

Reviewed-for: public-api

@pkozlowski-opensource
Copy link
Copy Markdown
Member Author

Will this work without zone.js?

@e-oz yes

There are so many ways to do similar but slightly different things: fixture.detectChanges(), await fixture.whenStable(), await fixture.whenRenderingDone(), and now TestBed.tick().

@jnizet agreed. This change is more about getting rid of TestBed.flushEffects() - we still have more to do in terms of streamlining TestBed APIs.

@pkozlowski-opensource
Copy link
Copy Markdown
Member Author

Passing TGP

@pkozlowski-opensource pkozlowski-opensource added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Apr 25, 2025
@jnizet
Copy link
Copy Markdown
Contributor

jnizet commented Apr 25, 2025

Thanks @pkozlowski-opensource . I hope this streamlining comes soon!

@pkozlowski-opensource
Copy link
Copy Markdown
Member Author

caretaker note: I did the G3 cleanup and atm there is no new usage of TestBed.flushEffects() - it might happen, though, that those will show up during the day. So I would suggest:

  • merging and syncing it in isolation
  • in case it does break other people we should fix forward (replace TestBed.flushEffects() with TestBed.tick()

Copy link
Copy Markdown
Contributor

@thePunderWoman thePunderWoman left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

@thePunderWoman thePunderWoman removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 25, 2025
@pkozlowski-opensource pkozlowski-opensource added the target: major This PR is targeted for the next major release label Apr 25, 2025
@crisbeto
Copy link
Copy Markdown
Member

This PR was merged into the repository by commit d5fd734.

The changes were merged into the following branches: main

@crisbeto crisbeto closed this in d5fd734 Apr 25, 2025
@e-oz
Copy link
Copy Markdown

e-oz commented May 2, 2025

Since this tick() is part of TestBed, will it be possible to add millis?: number argument (and related functionality)?

@JeanMeche
Copy link
Copy Markdown
Member

This is not a fakeAsync tick but an ApplicationRef.tick(). They're unreleated.

@e-oz
Copy link
Copy Markdown

e-oz commented May 2, 2025

I understand, but since this one is already part of TestBed and will be only used in tests, it would be very handy to replace current usages of tick() from core/testing with this new TestBed.tick() and also resolve the issue with zone.js dependency.

@JeanMeche
Copy link
Copy Markdown
Member

Those are not interchangeable as they do not have the same purpose.

tick() is stronly tied to fakeAsync.

You alternatives are either native async/await or this like the Jasmine.clock with tick or its new autotick option for example.

@angular-automatic-lock-bot
Copy link
Copy Markdown

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 Jun 2, 2025
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 detected: breaking change PR contains a commit with a breaking change detected: feature PR contains a feature commit merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants