Skip to content
Open
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 @@ -474,15 +474,34 @@ export function transformCalledOnceWith(
return node;
}

const expectCall = call.expression.expression;
const args = call.arguments;

// Splitting a negated assertion would assert that neither condition holds instead of not both.
if (ts.isPropertyAccessExpression(expectCall) && expectCall.name.text === 'not') {
reporter.reportTransformation(
sourceFile,
node,
'Transformed `.not.toHaveBeenCalledOnceWith()` to `.not.toHaveBeenCalledExactlyOnceWith()`.',
);

return ts.factory.updateExpressionStatement(
node,
ts.factory.updateCallExpression(
call,
createPropertyAccess(expectCall, 'toHaveBeenCalledExactlyOnceWith'),
call.typeArguments,
args,
),
);
}

reporter.reportTransformation(
sourceFile,
node,
'Transformed `.toHaveBeenCalledOnceWith()` to `.toHaveBeenCalledTimes(1)` and `.toHaveBeenCalledWith()`.',
);

const expectCall = call.expression.expression;
const args = call.arguments;

const timesCall = ts.factory.createCallExpression(
createPropertyAccess(expectCall, 'toHaveBeenCalledTimes'),
undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ describe('transformCalledOnceWith', () => {
expect(mySpy).toHaveBeenCalledWith('foo', 'bar');
`,
},
{
description: 'should transform a negated toHaveBeenCalledOnceWith(...) into a single call',
input: `expect(mySpy).not.toHaveBeenCalledOnceWith('foo');`,
expected: `expect(mySpy).not.toHaveBeenCalledExactlyOnceWith('foo');`,
},
];

testCases.forEach(({ description, input, expected }) => {
Expand Down
Loading