Skip to content

Commit 21c3eac

Browse files
jase88clydin
authored andcommitted
fix(@schematics/angular): handle createSpyObj without base name on refactor-jasmine-vitest
(cherry picked from commit 5ef8b99)
1 parent 7222b54 commit 21c3eac

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,14 @@ export function transformCreateSpyObj(
227227
'Transformed `jasmine.createSpyObj()` to an object literal with `vi.fn()`.',
228228
);
229229

230-
const baseNameArg = node.arguments[0];
231-
const baseName = ts.isStringLiteral(baseNameArg) ? baseNameArg.text : undefined;
232-
const methods = node.arguments[1];
233-
const propertiesArg = node.arguments[2];
230+
const firstArg = node.arguments[0];
231+
const hasBaseName = ts.isStringLiteral(firstArg);
232+
const baseName = hasBaseName ? firstArg.text : undefined;
233+
const methods = hasBaseName ? node.arguments[1] : firstArg;
234+
const propertiesArg = hasBaseName ? node.arguments[2] : node.arguments[1];
234235
let properties: ts.PropertyAssignment[] = [];
235236

236-
if (node.arguments.length < 2) {
237+
if (node.arguments.length < 2 && hasBaseName) {
237238
const category = 'createSpyObj-single-argument';
238239
reporter.recordTodo(category);
239240
addTodoComment(node, category);

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
122122
methodB: vi.fn().mockName("MyService.methodB"),
123123
};`,
124124
},
125+
{
126+
description:
127+
'should transform jasmine.createSpyObj with an array of methods without base name',
128+
input: `const myService = jasmine.createSpyObj(['methodA', 'methodB']);`,
129+
expected: `const myService = {
130+
methodA: vi.fn(),
131+
methodB: vi.fn(),
132+
};`,
133+
},
125134
{
126135
description: 'should add a TODO if the second argument is not a literal',
127136
input: `const myService = jasmine.createSpyObj('MyService', methodNames);`,
@@ -138,6 +147,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
138147
methodB: vi.fn().mockName("MyService.methodB").mockReturnValue(42),
139148
};`,
140149
},
150+
{
151+
description:
152+
'should transform jasmine.createSpyObj with an object of return values without base name',
153+
input: `const myService = jasmine.createSpyObj({ methodA: 'foo', methodB: 42 });`,
154+
expected: `const myService = {
155+
methodA: vi.fn().mockReturnValue('foo'),
156+
methodB: vi.fn().mockReturnValue(42),
157+
};`,
158+
},
141159
{
142160
description:
143161
'should transform jasmine.createSpyObj with an object of return values containing an asymmetric matcher',
@@ -147,7 +165,7 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
147165
};`,
148166
},
149167
{
150-
description: 'should add a TODO for jasmine.createSpyObj with only one argument',
168+
description: 'should add a TODO for jasmine.createSpyObj with only base name argument',
151169
input: `const myService = jasmine.createSpyObj('MyService');`,
152170
expected: `
153171
// TODO: vitest-migration: jasmine.createSpyObj called with a single argument is not supported for transformation. See: https://vitest.dev/api/vi.html#vi-fn
@@ -170,6 +188,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
170188
propA: 'valueA',
171189
};`,
172190
},
191+
{
192+
description:
193+
'should transform jasmine.createSpyObj with a method map and a property map without base name',
194+
input: `const myService = jasmine.createSpyObj({ methodA: 'foo' }, { propA: 'valueA' });`,
195+
expected: `const myService = {
196+
methodA: vi.fn().mockReturnValue('foo'),
197+
propA: 'valueA',
198+
};`,
199+
},
173200
{
174201
description: 'should ignore non-string literals in the method array',
175202
input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 123, someVar]);`,

0 commit comments

Comments
 (0)