feat(@schematics/angular): add option to migrate fakeAsync to Vitest fake timers#33111
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for migrating Angular fakeAsync tests to Vitest by introducing a new fakeAsync option and corresponding transformers for tick, flush, and flushMicrotasks. The implementation converts these utilities to Vitest's vi fake timer API. Reviewers suggested scoping the fake timer hooks more narrowly to avoid side effects on sibling tests, disabling shouldAdvanceTime to better match fakeAsync semantics, and refining the flush transformer's logic for detecting discarded return values.
e90eef2 to
fbd8e15
Compare
cc415cf to
24627cd
Compare
| `, | ||
| }, | ||
| { | ||
| description: 'should not replace `fakeAsync` if not used within a describe block', |
There was a problem hiding this comment.
This exact scenario is not likely within Jasmine but fakeAsync could be used in some test factory or something similar.
In that case, we just bail.
| // > beforeAll(() => { | ||
| // > vi.useFakeTimers({ | ||
| // > advanceTimeDelta: 1, | ||
| // > shouldAdvanceTime: true |
There was a problem hiding this comment.
Probably the best alternative to use fake timers without freezing other tests in the suite which rely on rely timers.
fakeAsync to Vitest fake timers
There was a problem hiding this comment.
Code Review
This pull request introduces the fakeAsync transformation to the jasmine-vitest schematic, enabling the migration of Angular fakeAsync tests to Vitest's fake timers. It adds specific transformers for flush, flushMicrotasks, tick, and the fakeAsync wrapper, supported by new utility functions for AST manipulation. Review feedback highlights several areas for improvement: ensuring tick() arguments are preserved even when they are not numeric literals, maintaining parameters and type parameters when converting callbacks to async functions, adjusting Vitest timer settings to better match standard fakeAsync behavior, and correcting misleading comments regarding vi.useRealTimers().
874cbe9 to
1ed014b
Compare
271377b to
978acd4
Compare
|
This PR was merged into the repository. The changes were merged into the following branches:
|
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
There is no migration from
fakeAsyncto Vitest fake timer APIs.Issue Number: N/A
What is the new behavior?
The current migration transforms the following test:
into:
Does this PR introduce a breaking change?
Open Questions
This migration could be part of the jasmine-vitest migration. Should I moved it there and add an opt-out?
Moved to jasmine-vitest migration under
fakeAsyncoption which is false by defaultflushMicrotaskscan only be partially reproduced (i.e. explicitqueueMicrotaskcalls), transforming it have high chances of producing a broken test. Should we just skip transforming tests that use it?Migrated to
await vi.advanceTimersByTimeAsync(0);.Should we generate code that measures time "manually" when the return value of
flushis used?Generates a TODO.
Should we generate an "ad-hoc" function in the test file that reproduces
flush'smaxTurnsoption?Generates a TODO.
What should we do about
processNewMacroTasksSynchronouslyoption?Ignored.
This only migrates
flushandtickif used insidefakeAsyncbut it's probably better to just replace in the whole file as users might create some wrappers. What do you think?Replaced everywhere.