From 991f4809e6c40744b30a1888dff61860310141a7 Mon Sep 17 00:00:00 2001 From: rk Date: Fri, 30 Oct 2026 14:15:48 +0500 Subject: [PATCH 1/2] fix(@schematics/angular): cast createSpyObj to MockedObject for jasmine.SpyObj variables in refactor-jasmine-vitest `let svc: jasmine.SpyObj; svc = jasmine.createSpyObj(...)` from Angular testing guide became `MockedObject` variable assigned an object literal with only spied methods, which fails with TS2322 after migration and nothing in report points to it when createSpyObj initializes or is assigned to variable annotated with `jasmine.SpyObj`, literal is now cast `as MockedObject`, declaration is looked up in nearest enclosing block so same name in another describe gets its own type --- .../transformers/jasmine-spy.ts | 96 ++++++++++++++++++- .../transformers/jasmine-spy_spec.ts | 65 +++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) 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 68a76b378194..c063bd354826 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts @@ -16,6 +16,7 @@ import ts from 'typescript'; import { + addVitestTypeImport, addVitestValueImport, createPropertyAccess, getPromiseResolveRejectMethod, @@ -459,7 +460,100 @@ export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.N } } - return ts.factory.createObjectLiteralExpression(properties, true); + const spyObjLiteral = ts.factory.createObjectLiteralExpression(properties, true); + + // The literal only has the spied members, so it is not assignable to the `MockedObject` + // that a `jasmine.SpyObj` annotation is transformed to. Jasmine's untyped `createSpyObj` + // returns `any`, which is why the original assignment compiled. + const spyObjTypeArguments = getAssignedSpyObjTypeArguments(node); + if (!spyObjTypeArguments) { + return spyObjLiteral; + } + + addVitestTypeImport(ctx.pendingVitestTypeImports, 'MockedObject'); + + return ts.factory.createAsExpression( + spyObjLiteral, + ts.factory.createTypeReferenceNode('MockedObject', spyObjTypeArguments), + ); +} + +function getSpyObjTypeArguments( + type: ts.TypeNode | undefined, +): ts.NodeArray | undefined { + if ( + type && + ts.isTypeReferenceNode(type) && + ts.isQualifiedName(type.typeName) && + ts.isIdentifier(type.typeName.left) && + type.typeName.left.text === 'jasmine' && + type.typeName.right.text === 'SpyObj' + ) { + return type.typeArguments; + } + + return undefined; +} + +/** + * Finds the declaration of a variable in the blocks that enclose a node, nearest first, + * so that a variable declared in another `describe` block with the same name is not used. + */ +function findEnclosingVariableDeclaration( + node: ts.Node, + name: string, +): ts.VariableDeclaration | undefined { + for (let current = node.parent; current; current = current.parent) { + if (!ts.isBlock(current) && !ts.isSourceFile(current)) { + continue; + } + + for (const statement of current.statements) { + if (!ts.isVariableStatement(statement)) { + continue; + } + + const declaration = statement.declarationList.declarations.find( + (d) => ts.isIdentifier(d.name) && d.name.text === name, + ); + if (declaration) { + return declaration; + } + } + } + + return undefined; +} + +/** + * Finds the `T` of a `jasmine.SpyObj` annotation on the variable that a `createSpyObj` call + * initializes (`const spy: jasmine.SpyObj = ...`) or is assigned to (`spy = ...`). + */ +function getAssignedSpyObjTypeArguments( + node: ts.CallExpression, +): ts.NodeArray | undefined { + let parent = node.parent; + while (parent && ts.isParenthesizedExpression(parent)) { + parent = parent.parent; + } + + if (!parent) { + return undefined; + } + + if (ts.isVariableDeclaration(parent)) { + return getSpyObjTypeArguments(parent.type); + } + + if ( + !ts.isBinaryExpression(parent) || + parent.operatorToken.kind !== ts.SyntaxKind.EqualsToken || + !ts.isIdentifier(parent.left) + ) { + return undefined; + } + + return getSpyObjTypeArguments(findEnclosingVariableDeclaration(parent, parent.left.text)?.type); } function createSpyObjWithArray( 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 e14de03c3c17..51fe8fc89bd8 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 @@ -187,6 +187,71 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`, describe('transformCreateSpyObj', () => { const testCases = [ + { + description: + 'should cast jasmine.createSpyObj to MockedObject when assigned to a jasmine.SpyObj variable', + input: ` + let myService: jasmine.SpyObj; + myService = jasmine.createSpyObj('MyService', ['methodA']); + `, + expected: ` + import type { MockedObject } from 'vitest'; + + let myService: MockedObject; + myService = { + methodA: vi.fn().mockName('MyService.methodA'), + } as MockedObject; + `, + }, + { + description: + 'should cast jasmine.createSpyObj to MockedObject when it initializes a jasmine.SpyObj variable', + input: `const myService: jasmine.SpyObj = jasmine.createSpyObj('MyService', ['methodA']);`, + expected: ` + import type { MockedObject } from 'vitest'; + const myService: MockedObject = { + methodA: vi.fn().mockName('MyService.methodA'), + } as MockedObject; + `, + }, + { + description: + 'should cast jasmine.createSpyObj to the jasmine.SpyObj type of the variable in the enclosing describe block', + input: ` + describe('Foo', () => { + let svc: jasmine.SpyObj; + beforeEach(() => { + svc = jasmine.createSpyObj('Foo', ['a']); + }); + }); + describe('Bar', () => { + let svc: jasmine.SpyObj; + beforeEach(() => { + svc = jasmine.createSpyObj('Bar', ['b']); + }); + }); + `, + expected: ` + import type { MockedObject } from 'vitest'; + + describe('Foo', () => { + let svc: MockedObject; + beforeEach(() => { + svc = { + a: vi.fn().mockName('Foo.a'), + } as MockedObject; + }); + }); + describe('Bar', () => { + let svc: MockedObject; + beforeEach(() => { + svc = { + b: vi.fn().mockName('Bar.b'), + } as MockedObject; + }); + }); + `, + }, { description: 'should transform jasmine.createSpyObj with an array of methods', input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 'methodB']);`, From befdb9ca7f76d134c167b2485448b7389bedea59 Mon Sep 17 00:00:00 2001 From: rk Date: Fri, 30 Oct 2026 15:06:13 +0500 Subject: [PATCH 2/2] fixup! fix(@schematics/angular): cast createSpyObj to MockedObject for jasmine.SpyObj variables in refactor-jasmine-vitest --- .../refactor/jasmine-vitest/transformers/jasmine-spy.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) 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 c063bd354826..a5a90201de58 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts @@ -532,11 +532,7 @@ function findEnclosingVariableDeclaration( function getAssignedSpyObjTypeArguments( node: ts.CallExpression, ): ts.NodeArray | undefined { - let parent = node.parent; - while (parent && ts.isParenthesizedExpression(parent)) { - parent = parent.parent; - } - + const parent = getUnwrappedParent(node); if (!parent) { return undefined; }