-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@schematics/angular): add option to migrate fakeAsync to Vitest fake timers
#33111
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
Open
yjaaidi
wants to merge
5
commits into
angular:main
Choose a base branch
from
yjaaidi:feat/add-refactor-fake-async-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f9ffa24
feat(@schematics/angular): migrate fake async to Vitest fake timers
yjaaidi ed56b64
fixup! feat(@schematics/angular): migrate fake async to Vitest fake t…
yjaaidi f2270b5
fixup! feat(@schematics/angular): migrate fake async to Vitest fake t…
yjaaidi d417fa0
fixup! feat(@schematics/angular): migrate fake async to Vitest fake t…
yjaaidi 1ed014b
fixup! feat(@schematics/angular): migrate fake async to Vitest fake t…
yjaaidi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
...es/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush-microtasks.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import ts from 'typescript'; | ||
| import { isNamedImportFrom } from '../utils/ast-helpers'; | ||
| import { ANGULAR_CORE_TESTING } from '../utils/constants'; | ||
| import { RefactorContext } from '../utils/refactor-context'; | ||
| import { addImportSpecifierRemoval, createViCallExpression } from '../utils/refactor-helpers'; | ||
|
|
||
| export function transformFakeAsyncFlushMicrotasks(node: ts.Node, ctx: RefactorContext): ts.Node { | ||
| if ( | ||
| !( | ||
| ts.isCallExpression(node) && | ||
| ts.isIdentifier(node.expression) && | ||
| node.expression.text === 'flushMicrotasks' && | ||
| isNamedImportFrom(ctx.sourceFile, 'flushMicrotasks', ANGULAR_CORE_TESTING) | ||
| ) | ||
| ) { | ||
| return node; | ||
| } | ||
|
|
||
| ctx.reporter.reportTransformation( | ||
| ctx.sourceFile, | ||
| node, | ||
| `Transformed \`flushMicrotasks\` to \`await vi.advanceTimersByTimeAsync(0)\`.`, | ||
| ); | ||
|
|
||
| addImportSpecifierRemoval(ctx, 'flushMicrotasks', ANGULAR_CORE_TESTING); | ||
|
|
||
| return ts.factory.createAwaitExpression( | ||
| createViCallExpression(ctx, 'advanceTimersByTimeAsync', [ts.factory.createNumericLiteral(0)]), | ||
| ); | ||
| } |
43 changes: 43 additions & 0 deletions
43
...hematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush-microtasks_spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { expectTransformation } from '../test-helpers'; | ||
|
|
||
| describe('transformFakeAsyncFlushMicrotasks', () => { | ||
| const testCases = [ | ||
| { | ||
| description: 'should replace `flushMicrotasks` with `await vi.advanceTimersByTimeAsync(0)`', | ||
| input: ` | ||
| import { flushMicrotasks } from '@angular/core/testing'; | ||
|
|
||
| flushMicrotasks(); | ||
| `, | ||
| expected: `await vi.advanceTimersByTimeAsync(0);`, | ||
| }, | ||
| { | ||
| description: | ||
| 'should not replace `flushMicrotasks` if not imported from `@angular/core/testing`', | ||
| input: ` | ||
| import { flushMicrotasks } from './my-flush-microtasks'; | ||
|
|
||
| flushMicrotasks(); | ||
| `, | ||
| expected: ` | ||
| import { flushMicrotasks } from './my-flush-microtasks'; | ||
|
|
||
| flushMicrotasks(); | ||
| `, | ||
| }, | ||
| ]; | ||
|
|
||
| testCases.forEach(({ description, input, expected }) => { | ||
| it(description, async () => { | ||
| await expectTransformation(input, expected); | ||
| }); | ||
| }); | ||
| }); |
60 changes: 60 additions & 0 deletions
60
packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import ts from 'typescript'; | ||
| import { isNamedImportFrom } from '../utils/ast-helpers'; | ||
| import { addTodoComment } from '../utils/comment-helpers'; | ||
| import { ANGULAR_CORE_TESTING } from '../utils/constants'; | ||
| import { RefactorContext } from '../utils/refactor-context'; | ||
| import { addImportSpecifierRemoval, createViCallExpression } from '../utils/refactor-helpers'; | ||
|
|
||
| export function transformFakeAsyncFlush(node: ts.Node, ctx: RefactorContext): ts.Node { | ||
| if ( | ||
| !( | ||
| ts.isCallExpression(node) && | ||
| ts.isIdentifier(node.expression) && | ||
| node.expression.text === 'flush' && | ||
| isNamedImportFrom(ctx.sourceFile, 'flush', ANGULAR_CORE_TESTING) | ||
| ) | ||
| ) { | ||
| return node; | ||
| } | ||
|
|
||
| ctx.reporter.reportTransformation( | ||
| ctx.sourceFile, | ||
| node, | ||
| `Transformed \`flush\` to \`await vi.runAllTimersAsync()\`.`, | ||
| ); | ||
|
|
||
| addImportSpecifierRemoval(ctx, 'flush', ANGULAR_CORE_TESTING); | ||
|
|
||
| if (node.arguments.length > 0) { | ||
| ctx.reporter.recordTodo('flush-max-turns', ctx.sourceFile, node); | ||
| addTodoComment(node, 'flush-max-turns'); | ||
| } | ||
|
|
||
| const awaitRunAllTimersAsync = ts.factory.createAwaitExpression( | ||
| createViCallExpression(ctx, 'runAllTimersAsync'), | ||
| ); | ||
|
|
||
| if (ts.isExpressionStatement(node.parent)) { | ||
| return awaitRunAllTimersAsync; | ||
| } else { | ||
|
yjaaidi marked this conversation as resolved.
|
||
| // If `flush` is not used as its own statement, then the return value is probably used. | ||
| // Therefore, we replace it with nullish coalescing that returns 0: | ||
| // > await vi.runAllTimersAsync() ?? 0; | ||
| ctx.reporter.recordTodo('flush-return-value', ctx.sourceFile, node); | ||
| addTodoComment(node, 'flush-return-value'); | ||
|
|
||
| return ts.factory.createBinaryExpression( | ||
| awaitRunAllTimersAsync, | ||
| ts.SyntaxKind.QuestionQuestionToken, | ||
| ts.factory.createNumericLiteral(0), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.