( matcherName: string, fromPromise?: boolean, )
| 76 | }; |
| 77 | |
| 78 | export 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), |
no test coverage detected