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

Function toContain

packages/expect/src/matchers.ts:465–567  ·  view source on GitHub ↗
(received: ContainIterable | string, expected: unknown)

Source from the content-addressed store, hash-verified

463 },
464
465 toContain(received: ContainIterable | string, expected: unknown) {
466 const matcherName = 'toContain';
467 const isNot = this.isNot;
468 const options: MatcherHintOptions = {
469 comment: 'indexOf',
470 isNot,
471 promise: this.promise,
472 };
473
474 if (received == null) {
475 throw new Error(
476 matcherErrorMessage(
477 matcherHint(matcherName, undefined, undefined, options),
478 `${RECEIVED_COLOR('received')} value must not be null nor undefined`,
479 printWithType('Received', received, printReceived),
480 ),
481 );
482 }
483
484 if (typeof received === 'string') {
485 const wrongTypeErrorMessage = `${EXPECTED_COLOR(
486 'expected',
487 )} value must be a string if ${RECEIVED_COLOR(
488 'received',
489 )} value is a string`;
490
491 if (typeof expected !== 'string') {
492 throw new TypeError(
493 matcherErrorMessage(
494 matcherHint(matcherName, received, String(expected), options),
495 wrongTypeErrorMessage,
496 // eslint-disable-next-line prefer-template
497 printWithType('Expected', expected, printExpected) +
498 '\n' +
499 printWithType('Received', received, printReceived),
500 ),
501 );
502 }
503
504 const index = received.indexOf(String(expected));
505 const pass = index !== -1;
506
507 const message = () => {
508 const labelExpected = `Expected ${
509 typeof expected === 'string' ? 'substring' : 'value'
510 }`;
511 const labelReceived = 'Received string';
512 const printLabel = getLabelPrinter(labelExpected, labelReceived);
513
514 return (
515 // eslint-disable-next-line prefer-template
516 matcherHint(matcherName, undefined, undefined, options) +
517 '\n\n' +
518 `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(
519 expected,
520 )}\n` +
521 `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
522 isNot

Callers

nothing calls this directly

Calls 3

matcherErrorMessageFunction · 0.90
matcherHintFunction · 0.90
printWithTypeFunction · 0.90

Tested by

no test coverage detected