Skip to content

Commit 7da9c22

Browse files
committed
fix(@schematics/angular): migrate spy.calls.first().args in refactor-jasmine-vitest
`spy.calls.first()` is migrated to `vi.mocked(spy).mock.calls[0]`, which is alredy arguments array of first call, but `.args` after it was kept so `spy.calls.first().args[0]` became `vi.mocked(spy).mock.calls[0].args[0]`, which fails with TS2339 and throws at runtime `.args` after `first()` is now dropped same way as after `mostRecent()` and `all()[i]`
1 parent 70c2143 commit 7da9c22

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ function transformMostRecentArgs(
527527
return node;
528528
}
529529

530-
// Check 3: Is it a call to `.mostRecent`?
530+
// Check 3: Is it a call to `.mostRecent` or `.first`?
531531
const mostRecentPae = mostRecentCall.expression;
532532
if (
533533
!ts.isIdentifier(mostRecentPae.name) ||
534-
mostRecentPae.name.text !== 'mostRecent' ||
534+
(mostRecentPae.name.text !== 'mostRecent' && mostRecentPae.name.text !== 'first') ||
535535
!ts.isPropertyAccessExpression(mostRecentPae.expression)
536536
) {
537537
return node;
@@ -543,6 +543,17 @@ function transformMostRecentArgs(
543543
return node;
544544
}
545545

546+
if (mostRecentPae.name.text === 'first') {
547+
reporter.reportTransformation(
548+
sourceFile,
549+
node,
550+
'Transformed `spy.calls.first().args` to `vi.mocked(spy).mock.calls[0]`.',
551+
);
552+
const mockProperty = createMockedSpyMockProperty(spyIdentifier, pendingVitestValueImports);
553+
554+
return ts.factory.createElementAccessExpression(createPropertyAccess(mockProperty, 'calls'), 0);
555+
}
556+
546557
// If all checks pass, perform the transformation.
547558
reporter.reportTransformation(
548559
sourceFile,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ describe('transformSpyCallInspection', () => {
322322
input: `const firstCall = mySpy.calls.first();`,
323323
expected: `const firstCall = vi.mocked(mySpy).mock.calls[0];`,
324324
},
325+
{
326+
description: 'should transform spy.calls.first().args',
327+
input: `const firstArgs = mySpy.calls.first().args;`,
328+
expected: `const firstArgs = vi.mocked(mySpy).mock.calls[0];`,
329+
},
330+
{
331+
description: 'should transform spy.calls.first().args[i]',
332+
input: `expect(mySpy.calls.first().args[0]).toBe(1);`,
333+
expected: `expect(vi.mocked(mySpy).mock.calls[0][0]).toBe(1);`,
334+
},
325335
{
326336
description: 'should add a TODO for spy.calls.mostRecent() without .args',
327337
input: `const mostRecent = mySpy.calls.mostRecent();`,

0 commit comments

Comments
 (0)