Skip to content

Commit 4ea7c67

Browse files
thekhegayalan-agius4
authored andcommitted
fix(@schematics/angular): keep the default getter access type of spyOnProperty in refactor-jasmine-vitest
Jasmine spies on getter when `spyOnProperty` gets no access type, migration turned `spyOnProperty(obj, 'prop')` into `vi.spyOn(obj, 'prop')`, which Vitest treats as method spy, so converted spec failed with TS2769 now `'get'` is passed when access type is omited, explicit one is kept
1 parent 7d6550e commit 4ea7c67

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,18 @@ function transformPrimarySpy(node: ts.CallExpression, refactorCtx: RefactorConte
103103
) {
104104
addVitestValueImport(pendingVitestValueImports, 'vi');
105105

106+
// `spyOnProperty` defaults to the getter while `vi.spyOn` needs the access type to spy on
107+
// a property instead of a method.
108+
const args =
109+
node.expression.text === 'spyOnProperty' && node.arguments.length === 2
110+
? [...node.arguments, ts.factory.createStringLiteral('get', true)]
111+
: node.arguments;
112+
106113
const viSpyOnCall = ts.factory.updateCallExpression(
107114
node,
108115
createPropertyAccess('vi', 'spyOn'),
109116
node.typeArguments,
110-
node.arguments,
117+
args,
111118
);
112119

113120
if (isChainedWithAnd(node)) {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ describe('Jasmine to Vitest Transformer - transformSpies', () => {
6060
},
6161
{
6262
description:
63-
'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop").mockReturnValue(undefined)',
63+
'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop", "get").mockReturnValue(undefined)',
6464
input: `spyOnProperty(service, 'myProp');`,
65-
expected: `vi.spyOn(service, 'myProp').mockReturnValue(undefined);`,
65+
expected: `vi.spyOn(service, 'myProp', 'get').mockReturnValue(undefined);`,
66+
},
67+
{
68+
description: 'should default the access type of a chained spyOnProperty to "get"',
69+
input: `spyOnProperty(service, 'myProp').and.returnValue(42);`,
70+
expected: `vi.spyOn(service, 'myProp', 'get').mockReturnValue(42);`,
71+
},
72+
{
73+
description: 'should keep an explicit spyOnProperty access type',
74+
input: `spyOnProperty(service, 'myProp', 'set');`,
75+
expected: `vi.spyOn(service, 'myProp', 'set').mockReturnValue(undefined);`,
6676
},
6777
{
6878
description: 'should transform .and.stub() to .mockImplementation(() => {})',

0 commit comments

Comments
 (0)