diff --git a/packages/schematics/angular/refactor/jasmine-vitest/index_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/index_spec.ts index 2581d8bbacc9..5135c5e294be 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/index_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/index_spec.ts @@ -62,6 +62,34 @@ describe('Jasmine to Vitest Schematic', () => { expect(newContent).toContain(`vi.spyOn(service, 'myMethod').mockReturnValue(undefined);`); }); + it('should report a nested done callback for manual migration', async () => { + const specFilePath = 'projects/bar/src/app/app.spec.ts'; + appTree.overwrite( + specFilePath, + ` + it('waits for a value', (done) => { + values.subscribe({ error: (error) => { done.fail(error); } }); + ready.then(done); + }); + `, + ); + + const logs: string[] = []; + schematicRunner.logger.subscribe((entry) => logs.push(entry.message)); + const tree = await schematicRunner.runSchematic( + 'refactor-jasmine-vitest', + { project: 'bar', verbose: true }, + appTree, + ); + + const content = tree.readContent(specFilePath); + expect(content).toContain('Please migrate manually.'); + expect(content).toContain('done.fail(error)'); + expect(content).toContain('ready.then(done)'); + expect(logs).toContain(' - 1x unhandled-done-usage'); + expect(logs.join('\n')).not.toContain('Transformed promise'); + }); + it('should only transform files matching the fileSuffix option', async () => { const specFilePath = 'projects/bar/src/app/app.spec.ts'; const specFileContent = ` diff --git a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts index abaf1de4456b..d84d52c45f88 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts @@ -46,11 +46,13 @@ describe('Jasmine to Vitest Transformer - Nested Transformations', () => { }); `, expected: ` - it('should handle timers and async', async () => { + // TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually. + it('should handle timers and async', (done) => { vi.useFakeTimers(); setTimeout(() => { expect(true).toBe(true); vi.useRealTimers(); + done(); }, 100); vi.advanceTimersByTime(100); }); @@ -175,10 +177,12 @@ describe('Comment Preservation', () => { }); `, expected: ` - it('should do something async', async () => { + // TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually. + it('should do something async', (done) => { // Start the async operation setTimeout(() => { // It's done now + done(); }, 100); }); `, diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts index f71e55eeef46..47aaa470a554 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts @@ -147,9 +147,7 @@ export function transformPending( function transformComplexDoneCallback( node: ts.Node, doneIdentifier: ts.Identifier, - refactorCtx: RefactorContext, ): ts.Node | ts.Node[] | undefined { - const { sourceFile, reporter } = refactorCtx; if ( !ts.isExpressionStatement(node) || !ts.isCallExpression(node.expression) || @@ -193,12 +191,6 @@ function transformComplexDoneCallback( return node; } - reporter.reportTransformation( - sourceFile, - node, - 'Transformed promise `.then()` with `done()` to `await`.', - ); - const newThenCallback = ts.isArrowFunction(thenCallback) ? ts.factory.updateArrowFunction( thenCallback, @@ -230,9 +222,7 @@ function transformComplexDoneCallback( function transformPromiseBasedDone( callExpr: ts.CallExpression, doneIdentifier: ts.Identifier, - refactorCtx: RefactorContext, ): ts.Node | undefined { - const { sourceFile, reporter } = refactorCtx; if ( ts.isPropertyAccessExpression(callExpr.expression) && (callExpr.expression.name.text === 'then' || callExpr.expression.name.text === 'catch') @@ -277,12 +267,6 @@ function transformPromiseBasedDone( } if (isDoneHandler) { - reporter.reportTransformation( - sourceFile, - callExpr, - 'Transformed promise `.then(done)` to `await`.', - ); - return ts.factory.createExpressionStatement( ts.factory.createAwaitExpression(callExpr.expression.expression), ); @@ -342,14 +326,20 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex // Count total usages of 'done' in the body const totalUsages = countDoneUsages(functionArg.body, doneIdentifier); - let handledUsages = 0; - let doneWasUsed = false; + const handledUsages: { node: ts.Node; message?: string }[] = []; const bodyVisitor = (bodyNode: ts.Node): ts.Node | ts.Node[] | undefined => { - const complexTransformed = transformComplexDoneCallback(bodyNode, doneIdentifier, refactorCtx); + // Leave nested callbacks for manual migration unless their enclosing promise is awaited. + if (ts.isFunctionLike(bodyNode)) { + return bodyNode; + } + + const complexTransformed = transformComplexDoneCallback(bodyNode, doneIdentifier); if (complexTransformed !== bodyNode) { - doneWasUsed = true; - handledUsages++; // complex transform handles one usage + handledUsages.push({ + node: bodyNode, + message: 'Transformed promise `.then()` with `done()` to `await`.', + }); return complexTransformed; } @@ -364,13 +354,10 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex callExpr.expression.expression.text === doneIdentifier.text && callExpr.expression.name.text === 'fail' ) { - doneWasUsed = true; - handledUsages++; - reporter.reportTransformation( - sourceFile, - bodyNode, - 'Transformed `done.fail()` to `throw new Error()`.', - ); + handledUsages.push({ + node: bodyNode, + message: 'Transformed `done.fail()` to `throw new Error()`.', + }); const errorArgs = callExpr.arguments.length > 0 ? [callExpr.arguments[0]] : []; return ts.factory.createThrowStatement( @@ -383,10 +370,12 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex } // Transform `promise.then(done)` or `promise.catch(done.fail)` to `await promise` - const promiseTransformed = transformPromiseBasedDone(callExpr, doneIdentifier, refactorCtx); + const promiseTransformed = transformPromiseBasedDone(callExpr, doneIdentifier); if (promiseTransformed) { - doneWasUsed = true; - handledUsages++; + handledUsages.push({ + node: callExpr, + message: 'Transformed promise `.then(done)` to `await`.', + }); return promiseTransformed; } @@ -396,8 +385,7 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex ts.isIdentifier(callExpr.expression) && callExpr.expression.text === doneIdentifier.text ) { - doneWasUsed = true; - handledUsages++; + handledUsages.push({ node: callExpr }); return ts.setTextRange(ts.factory.createEmptyStatement(), callExpr.expression); } @@ -422,7 +410,7 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex }); // Safety check: if we found usages but didn't handle all of them, abort. - if (handledUsages < totalUsages) { + if (handledUsages.length < totalUsages) { reporter.reportTransformation( sourceFile, node, @@ -435,10 +423,16 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex return node; } - if (!doneWasUsed) { + if (!handledUsages.length) { return node; } + for (const usage of handledUsages) { + if (usage.message) { + reporter.reportTransformation(sourceFile, usage.node, usage.message); + } + } + reporter.reportTransformation( sourceFile, node, diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts index 8fcaf40fc9c9..fea98c0706be 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts @@ -11,7 +11,7 @@ import { expectTransformation } from '../test-helpers'; describe('Jasmine to Vitest Transformer - transformDoneCallback', () => { const testCases = [ { - description: 'should transform an `it` block with a done callback to an async function', + description: 'should flag a done callback inside a timer for manual migration', input: ` it('should do something async', (done) => { setTimeout(() => { @@ -21,9 +21,11 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => { }); `, expected: ` - it('should do something async', async () => { + // TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually. + it('should do something async', (done) => { setTimeout(() => { expect(true).toBe(true); + done(); }, 100); }); `, @@ -89,7 +91,7 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => { `, }, { - description: 'should work with a custom done name', + description: 'should flag a nested callback with a custom done name', input: ` it('should work with a custom done name', (finish) => { setTimeout(() => { @@ -98,12 +100,30 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => { }); `, expected: ` - it('should work with a custom done name', async () => { + // TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually. + it('should work with a custom done name', (finish) => { setTimeout(() => { + finish(); }, 100); }); `, }, + { + description: 'should keep the done parameter when a nested done.fail cannot be migrated', + input: ` + beforeEach((done) => { + service.onError(function (error) { done.fail(error); }); + service.init().then(done); + }); + `, + expected: ` + // TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually. + beforeEach((done) => { + service.onError(function (error) { done.fail(error); }); + service.init().then(done); + }); + `, + }, { description: 'should handle done in a finally block', input: `