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

Function toMatch

packages/expect/src/matchers.ts:816–892  ·  view source on GitHub ↗
(received: string, expected: string | RegExp)

Source from the content-addressed store, hash-verified

814 },
815
816 toMatch(received: string, expected: string | RegExp) {
817 const matcherName = 'toMatch';
818 const options: MatcherHintOptions = {
819 isNot: this.isNot,
820 promise: this.promise,
821 };
822
823 if (typeof received !== 'string') {
824 throw new TypeError(
825 matcherErrorMessage(
826 matcherHint(matcherName, undefined, undefined, options),
827 `${RECEIVED_COLOR('received')} value must be a string`,
828 printWithType('Received', received, printReceived),
829 ),
830 );
831 }
832
833 if (
834 !(typeof expected === 'string') &&
835 !(expected && typeof expected.test === 'function')
836 ) {
837 throw new Error(
838 matcherErrorMessage(
839 matcherHint(matcherName, undefined, undefined, options),
840 `${EXPECTED_COLOR(
841 'expected',
842 )} value must be a string or regular expression`,
843 printWithType('Expected', expected, printExpected),
844 ),
845 );
846 }
847
848 const pass =
849 typeof expected === 'string'
850 ? received.includes(expected)
851 : new RegExp(expected).test(received);
852
853 const message = pass
854 ? () =>
855 typeof expected === 'string'
856 ? // eslint-disable-next-line prefer-template
857 matcherHint(matcherName, undefined, undefined, options) +
858 '\n\n' +
859 `Expected substring: not ${printExpected(expected)}\n` +
860 `Received string: ${printReceivedStringContainExpectedSubstring(
861 received,
862 received.indexOf(expected),
863 expected.length,
864 )}`
865 : // eslint-disable-next-line prefer-template
866 matcherHint(matcherName, undefined, undefined, options) +
867 '\n\n' +
868 `Expected pattern: not ${printExpected(expected)}\n` +
869 `Received string: ${printReceivedStringContainExpectedResult(
870 received,
871 typeof expected.exec === 'function'
872 ? expected.exec(received)
873 : null,

Callers

nothing calls this directly

Calls 10

matcherErrorMessageFunction · 0.90
matcherHintFunction · 0.90
printWithTypeFunction · 0.90
printExpectedFunction · 0.90
getLabelPrinterFunction · 0.90
printReceivedFunction · 0.90
execMethod · 0.80
testMethod · 0.45

Tested by

no test coverage detected