Skip to content

Commit a61b789

Browse files
fix(@schematics/angular): preserve nested done callbacks during Vitest migration
Keep Jasmine completion callbacks in nested functions when the migration cannot await their work. Flag the test for manual migration and report only transformations that were applied. Fixes #34730
1 parent 0a61cbc commit a61b789

4 files changed

Lines changed: 87 additions & 41 deletions

File tree

‎packages/schematics/angular/refactor/jasmine-vitest/index_spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ describe('Jasmine to Vitest Schematic', () => {
6262
expect(newContent).toContain(`vi.spyOn(service, 'myMethod').mockReturnValue(undefined);`);
6363
});
6464

65+
it('should report a nested done callback for manual migration', async () => {
66+
const specFilePath = 'projects/bar/src/app/app.spec.ts';
67+
appTree.overwrite(
68+
specFilePath,
69+
`
70+
it('waits for a value', (done) => {
71+
values.subscribe({ error: (error) => { done.fail(error); } });
72+
ready.then(done);
73+
});
74+
`,
75+
);
76+
77+
const logs: string[] = [];
78+
schematicRunner.logger.subscribe((entry) => logs.push(entry.message));
79+
const tree = await schematicRunner.runSchematic(
80+
'refactor-jasmine-vitest',
81+
{ project: 'bar', verbose: true },
82+
appTree,
83+
);
84+
85+
const content = tree.readContent(specFilePath);
86+
expect(content).toContain('Please migrate manually.');
87+
expect(content).toContain('done.fail(error)');
88+
expect(content).toContain('ready.then(done)');
89+
expect(logs).toContain(' - 1x unhandled-done-usage');
90+
expect(logs.join('\n')).not.toContain('Transformed promise');
91+
});
92+
6593
it('should only transform files matching the fileSuffix option', async () => {
6694
const specFilePath = 'projects/bar/src/app/app.spec.ts';
6795
const specFileContent = `

‎packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ describe('Jasmine to Vitest Transformer - Nested Transformations', () => {
4646
});
4747
`,
4848
expected: `
49-
it('should handle timers and async', async () => {
49+
// TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually.
50+
it('should handle timers and async', (done) => {
5051
vi.useFakeTimers();
5152
setTimeout(() => {
5253
expect(true).toBe(true);
5354
vi.useRealTimers();
55+
done();
5456
}, 100);
5557
vi.advanceTimersByTime(100);
5658
});
@@ -175,10 +177,12 @@ describe('Comment Preservation', () => {
175177
});
176178
`,
177179
expected: `
178-
it('should do something async', async () => {
180+
// TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually.
181+
it('should do something async', (done) => {
179182
// Start the async operation
180183
setTimeout(() => {
181184
// It's done now
185+
done();
182186
}, 100);
183187
});
184188
`,

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts‎

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ export function transformPending(
147147
function transformComplexDoneCallback(
148148
node: ts.Node,
149149
doneIdentifier: ts.Identifier,
150-
refactorCtx: RefactorContext,
151150
): ts.Node | ts.Node[] | undefined {
152-
const { sourceFile, reporter } = refactorCtx;
153151
if (
154152
!ts.isExpressionStatement(node) ||
155153
!ts.isCallExpression(node.expression) ||
@@ -193,12 +191,6 @@ function transformComplexDoneCallback(
193191
return node;
194192
}
195193

196-
reporter.reportTransformation(
197-
sourceFile,
198-
node,
199-
'Transformed promise `.then()` with `done()` to `await`.',
200-
);
201-
202194
const newThenCallback = ts.isArrowFunction(thenCallback)
203195
? ts.factory.updateArrowFunction(
204196
thenCallback,
@@ -230,9 +222,7 @@ function transformComplexDoneCallback(
230222
function transformPromiseBasedDone(
231223
callExpr: ts.CallExpression,
232224
doneIdentifier: ts.Identifier,
233-
refactorCtx: RefactorContext,
234225
): ts.Node | undefined {
235-
const { sourceFile, reporter } = refactorCtx;
236226
if (
237227
ts.isPropertyAccessExpression(callExpr.expression) &&
238228
(callExpr.expression.name.text === 'then' || callExpr.expression.name.text === 'catch')
@@ -277,12 +267,6 @@ function transformPromiseBasedDone(
277267
}
278268

279269
if (isDoneHandler) {
280-
reporter.reportTransformation(
281-
sourceFile,
282-
callExpr,
283-
'Transformed promise `.then(done)` to `await`.',
284-
);
285-
286270
return ts.factory.createExpressionStatement(
287271
ts.factory.createAwaitExpression(callExpr.expression.expression),
288272
);
@@ -342,14 +326,20 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
342326

343327
// Count total usages of 'done' in the body
344328
const totalUsages = countDoneUsages(functionArg.body, doneIdentifier);
345-
let handledUsages = 0;
346-
let doneWasUsed = false;
329+
const handledUsages: { node: ts.Node; message?: string }[] = [];
347330

348331
const bodyVisitor = (bodyNode: ts.Node): ts.Node | ts.Node[] | undefined => {
349-
const complexTransformed = transformComplexDoneCallback(bodyNode, doneIdentifier, refactorCtx);
332+
// Leave nested callbacks for manual migration unless their enclosing promise is awaited.
333+
if (ts.isFunctionLike(bodyNode)) {
334+
return bodyNode;
335+
}
336+
337+
const complexTransformed = transformComplexDoneCallback(bodyNode, doneIdentifier);
350338
if (complexTransformed !== bodyNode) {
351-
doneWasUsed = true;
352-
handledUsages++; // complex transform handles one usage
339+
handledUsages.push({
340+
node: bodyNode,
341+
message: 'Transformed promise `.then()` with `done()` to `await`.',
342+
});
353343

354344
return complexTransformed;
355345
}
@@ -364,13 +354,10 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
364354
callExpr.expression.expression.text === doneIdentifier.text &&
365355
callExpr.expression.name.text === 'fail'
366356
) {
367-
doneWasUsed = true;
368-
handledUsages++;
369-
reporter.reportTransformation(
370-
sourceFile,
371-
bodyNode,
372-
'Transformed `done.fail()` to `throw new Error()`.',
373-
);
357+
handledUsages.push({
358+
node: bodyNode,
359+
message: 'Transformed `done.fail()` to `throw new Error()`.',
360+
});
374361
const errorArgs = callExpr.arguments.length > 0 ? [callExpr.arguments[0]] : [];
375362

376363
return ts.factory.createThrowStatement(
@@ -383,10 +370,12 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
383370
}
384371

385372
// Transform `promise.then(done)` or `promise.catch(done.fail)` to `await promise`
386-
const promiseTransformed = transformPromiseBasedDone(callExpr, doneIdentifier, refactorCtx);
373+
const promiseTransformed = transformPromiseBasedDone(callExpr, doneIdentifier);
387374
if (promiseTransformed) {
388-
doneWasUsed = true;
389-
handledUsages++;
375+
handledUsages.push({
376+
node: callExpr,
377+
message: 'Transformed promise `.then(done)` to `await`.',
378+
});
390379

391380
return promiseTransformed;
392381
}
@@ -396,8 +385,7 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
396385
ts.isIdentifier(callExpr.expression) &&
397386
callExpr.expression.text === doneIdentifier.text
398387
) {
399-
doneWasUsed = true;
400-
handledUsages++;
388+
handledUsages.push({ node: callExpr });
401389

402390
return ts.setTextRange(ts.factory.createEmptyStatement(), callExpr.expression);
403391
}
@@ -422,7 +410,7 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
422410
});
423411

424412
// Safety check: if we found usages but didn't handle all of them, abort.
425-
if (handledUsages < totalUsages) {
413+
if (handledUsages.length < totalUsages) {
426414
reporter.reportTransformation(
427415
sourceFile,
428416
node,
@@ -435,10 +423,16 @@ export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContex
435423
return node;
436424
}
437425

438-
if (!doneWasUsed) {
426+
if (!handledUsages.length) {
439427
return node;
440428
}
441429

430+
for (const usage of handledUsages) {
431+
if (usage.message) {
432+
reporter.reportTransformation(sourceFile, usage.node, usage.message);
433+
}
434+
}
435+
442436
reporter.reportTransformation(
443437
sourceFile,
444438
node,

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { expectTransformation } from '../test-helpers';
1111
describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
1212
const testCases = [
1313
{
14-
description: 'should transform an `it` block with a done callback to an async function',
14+
description: 'should flag a done callback inside a timer for manual migration',
1515
input: `
1616
it('should do something async', (done) => {
1717
setTimeout(() => {
@@ -21,9 +21,11 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
2121
});
2222
`,
2323
expected: `
24-
it('should do something async', async () => {
24+
// TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually.
25+
it('should do something async', (done) => {
2526
setTimeout(() => {
2627
expect(true).toBe(true);
28+
done();
2729
}, 100);
2830
});
2931
`,
@@ -89,7 +91,7 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
8991
`,
9092
},
9193
{
92-
description: 'should work with a custom done name',
94+
description: 'should flag a nested callback with a custom done name',
9395
input: `
9496
it('should work with a custom done name', (finish) => {
9597
setTimeout(() => {
@@ -98,12 +100,30 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
98100
});
99101
`,
100102
expected: `
101-
it('should work with a custom done name', async () => {
103+
// TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually.
104+
it('should work with a custom done name', (finish) => {
102105
setTimeout(() => {
106+
finish();
103107
}, 100);
104108
});
105109
`,
106110
},
111+
{
112+
description: 'should keep the done parameter when a nested done.fail cannot be migrated',
113+
input: `
114+
beforeEach((done) => {
115+
service.onError(function (error) { done.fail(error); });
116+
service.init().then(done);
117+
});
118+
`,
119+
expected: `
120+
// TODO: vitest-migration: The 'done' callback was used in an unhandled way. Please migrate manually.
121+
beforeEach((done) => {
122+
service.onError(function (error) { done.fail(error); });
123+
service.init().then(done);
124+
});
125+
`,
126+
},
107127
{
108128
description: 'should handle done in a finally block',
109129
input: `

0 commit comments

Comments
 (0)