From 32b6db32be4ecfee4c77c4c512b93e79f919e85a Mon Sep 17 00:00:00 2001 From: MeAkib Date: Thu, 5 Nov 2026 23:57:18 +0600 Subject: [PATCH 1/2] fix(@schematics/angular): preserve stub-by-default semantics for createSpy with an original function `jasmine.createSpy(name, originalFn)` stores the original function but does not invoke it: the default spy plan is a no-op stub, and the original function only runs after `.and.callThrough()`. The refactor-jasmine-vitest schematic maps this call to `vi.fn(originalFn)`, which invokes the function immediately, so a migrated bare spy suddenly calls through and can change test behavior. Append `.mockReturnValue(undefined)` when `createSpy` is given an original function and is not chained with an `.and` strategy. This keeps the original function available for `mockReset()` while preserving the stub-by-default behavior, mirroring how bare `spyOn` calls are already handled. Chained spies are unchanged: `.and.callThrough()` keeps mapping to a plain `vi.fn(originalFn)` and strategies such as `.and.returnValue()` keep replacing the stub. The updated spec fails without the fix: `jasmine.createSpy('mySpy', () => 'foo')` was migrated to `vi.fn(() => 'foo').mockName('mySpy')`, which calls the function. --- .../transformers/jasmine-spy.ts | 45 +++++++++++++------ .../transformers/jasmine-spy_spec.ts | 9 +++- 2 files changed, 40 insertions(+), 14 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 7506cea81e0c..55d8a2c7fdd9 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts @@ -441,28 +441,47 @@ export function transformCreateSpy(node: ts.Node, ctx: RefactorContext): ts.Node } addVitestValueImport(pendingVitestValueImports, 'vi'); - reporter.reportTransformation( - sourceFile, - node, - 'Transformed `jasmine.createSpy()` to `vi.fn()`.', - ); const spyName = node.arguments[0]; - const viFnCallExpression = createViCallExpression( - ctx, - 'fn', - node.arguments.length > 1 ? [node.arguments[1]] : [], - ); + const originalFn = node.arguments[1]; + const viFnCallExpression = createViCallExpression(ctx, 'fn', originalFn ? [originalFn] : []); // jasmine.createSpy() -> vi.fn() - // jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name) - return !spyName + // jasmine.createSpy(name) -> vi.fn().mockName(name) + // jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name)... + let result: ts.Expression = !spyName ? viFnCallExpression : ts.factory.createCallExpression( createPropertyAccess(viFnCallExpression, 'mockName'), undefined, - [node.arguments[0]], + [spyName], ); + + // Jasmine only invokes `originalFn` after `.and.callThrough()`, while `vi.fn(originalFn)` + // calls it right away. Without a chained strategy, stub the spy so `originalFn` is kept + // for `mockReset()` but not invoked, as `transformPrimarySpy` does for bare `spyOn`. + if (originalFn && !isChainedWithAnd(node)) { + reporter.reportTransformation( + sourceFile, + node, + 'Transformed `jasmine.createSpy()` to `vi.fn()`, ' + + 'appending `.mockReturnValue(undefined)` to preserve stub-by-default semantics.', + ); + + result = ts.factory.createCallExpression( + createPropertyAccess(result, 'mockReturnValue'), + undefined, + [ts.factory.createIdentifier('undefined')], + ); + } else { + reporter.reportTransformation( + sourceFile, + node, + 'Transformed `jasmine.createSpy()` to `vi.fn()`.', + ); + } + + return result; } export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.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 5c82afd10c53..41d624d2d76d 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 @@ -71,8 +71,15 @@ getSpy().and.callThrough();`, expected: `const mySpy = vi.fn().mockName('mySpy');`, }, { - description: 'should transform jasmine.createSpy("name", fn) to vi.fn(fn).mockName("name")', + description: + 'should transform jasmine.createSpy("name", fn) to ' + + 'vi.fn(fn).mockName("name").mockReturnValue(undefined)', input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo');`, + expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy').mockReturnValue(undefined);`, + }, + { + description: 'should not stub jasmine.createSpy("name", fn) chained with .and.callThrough()', + input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo').and.callThrough();`, expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy');`, }, { From 8a731a9d8098a8b0068cdc82ee725313bb4111ac Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Wed, 11 Nov 2026 20:26:35 +0600 Subject: [PATCH 2/2] fixup! fix(@schematics/angular): preserve stub-by-default semantics for createSpy with an original function --- .../refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts | 5 +++++ 1 file changed, 5 insertions(+) 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 41d624d2d76d..827036d765c8 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 @@ -82,6 +82,11 @@ getSpy().and.callThrough();`, input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo').and.callThrough();`, expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy');`, }, + { + description: 'should not stub jasmine.createSpy("name", fn) chained with another strategy', + input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo').and.returnValue(true);`, + expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy').mockReturnValue(true);`, + }, { description: 'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop", "get").mockReturnValue(undefined)',