Skip to content

feat(@schematics/angular): migrate fakeAsync's flush behavior when used in beforeEach#33135

Merged
clydin merged 1 commit intoangular:mainfrom
yjaaidi:feat/migrate-fake-async-flush-behavior
May 6, 2026
Merged

feat(@schematics/angular): migrate fakeAsync's flush behavior when used in beforeEach#33135
clydin merged 1 commit intoangular:mainfrom
yjaaidi:feat/migrate-fake-async-flush-behavior

Conversation

@yjaaidi
Copy link
Copy Markdown
Contributor

@yjaaidi yjaaidi commented May 6, 2026

PR Checklist

Please check to confirm 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
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: @schematics/angular labels May 6, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the fakeAsync transformer to mimic Angular's flush behavior in beforeEach hooks by appending await vi.runOnlyPendingTimersAsync(). It also adds logic to detect and respect the { flush: false } option. Feedback was provided to improve the robustness of the AST parsing by avoiding getText() when checking for the flush property, suggesting the use of the text property and ts.SyntaxKind instead.

},
{
description:
'should transform fakeAsync test to `vi.useFakeTimers()` in `beforeEach`, `afterEach`, `beforeAll`, `afterAll`',
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I split the different hooks in different tests as beforeEach diverges from others.

let count = 0;
beforeEach(async () => {
setTimeout(() => ++count, 100);
await vi.runOnlyPendingTimersAsync();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is what mimics best fakeAsync's flush option behavior.
I've only enabled it for beforeEach as it's the hook that is most likely to break tests if timers are not flushed.
I've decided not to apply it to tests and other hooks to reduce code pollution. On the other hand, it is also possible that some afterEach hooks make assertions that assume timers are flushed in tests.

While writing this, I realize that it would be more consistent to flush pending timers everywhere and not just in beforeEach hook.
@atscott @clydin what do you think?

},
{
description:
'should not append `vi.runOnlyPendingTimersAsync()` if `flush` option is set to false',
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To keep the migration simple, we only detect object expression with false as a literal.

@clydin clydin requested review from atscott May 6, 2026 14:46
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label May 6, 2026
@clydin clydin removed the request for review from atscott May 6, 2026 19:29
@clydin clydin added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 6, 2026
@clydin clydin merged commit d2aa9ed into angular:main May 6, 2026
39 checks passed
@clydin
Copy link
Copy Markdown
Member

clydin commented May 6, 2026

This PR was merged into the repository. The changes were merged into the following branches:

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

Labels

action: merge The PR is ready for merge by the caretaker area: @schematics/angular detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants