|
| 1 | +import { expect } from "@jest/globals"; |
| 2 | +import { |
| 3 | + EXPECTED_COLOR, |
| 4 | + RECEIVED_COLOR, |
| 5 | + ensureNoExpected, |
| 6 | + matcherErrorMessage, |
| 7 | + matcherHint, |
| 8 | + printReceived, |
| 9 | +} from "jest-matcher-utils"; |
| 10 | + |
| 11 | +import { BigDecimal } from "../src/BigDecimal"; |
| 12 | + |
| 13 | +import { printWithType } from "./print"; |
| 14 | + |
| 15 | +import type { DecimalValue } from "../src/BigDecimal"; |
| 16 | +import type { MatcherHintOptions } from "jest-matcher-utils"; |
| 17 | + |
| 18 | +expect.extend({ |
| 19 | + toEqualDecimal(received: unknown, expected: DecimalValue) { |
| 20 | + const matcherName = "toEqualDecimal"; |
| 21 | + const options: MatcherHintOptions = { |
| 22 | + isNot: this.isNot, |
| 23 | + promise: this.promise, |
| 24 | + }; |
| 25 | + expected = new BigDecimal(expected); |
| 26 | + |
| 27 | + if (!(received instanceof BigDecimal)) { |
| 28 | + return { |
| 29 | + pass: false, |
| 30 | + message: () => |
| 31 | + matcherErrorMessage( |
| 32 | + matcherHint(matcherName, undefined, undefined, options), |
| 33 | + `${RECEIVED_COLOR("received")} value must be a BigDecimal`, |
| 34 | + printWithType("Received", received, printReceived), |
| 35 | + ), |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + const pass = received.eq(expected); |
| 40 | + const message = pass |
| 41 | + ? () => |
| 42 | + // eslint-disable-next-line prefer-template |
| 43 | + matcherHint(matcherName, undefined, undefined, options) + |
| 44 | + "\n\n" + |
| 45 | + `Expected: ${EXPECTED_COLOR(expected)}` |
| 46 | + : () => |
| 47 | + // eslint-disable-next-line prefer-template |
| 48 | + matcherHint(matcherName, undefined, undefined, options) + |
| 49 | + "\n\n" + |
| 50 | + `Expected: ${EXPECTED_COLOR(expected)}\n` + |
| 51 | + `Received: ${RECEIVED_COLOR(received)}`; |
| 52 | + |
| 53 | + return { pass, message }; |
| 54 | + }, |
| 55 | + |
| 56 | + toBeEmpty(received: unknown, expected: void) { |
| 57 | + const matcherName = "toBeEmpty"; |
| 58 | + const options: MatcherHintOptions = { |
| 59 | + isNot: this.isNot, |
| 60 | + promise: this.promise, |
| 61 | + }; |
| 62 | + ensureNoExpected(expected, matcherName, options); |
| 63 | + |
| 64 | + if ( |
| 65 | + received === null || |
| 66 | + received === undefined || |
| 67 | + typeof (received as never)[Symbol.iterator] !== "function" |
| 68 | + ) { |
| 69 | + return { |
| 70 | + pass: false, |
| 71 | + message: () => |
| 72 | + matcherErrorMessage( |
| 73 | + matcherHint(matcherName, undefined, undefined, options), |
| 74 | + `${RECEIVED_COLOR("received")} value must be an iterable`, |
| 75 | + printWithType("Received", received, printReceived), |
| 76 | + ), |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + const pass = (received as Iterable<unknown>)[Symbol.iterator]().next().done === true; |
| 81 | + const message = pass |
| 82 | + ? () => |
| 83 | + // eslint-disable-next-line prefer-template |
| 84 | + matcherHint(matcherName, undefined, undefined, options) + |
| 85 | + "\n\n" + |
| 86 | + `Expected ${RECEIVED_COLOR("received")} to not be empty` |
| 87 | + : () => |
| 88 | + // eslint-disable-next-line prefer-template |
| 89 | + matcherHint(matcherName, undefined, undefined, options) + |
| 90 | + "\n\n" + |
| 91 | + `Received: ${printReceived(received)}`; |
| 92 | + |
| 93 | + return { pass, message }; |
| 94 | + }, |
| 95 | +}); |
0 commit comments