From cc5895ee9ed58105a6e102b5cee6d09b8551047f Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 14 May 2026 19:55:26 -0400 Subject: [PATCH] fix(@schematics/angular): support spy call arguments migration in refactor-jasmine-vitest The `refactor-jasmine-vitest` schematic fails to remove `.args` when migrating `spy.calls.all()[i].args`, resulting in uncompilable code in Vitest. Now, a specialized transformer detects `spy.calls.all()[i].args` and transforms it to `vi.mocked(spy).mock.calls[i]`, removing the unnecessary `.args` property access. Fixes #33112 --- .../transformers/jasmine-spy.ts | 52 +++++++++++++++++++ .../transformers/jasmine-spy_spec.ts | 5 ++ 2 files changed, 57 insertions(+) diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts index 543ba5a2daee..c840c374976d 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts @@ -505,6 +505,53 @@ function transformThisFor( ); } +function transformAllCallsArgs( + node: ts.Node, + { sourceFile, reporter, pendingVitestValueImports }: RefactorContext, +): ts.Node { + if ( + !ts.isPropertyAccessExpression(node) || + !ts.isIdentifier(node.name) || + node.name.text !== 'args' + ) { + return node; + } + + const elementAccess = node.expression; + if (!ts.isElementAccessExpression(elementAccess)) { + return node; + } + + const allCall = elementAccess.expression; + if (!ts.isCallExpression(allCall) || !ts.isPropertyAccessExpression(allCall.expression)) { + return node; + } + + const allPae = allCall.expression; + if (!ts.isIdentifier(allPae.name) || allPae.name.text !== 'all') { + return node; + } + + if (!ts.isPropertyAccessExpression(allPae.expression)) { + return node; + } + + const spyIdentifier = getSpyIdentifierFromCalls(allPae.expression); + if (!spyIdentifier) { + return node; + } + + reporter.reportTransformation( + sourceFile, + node, + 'Transformed `spy.calls.all()[i].args` to `vi.mocked(spy).mock.calls[i]`.', + ); + const mockProperty = createMockedSpyMockProperty(spyIdentifier, pendingVitestValueImports); + const callsProperty = createPropertyAccess(mockProperty, 'calls'); + + return ts.factory.createElementAccessExpression(callsProperty, elementAccess.argumentExpression); +} + export function transformSpyCallInspection(node: ts.Node, refactorCtx: RefactorContext): ts.Node { const mostRecentArgsTransformed = transformMostRecentArgs(node, refactorCtx); if (mostRecentArgsTransformed !== node) { @@ -516,6 +563,11 @@ export function transformSpyCallInspection(node: ts.Node, refactorCtx: RefactorC return thisForTransformed; } + const allCallsArgsTransformed = transformAllCallsArgs(node, refactorCtx); + if (allCallsArgsTransformed !== node) { + return allCallsArgsTransformed; + } + if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) { return node; } diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts index 85a0068240c7..97881049c1d5 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts @@ -270,6 +270,11 @@ describe('transformSpyCallInspection', () => { input: `const allCalls = mySpy.calls.all();`, expected: `const allCalls = vi.mocked(mySpy).mock.calls;`, }, + { + description: 'should transform spy.calls.all()[i].args', + input: `expect(mySpy.calls.all()[2].args[0]).toBeInstanceOf(RemoveShareUrlAction);`, + expected: `expect(vi.mocked(mySpy).mock.calls[2][0]).toBeInstanceOf(RemoveShareUrlAction);`, + }, { description: 'should transform spy.calls.mostRecent().args', input: `const recentArgs = mySpy.calls.mostRecent().args;`,