Skip to content

Commit aea9899

Browse files
committed
fix(@schematics/angular): add a TODO for spy strategies configured with withArgs in refactor-jasmine-vitest
Spy strategies configured for specific arguments, such as `spyOn(service, 'calc').withArgs(1).and.returnValue(2)`, were converted to `vi.spyOn(service, 'calc').mockReturnValue(undefined).withArgs(1).mockReturnValue(2)` without a TODO comment. Vitest mocks have no `withArgs` method, so the migrated test failed with a TypeError. Add a TODO comment for these strategies instead, since they need to be migrated manually, for example with `mockImplementation()`.
1 parent ef600f2 commit aea9899

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ function transformSpyStrategy(node: ts.CallExpression, refactorCtx: RefactorCont
181181
}
182182

183183
if (spyCall) {
184+
// Vitest mocks have no `withArgs`, so a strategy for specific arguments cannot be converted.
185+
if (
186+
ts.isCallExpression(spyCall) &&
187+
ts.isPropertyAccessExpression(spyCall.expression) &&
188+
spyCall.expression.name.text === 'withArgs'
189+
) {
190+
const category = 'withArgs';
191+
reporter.recordTodo(category, sourceFile, node);
192+
addTodoComment(node, category);
193+
194+
return node;
195+
}
196+
184197
let newMethodName: string | undefined;
185198
let args = node.arguments;
186199

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ getSpy().and.callThrough();`,
145145
input: `spyOn(service, 'myMethod').and.unknownStrategy();`,
146146
expected: `// TODO: vitest-migration: Unsupported spy strategy ".and.unknownStrategy()" found. Please migrate this manually. See: https://vitest.dev/api/mocked.html#mock
147147
vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
148+
},
149+
{
150+
description: 'should add a TODO for a spy strategy configured with .withArgs()',
151+
input: `spyOn(service, 'myMethod').withArgs(1).and.returnValue(2);`,
152+
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
153+
vi.spyOn(service, 'myMethod').mockReturnValue(undefined).withArgs(1).and.returnValue(2);`,
154+
},
155+
{
156+
description: 'should add a TODO for .withArgs() on an existing spy',
157+
input: `mySpy.withArgs(1).and.returnValue(2);`,
158+
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
159+
mySpy.withArgs(1).and.returnValue(2);`,
148160
},
149161
{
150162
description: 'should correctly identify chained spies with element access (bracket notation)',

‎packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ export const TODO_NOTES = {
165165
' the original implementation but also clears the recorded calls.',
166166
url: 'https://vitest.dev/api/mock.html#mockreset',
167167
},
168+
'withArgs': {
169+
message:
170+
'Vitest does not have a direct equivalent for spy.withArgs().' +
171+
' Please migrate this manually, for example with mockImplementation() that checks the arguments.',
172+
url: 'https://vitest.dev/api/mock.html#mockimplementation',
173+
},
168174
'mostRecent-without-args': {
169175
message:
170176
'Direct usage of mostRecent() is not supported.' +

0 commit comments

Comments
 (0)