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

Function createToHaveReturnedTimesMatcher

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

Source from the content-addressed store, hash-verified

531 };
532
533const createToHaveReturnedTimesMatcher = (): MatcherFunction<[number]> =>
534 function (received: any, expected): SyncExpectationResult {
535 const expectedArgument = 'expected';
536 const options: MatcherHintOptions = {
537 isNot: this.isNot,
538 promise: this.promise,
539 };
540 ensureExpectedIsNonNegativeInteger(
541 expected,
542 'toHaveReturnedTimes',
543 options,
544 );
545 ensureMock(received, 'toHaveReturnedTimes', expectedArgument, options);
546
547 const receivedName = received.getMockName();
548
549 // Count return values that correspond only to calls that returned
550 const count = received.mock.results.reduce(
551 (n: number, result: any) => (result.type === 'return' ? n + 1 : n),
552 0,
553 );
554
555 const pass = count === expected;
556
557 const message = pass
558 ? () =>
559 // eslint-disable-next-line prefer-template
560 matcherHint(
561 'toHaveReturnedTimes',
562 receivedName,
563 expectedArgument,
564 options,
565 ) +
566 '\n\n' +
567 `Expected number of returns: not ${printExpected(expected)}` +
568 (received.mock.calls.length === count
569 ? ''
570 : `\n\nReceived number of calls: ${printReceived(
571 received.mock.calls.length,
572 )}`)
573 : () =>
574 // eslint-disable-next-line prefer-template
575 matcherHint(
576 'toHaveReturnedTimes',
577 receivedName,
578 expectedArgument,
579 options,
580 ) +
581 '\n\n' +
582 `Expected number of returns: ${printExpected(expected)}\n` +
583 `Received number of returns: ${printReceived(count)}` +
584 (received.mock.calls.length === count
585 ? ''
586 : `\nReceived number of calls: ${printReceived(
587 received.mock.calls.length,
588 )}`);
589
590 return {message, pass};

Callers 1

spyMatchers.tsFile · 0.85

Calls 6

matcherHintFunction · 0.90
printExpectedFunction · 0.90
printReceivedFunction · 0.90
ensureMockFunction · 0.85
getMockNameMethod · 0.80

Tested by

no test coverage detected