MCPcopy
hub / github.com/jestjs/jest / createToHaveBeenCalledWithMatcher

Function createToHaveBeenCalledWithMatcher

packages/expect/src/spyMatchers.ts:593–677  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

591 };
592
593const createToHaveBeenCalledWithMatcher = (): MatcherFunction<Array<unknown>> =>
594 function (received: any, ...expected): SyncExpectationResult {
595 const expectedArgument = '...expected';
596 const options: MatcherHintOptions = {
597 isNot: this.isNot,
598 promise: this.promise,
599 };
600 ensureMockOrSpy(
601 received,
602 'toHaveBeenCalledWith',
603 expectedArgument,
604 options,
605 );
606
607 const receivedIsSpy = isSpy(received);
608 const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
609
610 const calls = receivedIsSpy
611 ? received.calls.all().map((x: any) => x.args)
612 : received.mock.calls;
613
614 const pass = calls.some((call: any) => isEqualCall(expected, call));
615
616 const message = pass
617 ? () => {
618 // Some examples of calls that are equal to expected value.
619 const indexedCalls: Array<IndexedCall> = [];
620 let i = 0;
621 while (i < calls.length && indexedCalls.length < PRINT_LIMIT) {
622 if (isEqualCall(expected, calls[i])) {
623 indexedCalls.push([i, calls[i]]);
624 }
625 i += 1;
626 }
627
628 return (
629 // eslint-disable-next-line prefer-template
630 matcherHint(
631 'toHaveBeenCalledWith',
632 receivedName,
633 expectedArgument,
634 options,
635 ) +
636 '\n\n' +
637 `Expected: not ${printExpectedArgs(expected)}\n` +
638 (calls.length === 1 && stringify(calls[0]) === stringify(expected)
639 ? ''
640 : printReceivedCallsNegative(
641 expected,
642 indexedCalls,
643 calls.length === 1,
644 )) +
645 `\nNumber of calls: ${printReceived(calls.length)}`
646 );
647 }
648 : () => {
649 // Some examples of calls that are not equal to expected value.
650 const indexedCalls: Array<IndexedCall> = [];

Callers 1

spyMatchers.tsFile · 0.85

Calls 11

matcherHintFunction · 0.90
stringifyFunction · 0.90
printReceivedFunction · 0.90
ensureMockOrSpyFunction · 0.85
isEqualCallFunction · 0.85
printExpectedArgsFunction · 0.85
getMockNameMethod · 0.80
isSpyFunction · 0.70
isExpandFunction · 0.70

Tested by

no test coverage detected