Skip to content

Commit 9092f71

Browse files
committed
fixup! fix(@schematics/angular): add a TODO for spy strategies configured with withArgs in refactor-jasmine-vitest
1 parent 08c66cb commit 9092f71

2 files changed

Lines changed: 72 additions & 18 deletions

File tree

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

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,36 @@ function getUnwrappedParent(node: ts.Node): ts.Node | undefined {
7272
}
7373

7474
function isChainedWithAnd(node: ts.Node): boolean {
75+
let previous: ts.Node = node;
7576
let parent = node.parent;
7677
while (parent) {
77-
if (ts.isPropertyAccessExpression(parent)) {
78-
if (ts.isIdentifier(parent.name) && parent.name.text === 'and') {
78+
if (ts.isPropertyAccessExpression(parent) && ts.isIdentifier(parent.name)) {
79+
if (parent.name.text === 'and') {
7980
return true;
8081
}
81-
} else if (ts.isElementAccessExpression(parent)) {
82-
if (
83-
ts.isStringLiteralLike(parent.argumentExpression) &&
84-
parent.argumentExpression.text === 'and'
85-
) {
82+
// `withArgs(...)` returns the spy, so a strategy may still follow.
83+
if (parent.name.text === 'withArgs') {
84+
previous = parent;
85+
parent = parent.parent;
86+
continue;
87+
}
88+
} else if (
89+
ts.isElementAccessExpression(parent) &&
90+
ts.isStringLiteralLike(parent.argumentExpression)
91+
) {
92+
if (parent.argumentExpression.text === 'and') {
8693
return true;
8794
}
88-
} else if (isTransparentWrapper(parent)) {
95+
if (parent.argumentExpression.text === 'withArgs') {
96+
previous = parent;
97+
parent = parent.parent;
98+
continue;
99+
}
100+
} else if (
101+
isTransparentWrapper(parent) ||
102+
(ts.isCallExpression(parent) && parent.expression === previous)
103+
) {
104+
previous = parent;
89105
parent = parent.parent;
90106
continue;
91107
}
@@ -95,6 +111,33 @@ function isChainedWithAnd(node: ts.Node): boolean {
95111
return false;
96112
}
97113

114+
/**
115+
* Checks whether a spy expression chain contains a `withArgs(...)` link.
116+
*/
117+
function chainContainsWithArgs(expression: ts.Expression): boolean {
118+
let current: ts.Expression = unwrapExpression(expression);
119+
while (true) {
120+
if (ts.isCallExpression(current)) {
121+
current = unwrapExpression(current.expression);
122+
} else if (ts.isPropertyAccessExpression(current)) {
123+
if (ts.isIdentifier(current.name) && current.name.text === 'withArgs') {
124+
return true;
125+
}
126+
current = unwrapExpression(current.expression);
127+
} else if (ts.isElementAccessExpression(current)) {
128+
if (
129+
ts.isStringLiteralLike(current.argumentExpression) &&
130+
current.argumentExpression.text === 'withArgs'
131+
) {
132+
return true;
133+
}
134+
current = unwrapExpression(current.expression);
135+
} else {
136+
return false;
137+
}
138+
}
139+
}
140+
98141
function isSpyCreation(node: ts.Expression): boolean {
99142
const expression = unwrapExpression(node);
100143
if (!ts.isCallExpression(expression)) {
@@ -182,15 +225,14 @@ function transformSpyStrategy(node: ts.CallExpression, refactorCtx: RefactorCont
182225

183226
if (spyCall) {
184227
// Vitest mocks have no `withArgs`, so a strategy for specific arguments cannot be converted.
185-
const unwrappedSpyCall = unwrapExpression(spyCall);
186-
if (
187-
ts.isCallExpression(unwrappedSpyCall) &&
188-
ts.isPropertyAccessExpression(unwrappedSpyCall.expression) &&
189-
unwrappedSpyCall.expression.name.text === 'withArgs'
190-
) {
191-
const category = 'withArgs';
192-
reporter.recordTodo(category, sourceFile, node);
193-
addTodoComment(node, category);
228+
// The whole statement is left untouched, with a single TODO on the outermost strategy call;
229+
// inner `.and` chains reached while the visitor descends are covered by that TODO.
230+
if (chainContainsWithArgs(spyCall)) {
231+
if (!isChainedWithAnd(node)) {
232+
const category = 'withArgs';
233+
reporter.recordTodo(category, sourceFile, node);
234+
addTodoComment(node, category);
235+
}
194236

195237
return node;
196238
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,19 @@ vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
150150
description: 'should add a TODO for a spy strategy configured with .withArgs()',
151151
input: `spyOn(service, 'myMethod').withArgs(1).and.returnValue(2);`,
152152
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);`,
153+
vi.spyOn(service, 'myMethod').withArgs(1).and.returnValue(2);`,
154+
},
155+
{
156+
description: 'should add a single TODO for multiple chained .withArgs() strategies',
157+
input: `mySpy.withArgs(1).and.returnValue(10).withArgs(2).and.returnValue(20);`,
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(10).withArgs(2).and.returnValue(20);`,
160+
},
161+
{
162+
description: 'should not transform a default strategy chained after .withArgs()',
163+
input: `mySpy.withArgs(1).and.returnValue(2).and.returnValue(0);`,
164+
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
165+
mySpy.withArgs(1).and.returnValue(2).and.returnValue(0);`,
154166
},
155167
{
156168
description: 'should add a TODO for .withArgs() on an existing spy',

0 commit comments

Comments
 (0)