From 991f4809e6c40744b30a1888dff61860310141a7 Mon Sep 17 00:00:00 2001 From: rk Date: Fri, 30 Oct 2026 14:15:48 +0500 Subject: [PATCH 1/3] 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/3] 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; } From 89e35f47b138f782a122e9ebb56b5830a075d476 Mon Sep 17 00:00:00 2001 From: rk Date: Tue, 3 Nov 2026 16:33:34 +0500 Subject: [PATCH 3/3] fixup! fix(@schematics/angular): cast createSpyObj to MockedObject for jasmine.SpyObj variables in refactor-jasmine-vitest Address review: use explicit type argument of createSpyObj, look into union types and keep an existing cast instead of adding a second one. Co-authored-by: Alan Agius --- .../transformers/jasmine-spy.ts | 30 ++++++++++-- .../transformers/jasmine-spy_spec.ts | 47 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 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 a5a90201de58..5bb25d67bc5d 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts @@ -462,10 +462,20 @@ export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.N const spyObjLiteral = ts.factory.createObjectLiteralExpression(properties, true); + let parent = node.parent; + while (parent && ts.isParenthesizedExpression(parent)) { + parent = parent.parent; + } + + // An existing cast on the call already gives the literal its type. + if (parent && (ts.isAsExpression(parent) || ts.isTypeAssertionExpression(parent))) { + return spyObjLiteral; + } + // 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); + const spyObjTypeArguments = node.typeArguments ?? getAssignedSpyObjTypeArguments(parent); if (!spyObjTypeArguments) { return spyObjLiteral; } @@ -481,6 +491,18 @@ export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.N function getSpyObjTypeArguments( type: ts.TypeNode | undefined, ): ts.NodeArray | undefined { + // `jasmine.SpyObj | undefined` under `strictNullChecks`. + if (type && ts.isUnionTypeNode(type)) { + for (const member of type.types) { + const typeArguments = getSpyObjTypeArguments(member); + if (typeArguments) { + return typeArguments; + } + } + + return undefined; + } + if ( type && ts.isTypeReferenceNode(type) && @@ -527,12 +549,12 @@ function findEnclosingVariableDeclaration( /** * 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 = ...`). + * initializes (`const spy: jasmine.SpyObj = ...`) or is assigned to (`spy = ...`), given the + * call's parent outside any parentheses. */ function getAssignedSpyObjTypeArguments( - node: ts.CallExpression, + parent: ts.Node | undefined, ): ts.NodeArray | undefined { - const parent = getUnwrappedParent(node); if (!parent) { return undefined; } 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 51fe8fc89bd8..38383e7c47ca 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 @@ -214,6 +214,53 @@ describe('transformCreateSpyObj', () => { } as MockedObject; `, }, + { + description: + 'should cast jasmine.createSpyObj to MockedObject when assigned to a jasmine.SpyObj union variable', + input: ` + let myService: jasmine.SpyObj | undefined; + myService = jasmine.createSpyObj('MyService', ['methodA']); + `, + expected: ` + import type { MockedObject } from 'vitest'; + + let myService: MockedObject | undefined; + myService = { + methodA: vi.fn().mockName('MyService.methodA'), + } as MockedObject; + `, + }, + { + description: + 'should cast jasmine.createSpyObj with an explicit type argument to MockedObject', + input: `const myService = jasmine.createSpyObj('MyService', ['methodA']);`, + expected: ` + import type { MockedObject } from 'vitest'; + const myService = { + methodA: vi.fn().mockName('MyService.methodA'), + } as MockedObject; + `, + }, + { + description: + 'should not add a second cast to a jasmine.createSpyObj call that is already cast', + input: ` + let myService: jasmine.SpyObj; + myService = jasmine.createSpyObj('MyService', ['methodA']) as jasmine.SpyObj; + const other = jasmine.createSpyObj('Other', ['methodB']) as any; + `, + expected: ` + import type { MockedObject } from 'vitest'; + + let myService: MockedObject; + myService = { + methodA: vi.fn().mockName('MyService.methodA'), + } as MockedObject; + const other = { + methodB: vi.fn().mockName('Other.methodB'), + } as any; + `, + }, { description: 'should cast jasmine.createSpyObj to the jasmine.SpyObj type of the variable in the enclosing describe block',