-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@schematics/angular): add refactor-fake-async migration
#32784
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
1
commit 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
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,10 @@ | |
| */ | ||
|
|
||
| import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
| import { transformFakeAsyncFlush } from './transformers/fake-async-flush'; | ||
| import { transformFakeAsyncFlushMicrotasks } from './transformers/fake-async-flush-microtasks'; | ||
| import { transformFakeAsyncTest } from './transformers/fake-async-test'; | ||
| import { transformFakeAsyncTick } from './transformers/fake-async-tick'; | ||
| import { | ||
| transformDoneCallback, | ||
| transformFocusedAndSkippedTests, | ||
|
|
@@ -46,7 +50,11 @@ import { | |
| transformSpyReset, | ||
| } from './transformers/jasmine-spy'; | ||
| import { transformJasmineTypes } from './transformers/jasmine-type'; | ||
| import { addVitestValueImport, getVitestAutoImports } from './utils/ast-helpers'; | ||
| import { | ||
| addVitestValueImport, | ||
| getVitestAutoImports, | ||
| removeImportSpecifiers, | ||
| } from './utils/ast-helpers'; | ||
| import { RefactorContext } from './utils/refactor-context'; | ||
| import { RefactorReporter } from './utils/refactor-reporter'; | ||
|
|
||
|
|
@@ -121,6 +129,10 @@ const callExpressionTransformers = [ | |
| transformSpyCallInspection, | ||
| transformtoHaveBeenCalledBefore, | ||
| transformToHaveClass, | ||
| transformFakeAsyncTest, | ||
| transformFakeAsyncTick, | ||
| transformFakeAsyncFlush, | ||
| transformFakeAsyncFlushMicrotasks, | ||
|
|
||
| // **Stage 3: Global Functions & Cleanup** | ||
| // These handle global Jasmine functions and catch-alls for unsupported APIs. | ||
|
|
@@ -179,6 +191,7 @@ export function transformJasmineToVitest( | |
|
|
||
| const pendingVitestValueImports = new Set<string>(); | ||
| const pendingVitestTypeImports = new Set<string>(); | ||
| const pendingImportSpecifierRemovals = new Map<string, Set<string>>(); | ||
|
|
||
| const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => { | ||
| const refactorCtx: RefactorContext = { | ||
|
|
@@ -187,6 +200,7 @@ export function transformJasmineToVitest( | |
| tsContext: context, | ||
| pendingVitestValueImports, | ||
| pendingVitestTypeImports, | ||
| pendingImportSpecifierRemovals, | ||
| }; | ||
|
|
||
| const visitor: ts.Visitor = (node) => { | ||
|
|
@@ -240,16 +254,25 @@ export function transformJasmineToVitest( | |
|
|
||
| const hasPendingValueImports = pendingVitestValueImports.size > 0; | ||
| const hasPendingTypeImports = pendingVitestTypeImports.size > 0; | ||
| const hasPendingImportSpecifierRemovals = pendingImportSpecifierRemovals.size > 0; | ||
|
|
||
| if ( | ||
| transformedSourceFile === sourceFile && | ||
| !reporter.hasTodos && | ||
| !hasPendingValueImports && | ||
| !hasPendingTypeImports | ||
| !hasPendingTypeImports && | ||
| !hasPendingImportSpecifierRemovals | ||
| ) { | ||
| return content; | ||
| } | ||
|
|
||
| if (hasPendingImportSpecifierRemovals) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is the way to go but I couldn't find any previous occurrence of something that relies on a formatter or a fixable eslint rule that removes unused imports. |
||
| transformedSourceFile = removeImportSpecifiers( | ||
| transformedSourceFile, | ||
| pendingImportSpecifierRemovals, | ||
| ); | ||
| } | ||
|
|
||
| if (hasPendingTypeImports || (options.addImports && hasPendingValueImports)) { | ||
| const vitestImport = getVitestAutoImports( | ||
| options.addImports ? pendingVitestValueImports : new Set(), | ||
|
|
||
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 '../../../third_party/github.com/Microsoft/TypeScript/lib/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 '../../../third_party/github.com/Microsoft/TypeScript/lib/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 { | ||
| // 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), | ||
| ); | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush_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,108 @@ | ||
| /** | ||
| * @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('transformFakeAsyncFlush', () => { | ||
| const testCases = [ | ||
| { | ||
| description: 'should replace `flush` with `await vi.runAllTimersAsync()`', | ||
| input: ` | ||
| import { flush } from '@angular/core/testing'; | ||
|
|
||
| flush(); | ||
| `, | ||
| expected: `await vi.runAllTimersAsync();`, | ||
| }, | ||
| { | ||
| description: 'should add TODO comment when flush is called with maxTurns', | ||
| input: ` | ||
| import { flush } from '@angular/core/testing'; | ||
|
|
||
| flush(42); | ||
| `, | ||
| expected: ` | ||
| // TODO: vitest-migration: flush(maxTurns) was called but maxTurns parameter is not migrated. Please migrate manually. | ||
| await vi.runAllTimersAsync(); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should add TODO comment when flush return value is used', | ||
| input: ` | ||
| import { flush } from '@angular/core/testing'; | ||
|
|
||
| const turns = flush(); | ||
| `, | ||
| expected: ` | ||
| // TODO: vitest-migration: flush() return value is not migrated. Please migrate manually. | ||
| const turns = await vi.runAllTimersAsync() ?? 0; | ||
atscott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `, | ||
| }, | ||
| { | ||
| description: 'should add TODO comment when flush return value is used in a return statement', | ||
| input: ` | ||
| import { flush } from '@angular/core/testing'; | ||
|
|
||
| async function myFlushWrapper() { | ||
| return flush(); | ||
| } | ||
| `, | ||
| expected: ` | ||
| async function myFlushWrapper() { | ||
| // TODO: vitest-migration: flush() return value is not migrated. Please migrate manually. | ||
| return await vi.runAllTimersAsync() ?? 0; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should not replace `flush` if not imported from `@angular/core/testing`', | ||
| input: ` | ||
| import { flush } from './my-flush'; | ||
|
|
||
| flush(); | ||
| `, | ||
| expected: ` | ||
| import { flush } from './my-flush'; | ||
|
|
||
| flush(); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should keep other imported symbols from `@angular/core/testing`', | ||
| input: ` | ||
| import { TestBed, flush } from '@angular/core/testing'; | ||
|
|
||
| flush(); | ||
| `, | ||
| expected: ` | ||
| import { TestBed } from '@angular/core/testing'; | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should keep imported types from `@angular/core/testing`', | ||
| input: ` | ||
| import { flush, type ComponentFixture } from '@angular/core/testing'; | ||
|
|
||
| flush(); | ||
| `, | ||
| expected: ` | ||
| import { type ComponentFixture } from '@angular/core/testing'; | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
| `, | ||
| }, | ||
| ]; | ||
|
|
||
| testCases.forEach(({ description, input, expected }) => { | ||
| it(description, async () => { | ||
| await expectTransformation(input, expected); | ||
| }); | ||
| }); | ||
| }); | ||
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.