Skip to content

Commit 3436f92

Browse files
MeAkibdgp1130
authored andcommitted
fix(@schematics/angular): keep the meaning of negated toHaveBeenCalledOnceWith in refactor-jasmine-vitest
`expect(spy).not.toHaveBeenCalledOnceWith(...)` was split into `not.toHaveBeenCalledTimes(1)` and `not.toHaveBeenCalledWith(...)`, which asserts that neither condition holds instead of not both. For example, a spy called twice with the same arguments passed before the migration and failed after it. Transform the negated form to `not.toHaveBeenCalledExactlyOnceWith(...)`, which has the same meaning.
1 parent ed38464 commit 3436f92

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,34 @@ export function transformCalledOnceWith(
474474
return node;
475475
}
476476

477+
const expectCall = call.expression.expression;
478+
const args = call.arguments;
479+
480+
// Splitting a negated assertion would assert that neither condition holds instead of not both.
481+
if (ts.isPropertyAccessExpression(expectCall) && expectCall.name.text === 'not') {
482+
reporter.reportTransformation(
483+
sourceFile,
484+
node,
485+
'Transformed `.not.toHaveBeenCalledOnceWith()` to `.not.toHaveBeenCalledExactlyOnceWith()`.',
486+
);
487+
488+
return ts.factory.updateExpressionStatement(
489+
node,
490+
ts.factory.updateCallExpression(
491+
call,
492+
createPropertyAccess(expectCall, 'toHaveBeenCalledExactlyOnceWith'),
493+
call.typeArguments,
494+
args,
495+
),
496+
);
497+
}
498+
477499
reporter.reportTransformation(
478500
sourceFile,
479501
node,
480502
'Transformed `.toHaveBeenCalledOnceWith()` to `.toHaveBeenCalledTimes(1)` and `.toHaveBeenCalledWith()`.',
481503
);
482504

483-
const expectCall = call.expression.expression;
484-
const args = call.arguments;
485-
486505
const timesCall = ts.factory.createCallExpression(
487506
createPropertyAccess(expectCall, 'toHaveBeenCalledTimes'),
488507
undefined,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ describe('transformCalledOnceWith', () => {
113113
expect(mySpy).toHaveBeenCalledWith('foo', 'bar');
114114
`,
115115
},
116+
{
117+
description: 'should transform a negated toHaveBeenCalledOnceWith(...) into a single call',
118+
input: `expect(mySpy).not.toHaveBeenCalledOnceWith('foo');`,
119+
expected: `expect(mySpy).not.toHaveBeenCalledExactlyOnceWith('foo');`,
120+
},
116121
];
117122

118123
testCases.forEach(({ description, input, expected }) => {

0 commit comments

Comments
 (0)