Skip to content

Commit c13afda

Browse files
MeAkibalan-agius4
authored andcommitted
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. (cherry picked from commit d899c51)
1 parent d363c42 commit c13afda

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,28 +441,47 @@ export function transformCreateSpy(node: ts.Node, ctx: RefactorContext): ts.Node
441441
}
442442

443443
addVitestValueImport(pendingVitestValueImports, 'vi');
444-
reporter.reportTransformation(
445-
sourceFile,
446-
node,
447-
'Transformed `jasmine.createSpy()` to `vi.fn()`.',
448-
);
449444

450445
const spyName = node.arguments[0];
451-
const viFnCallExpression = createViCallExpression(
452-
ctx,
453-
'fn',
454-
node.arguments.length > 1 ? [node.arguments[1]] : [],
455-
);
446+
const originalFn = node.arguments[1];
447+
const viFnCallExpression = createViCallExpression(ctx, 'fn', originalFn ? [originalFn] : []);
456448

457449
// jasmine.createSpy() -> vi.fn()
458-
// jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name)
459-
return !spyName
450+
// jasmine.createSpy(name) -> vi.fn().mockName(name)
451+
// jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name)...
452+
let result: ts.Expression = !spyName
460453
? viFnCallExpression
461454
: ts.factory.createCallExpression(
462455
createPropertyAccess(viFnCallExpression, 'mockName'),
463456
undefined,
464-
[node.arguments[0]],
457+
[spyName],
465458
);
459+
460+
// Jasmine only invokes `originalFn` after `.and.callThrough()`, while `vi.fn(originalFn)`
461+
// calls it right away. Without a chained strategy, stub the spy so `originalFn` is kept
462+
// for `mockReset()` but not invoked, as `transformPrimarySpy` does for bare `spyOn`.
463+
if (originalFn && !isChainedWithAnd(node)) {
464+
reporter.reportTransformation(
465+
sourceFile,
466+
node,
467+
'Transformed `jasmine.createSpy()` to `vi.fn()`, ' +
468+
'appending `.mockReturnValue(undefined)` to preserve stub-by-default semantics.',
469+
);
470+
471+
result = ts.factory.createCallExpression(
472+
createPropertyAccess(result, 'mockReturnValue'),
473+
undefined,
474+
[ts.factory.createIdentifier('undefined')],
475+
);
476+
} else {
477+
reporter.reportTransformation(
478+
sourceFile,
479+
node,
480+
'Transformed `jasmine.createSpy()` to `vi.fn()`.',
481+
);
482+
}
483+
484+
return result;
466485
}
467486

468487
export function transformCreateSpyObj(node: ts.Node, ctx: RefactorContext): ts.Node {

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,22 @@ getSpy().and.callThrough();`,
7171
expected: `const mySpy = vi.fn().mockName('mySpy');`,
7272
},
7373
{
74-
description: 'should transform jasmine.createSpy("name", fn) to vi.fn(fn).mockName("name")',
74+
description:
75+
'should transform jasmine.createSpy("name", fn) to ' +
76+
'vi.fn(fn).mockName("name").mockReturnValue(undefined)',
7577
input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo');`,
78+
expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy').mockReturnValue(undefined);`,
79+
},
80+
{
81+
description: 'should not stub jasmine.createSpy("name", fn) chained with .and.callThrough()',
82+
input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo').and.callThrough();`,
7683
expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy');`,
7784
},
85+
{
86+
description: 'should not stub jasmine.createSpy("name", fn) chained with another strategy',
87+
input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo').and.returnValue(true);`,
88+
expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy').mockReturnValue(true);`,
89+
},
7890
{
7991
description:
8092
'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop", "get").mockReturnValue(undefined)',

0 commit comments

Comments
 (0)