Skip to content
Open
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 @@ -181,6 +181,20 @@ function transformSpyStrategy(node: ts.CallExpression, refactorCtx: RefactorCont
}

if (spyCall) {
// Vitest mocks have no `withArgs`, so a strategy for specific arguments cannot be converted.
const unwrappedSpyCall = unwrapExpression(spyCall);
if (
ts.isCallExpression(unwrappedSpyCall) &&
ts.isPropertyAccessExpression(unwrappedSpyCall.expression) &&
unwrappedSpyCall.expression.name.text === 'withArgs'
) {
Comment thread
MeAkib marked this conversation as resolved.
Resolved
const category = 'withArgs';
reporter.recordTodo(category, sourceFile, node);
addTodoComment(node, category);

return node;
}

let newMethodName: string | undefined;
let args = node.arguments;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ getSpy().and.callThrough();`,
input: `spyOn(service, 'myMethod').and.unknownStrategy();`,
expected: `// TODO: vitest-migration: Unsupported spy strategy ".and.unknownStrategy()" found. Please migrate this manually. See: https://vitest.dev/api/mocked.html#mock
vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
},
{
description: 'should add a TODO for a spy strategy configured with .withArgs()',
input: `spyOn(service, 'myMethod').withArgs(1).and.returnValue(2);`,
expected: `// TODO: vitest-migration: Vitest does not have a direct equivalent for spy.withArgs(). Please migrate this manually, for example with mockImplementation() that checks the arguments. See: https://vitest.dev/api/mock.html#mockimplementation
vi.spyOn(service, 'myMethod').mockReturnValue(undefined).withArgs(1).and.returnValue(2);`,
},
{
description: 'should add a TODO for .withArgs() on an existing spy',
input: `mySpy.withArgs(1).and.returnValue(2);`,
expected: `// TODO: vitest-migration: Vitest does not have a direct equivalent for spy.withArgs(). Please migrate this manually, for example with mockImplementation() that checks the arguments. See: https://vitest.dev/api/mock.html#mockimplementation
mySpy.withArgs(1).and.returnValue(2);`,
},
{
description: 'should add a TODO for a wrapped .withArgs() call',
input: `mySpy.withArgs(1)!.and.returnValue(2);`,
expected: `// TODO: vitest-migration: Vitest does not have a direct equivalent for spy.withArgs(). Please migrate this manually, for example with mockImplementation() that checks the arguments. See: https://vitest.dev/api/mock.html#mockimplementation
mySpy.withArgs(1)!.and.returnValue(2);`,
},
{
description: 'should correctly identify chained spies with element access (bracket notation)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ export const TODO_NOTES = {
' the original implementation but also clears the recorded calls.',
url: 'https://vitest.dev/api/mock.html#mockreset',
},
'withArgs': {
message:
'Vitest does not have a direct equivalent for spy.withArgs().' +
' Please migrate this manually, for example with mockImplementation() that checks the arguments.',
url: 'https://vitest.dev/api/mock.html#mockimplementation',
},
'mostRecent-without-args': {
message:
'Direct usage of mostRecent() is not supported.' +
Expand Down
Loading