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 9608a5e931f5..f71e55eeef46 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts @@ -50,23 +50,30 @@ export function transformFocusedAndSkippedTests( return node; } +/** + * Checks whether a call expression target is `it`, `it.only`, or `it.skip`. + * `fit`/`xit` have already become `it.only`/`it.skip` when `transformPending` + * and `transformDoneCallback` run, as `transformFocusedAndSkippedTests` is + * applied earlier in the pipeline. + */ +function isItCallExpression(expression: ts.Expression): boolean { + if (ts.isIdentifier(expression) && expression.text === 'it') { + return true; + } + + return ( + ts.isPropertyAccessExpression(expression) && + ts.isIdentifier(expression.expression) && + expression.expression.text === 'it' && + (expression.name.text === 'only' || expression.name.text === 'skip') + ); +} + export function transformPending( node: ts.Node, { sourceFile, reporter, tsContext }: RefactorContext, ): ts.Node { - if (!ts.isCallExpression(node)) { - return node; - } - - // `fit`/`xit` have already become `it.only`/`it.skip` when this transformer runs, - // as `transformFocusedAndSkippedTests` is applied earlier in the pipeline. - const isBareIt = ts.isIdentifier(node.expression) && node.expression.text === 'it'; - const isFocusedOrSkippedIt = - ts.isPropertyAccessExpression(node.expression) && - ts.isIdentifier(node.expression.expression) && - node.expression.expression.text === 'it' && - (node.expression.name.text === 'only' || node.expression.name.text === 'skip'); - if (!isBareIt && !isFocusedOrSkippedIt) { + if (!ts.isCallExpression(node) || !isItCallExpression(node.expression)) { return node; } @@ -301,11 +308,17 @@ function countDoneUsages(node: ts.Node, doneIdentifier: ts.Identifier): number { export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContext): ts.Node { const { sourceFile, reporter, tsContext } = refactorCtx; - if ( - !ts.isCallExpression(node) || - !ts.isIdentifier(node.expression) || - !['it', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll'].includes(node.expression.text) - ) { + if (!ts.isCallExpression(node)) { + return node; + } + + const isLifecycleHook = + ts.isIdentifier(node.expression) && + (node.expression.text === 'beforeEach' || + node.expression.text === 'afterEach' || + node.expression.text === 'beforeAll' || + node.expression.text === 'afterAll'); + if (!isItCallExpression(node.expression) && !isLifecycleHook) { return 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 d8fb576948ce..8fcaf40fc9c9 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 @@ -171,6 +171,32 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => { }); `, }, + { + description: 'should transform a focused test (fit) with a done callback', + input: ` + fit('should do something async', (done) => { + service.init().then(() => done()); + }); + `, + expected: ` + it.only('should do something async', async () => { + await service.init().then(() => {}); + }); + `, + }, + { + description: 'should transform a skipped test (xit) with a done callback', + input: ` + xit('should do something async', (done) => { + done.fail('it failed'); + }); + `, + expected: ` + it.skip('should do something async', async () => { + throw new Error('it failed'); + }); + `, + }, ]; testCases.forEach(({ description, input, expected }) => {