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 @@ -505,6 +505,53 @@ function transformThisFor(
);
}

function transformAllCallsArgs(
node: ts.Node,
{ sourceFile, reporter, pendingVitestValueImports }: RefactorContext,
): ts.Node {
if (
!ts.isPropertyAccessExpression(node) ||
!ts.isIdentifier(node.name) ||
node.name.text !== 'args'
) {
return node;
}

const elementAccess = node.expression;
if (!ts.isElementAccessExpression(elementAccess)) {
return node;
}

const allCall = elementAccess.expression;
if (!ts.isCallExpression(allCall) || !ts.isPropertyAccessExpression(allCall.expression)) {
return node;
}

const allPae = allCall.expression;
if (!ts.isIdentifier(allPae.name) || allPae.name.text !== 'all') {
return node;
}
Comment thread
clydin marked this conversation as resolved.

if (!ts.isPropertyAccessExpression(allPae.expression)) {
return node;
}

const spyIdentifier = getSpyIdentifierFromCalls(allPae.expression);
if (!spyIdentifier) {
return node;
}

reporter.reportTransformation(
sourceFile,
node,
'Transformed `spy.calls.all()[i].args` to `vi.mocked(spy).mock.calls[i]`.',
);
const mockProperty = createMockedSpyMockProperty(spyIdentifier, pendingVitestValueImports);
const callsProperty = createPropertyAccess(mockProperty, 'calls');

return ts.factory.createElementAccessExpression(callsProperty, elementAccess.argumentExpression);
}

export function transformSpyCallInspection(node: ts.Node, refactorCtx: RefactorContext): ts.Node {
const mostRecentArgsTransformed = transformMostRecentArgs(node, refactorCtx);
if (mostRecentArgsTransformed !== node) {
Expand All @@ -516,6 +563,11 @@ export function transformSpyCallInspection(node: ts.Node, refactorCtx: RefactorC
return thisForTransformed;
}

const allCallsArgsTransformed = transformAllCallsArgs(node, refactorCtx);
if (allCallsArgsTransformed !== node) {
return allCallsArgsTransformed;
}

if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) {
return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ describe('transformSpyCallInspection', () => {
input: `const allCalls = mySpy.calls.all();`,
expected: `const allCalls = vi.mocked(mySpy).mock.calls;`,
},
{
description: 'should transform spy.calls.all()[i].args',
input: `expect(mySpy.calls.all()[2].args[0]).toBeInstanceOf(RemoveShareUrlAction);`,
expected: `expect(vi.mocked(mySpy).mock.calls[2][0]).toBeInstanceOf(RemoveShareUrlAction);`,
},
{
description: 'should transform spy.calls.mostRecent().args',
input: `const recentArgs = mySpy.calls.mostRecent().args;`,
Expand Down
Loading