Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,14 @@ export function transformCreateSpyObj(
'Transformed `jasmine.createSpyObj()` to an object literal with `vi.fn()`.',
);

const baseNameArg = node.arguments[0];
const baseName = ts.isStringLiteral(baseNameArg) ? baseNameArg.text : undefined;
const methods = node.arguments[1];
const propertiesArg = node.arguments[2];
const firstArg = node.arguments[0];
const hasBaseName = ts.isStringLiteral(firstArg);
const baseName = hasBaseName ? firstArg.text : undefined;
const methods = hasBaseName ? node.arguments[1] : firstArg;
const propertiesArg = hasBaseName ? node.arguments[2] : node.arguments[1];
let properties: ts.PropertyAssignment[] = [];

if (node.arguments.length < 2) {
if (node.arguments.length < 2 && hasBaseName) {
const category = 'createSpyObj-single-argument';
reporter.recordTodo(category);
addTodoComment(node, category);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
methodB: vi.fn().mockName("MyService.methodB"),
};`,
},
{
description:
'should transform jasmine.createSpyObj with an array of methods without base name',
input: `const myService = jasmine.createSpyObj(['methodA', 'methodB']);`,
expected: `const myService = {
methodA: vi.fn(),
methodB: vi.fn(),
};`,
},
{
description: 'should add a TODO if the second argument is not a literal',
input: `const myService = jasmine.createSpyObj('MyService', methodNames);`,
Expand All @@ -138,6 +147,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
methodB: vi.fn().mockName("MyService.methodB").mockReturnValue(42),
};`,
},
{
description:
'should transform jasmine.createSpyObj with an object of return values without base name',
input: `const myService = jasmine.createSpyObj({ methodA: 'foo', methodB: 42 });`,
expected: `const myService = {
methodA: vi.fn().mockReturnValue('foo'),
methodB: vi.fn().mockReturnValue(42),
};`,
},
{
description:
'should transform jasmine.createSpyObj with an object of return values containing an asymmetric matcher',
Expand All @@ -147,7 +165,7 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
};`,
},
{
description: 'should add a TODO for jasmine.createSpyObj with only one argument',
description: 'should add a TODO for jasmine.createSpyObj with only base name argument',
input: `const myService = jasmine.createSpyObj('MyService');`,
expected: `
// TODO: vitest-migration: jasmine.createSpyObj called with a single argument is not supported for transformation. See: https://vitest.dev/api/vi.html#vi-fn
Expand All @@ -170,6 +188,15 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
propA: 'valueA',
};`,
},
{
description:
'should transform jasmine.createSpyObj with a method map and a property map without base name',
input: `const myService = jasmine.createSpyObj({ methodA: 'foo' }, { propA: 'valueA' });`,
expected: `const myService = {
methodA: vi.fn().mockReturnValue('foo'),
propA: 'valueA',
};`,
},
{
description: 'should ignore non-string literals in the method array',
input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 123, someVar]);`,
Expand Down