()
| 531 | }; |
| 532 | |
| 533 | const 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}; |
no test coverage detected