(received: number, expected: number, precision = 2)
| 140 | }, |
| 141 | |
| 142 | toBeCloseTo(received: number, expected: number, precision = 2) { |
| 143 | const matcherName = 'toBeCloseTo'; |
| 144 | const secondArgument = arguments.length === 3 ? 'precision' : undefined; |
| 145 | const isNot = this.isNot; |
| 146 | const options: MatcherHintOptions = { |
| 147 | isNot, |
| 148 | promise: this.promise, |
| 149 | secondArgument, |
| 150 | secondArgumentColor: (arg: string) => arg, |
| 151 | }; |
| 152 | |
| 153 | if (typeof expected !== 'number') { |
| 154 | throw new TypeError( |
| 155 | matcherErrorMessage( |
| 156 | matcherHint(matcherName, undefined, undefined, options), |
| 157 | `${EXPECTED_COLOR('expected')} value must be a number`, |
| 158 | printWithType('Expected', expected, printExpected), |
| 159 | ), |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | if (typeof received !== 'number') { |
| 164 | throw new TypeError( |
| 165 | matcherErrorMessage( |
| 166 | matcherHint(matcherName, undefined, undefined, options), |
| 167 | `${RECEIVED_COLOR('received')} value must be a number`, |
| 168 | printWithType('Received', received, printReceived), |
| 169 | ), |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | let pass = false; |
| 174 | let expectedDiff = 0; |
| 175 | let receivedDiff = 0; |
| 176 | |
| 177 | if ( |
| 178 | received === Number.POSITIVE_INFINITY && |
| 179 | expected === Number.POSITIVE_INFINITY |
| 180 | ) { |
| 181 | pass = true; // Infinity - Infinity is NaN |
| 182 | } else if ( |
| 183 | received === Number.NEGATIVE_INFINITY && |
| 184 | expected === Number.NEGATIVE_INFINITY |
| 185 | ) { |
| 186 | pass = true; // -Infinity - -Infinity is NaN |
| 187 | } else { |
| 188 | expectedDiff = Math.pow(10, -precision) / 2; |
| 189 | receivedDiff = Math.abs(expected - received); |
| 190 | pass = receivedDiff < expectedDiff; |
| 191 | } |
| 192 | |
| 193 | const message = pass |
| 194 | ? () => |
| 195 | // eslint-disable-next-line prefer-template |
| 196 | matcherHint(matcherName, undefined, undefined, options) + |
| 197 | '\n\n' + |
| 198 | `Expected: not ${printExpected(expected)}\n` + |
| 199 | (receivedDiff === 0 |
nothing calls this directly
no test coverage detected