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

Function createMatcher

packages/expect/src/toThrowMatchers.ts:78–139  ·  view source on GitHub ↗
(
  matcherName: string,
  fromPromise?: boolean,
)

Source from the content-addressed store, hash-verified

76};
77
78export const createMatcher = (
79 matcherName: string,
80 fromPromise?: boolean,
81): MatcherFunction<[any]> =>
82 function (received, expected): ExpectationResult {
83 const options = {
84 isNot: this.isNot,
85 promise: this.promise,
86 };
87
88 let thrown = null;
89
90 if (fromPromise && isError(received)) {
91 thrown = getThrown(received);
92 } else {
93 if (typeof received === 'function') {
94 try {
95 received();
96 } catch (error) {
97 thrown = getThrown(error);
98 }
99 } else {
100 if (!fromPromise) {
101 const placeholder = expected === undefined ? '' : 'expected';
102 throw new Error(
103 matcherErrorMessage(
104 matcherHint(matcherName, undefined, placeholder, options),
105 `${RECEIVED_COLOR('received')} value must be a function`,
106 printWithType('Received', received, printReceived),
107 ),
108 );
109 }
110 }
111 }
112
113 if (expected === undefined) {
114 return toThrow(matcherName, options, thrown);
115 } else if (typeof expected === 'function') {
116 return toThrowExpectedClass(matcherName, options, thrown, expected);
117 } else if (typeof expected === 'string') {
118 return toThrowExpectedString(matcherName, options, thrown, expected);
119 } else if (expected !== null && typeof expected.test === 'function') {
120 return toThrowExpectedRegExp(matcherName, options, thrown, expected);
121 } else if (
122 expected !== null &&
123 typeof expected.asymmetricMatch === 'function'
124 ) {
125 return toThrowExpectedAsymmetric(matcherName, options, thrown, expected);
126 } else if (expected !== null && typeof expected === 'object') {
127 return toThrowExpectedObject(matcherName, options, thrown, expected);
128 } else {
129 throw new Error(
130 matcherErrorMessage(
131 matcherHint(matcherName, undefined, undefined, options),
132 `${EXPECTED_COLOR(
133 'expected',
134 )} value must be a string or regular expression or class or error`,
135 printWithType('Expected', expected, printExpected),

Callers 1

toThrowMatchers.tsFile · 0.85

Calls 12

isErrorFunction · 0.90
matcherErrorMessageFunction · 0.90
matcherHintFunction · 0.90
printWithTypeFunction · 0.90
getThrownFunction · 0.85
receivedFunction · 0.85
toThrowFunction · 0.85
toThrowExpectedClassFunction · 0.85
toThrowExpectedStringFunction · 0.85
toThrowExpectedRegExpFunction · 0.85
toThrowExpectedObjectFunction · 0.85

Tested by

no test coverage detected