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

Function toHaveLength

packages/expect/src/matchers.ts:656–709  ·  view source on GitHub ↗
(received: any, expected: number)

Source from the content-addressed store, hash-verified

654 },
655
656 toHaveLength(received: any, expected: number) {
657 const matcherName = 'toHaveLength';
658 const isNot = this.isNot;
659 const options: MatcherHintOptions = {
660 isNot,
661 promise: this.promise,
662 };
663
664 if (typeof received?.length !== 'number') {
665 throw new TypeError(
666 matcherErrorMessage(
667 matcherHint(matcherName, undefined, undefined, options),
668 `${RECEIVED_COLOR(
669 'received',
670 )} value must have a length property whose value must be a number`,
671 printWithType('Received', received, printReceived),
672 ),
673 );
674 }
675
676 ensureExpectedIsNonNegativeInteger(expected, matcherName, options);
677
678 const pass = received.length === expected;
679
680 const message = () => {
681 const labelExpected = 'Expected length';
682 const labelReceivedLength = 'Received length';
683 const labelReceivedValue = `Received ${getType(received)}`;
684 const printLabel = getLabelPrinter(
685 labelExpected,
686 labelReceivedLength,
687 labelReceivedValue,
688 );
689
690 return (
691 // eslint-disable-next-line prefer-template
692 matcherHint(matcherName, undefined, undefined, options) +
693 '\n\n' +
694 `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(
695 expected,
696 )}\n` +
697 (isNot
698 ? ''
699 : `${printLabel(labelReceivedLength)}${printReceived(
700 received.length,
701 )}\n`) +
702 `${printLabel(labelReceivedValue)}${isNot ? ' ' : ''}${printReceived(
703 received,
704 )}`
705 );
706 };
707
708 return {message, pass};
709 },
710
711 toHaveProperty(
712 received: object,

Callers

nothing calls this directly

Calls 4

matcherErrorMessageFunction · 0.90
matcherHintFunction · 0.90
printWithTypeFunction · 0.90

Tested by

no test coverage detected