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

Function createToHaveReturnedMatcher

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

Source from the content-addressed store, hash-verified

412 };
413
414const createToHaveReturnedMatcher = (): MatcherFunction<[unknown]> =>
415 function (received: any, expected): SyncExpectationResult {
416 const expectedArgument = '';
417 const options: MatcherHintOptions = {
418 isNot: this.isNot,
419 promise: this.promise,
420 };
421 ensureNoExpected(expected, 'toHaveReturned', options);
422 ensureMock(received, 'toHaveReturned', expectedArgument, options);
423
424 const receivedName = received.getMockName();
425
426 // Count return values that correspond only to calls that returned
427 const count = received.mock.results.reduce(
428 (n: number, result: any) => (result.type === 'return' ? n + 1 : n),
429 0,
430 );
431
432 const pass = count > 0;
433
434 const message = pass
435 ? () =>
436 // eslint-disable-next-line prefer-template
437 matcherHint(
438 'toHaveReturned',
439 receivedName,
440 expectedArgument,
441 options,
442 ) +
443 '\n\n' +
444 `Expected number of returns: ${printExpected(0)}\n` +
445 `Received number of returns: ${printReceived(count)}\n\n` +
446 received.mock.results
447 .reduce((lines: Array<string>, result: any, i: number) => {
448 if (result.type === 'return' && lines.length < PRINT_LIMIT) {
449 lines.push(`${i + 1}: ${printReceived(result.value)}`);
450 }
451
452 return lines;
453 }, [])
454 .join('\n') +
455 (received.mock.calls.length === count
456 ? ''
457 : `\n\nReceived number of calls: ${printReceived(
458 received.mock.calls.length,
459 )}`)
460 : () =>
461 // eslint-disable-next-line prefer-template
462 matcherHint(
463 'toHaveReturned',
464 receivedName,
465 expectedArgument,
466 options,
467 ) +
468 '\n\n' +
469 `Expected number of returns: >= ${printExpected(1)}\n` +
470 `Received number of returns: ${printReceived(count)}` +
471 (received.mock.calls.length === count

Callers 1

spyMatchers.tsFile · 0.85

Calls 6

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

Tested by

no test coverage detected