|
| 1 | +// Copyright (c) 2023 Developer Innovations, LLC |
| 2 | + |
| 3 | +import { |
| 4 | + MatcherHintOptions, |
| 5 | + matcherErrorMessage, |
| 6 | + matcherHint, |
| 7 | + RECEIVED_COLOR, |
| 8 | + printWithType, |
| 9 | + printReceived, |
| 10 | + getLabelPrinter, |
| 11 | + printExpected, |
| 12 | + INVERTED_COLOR, |
| 13 | + stringify, |
| 14 | +} from "jest-matcher-utils"; |
| 15 | +import type { SyncExpectationResult } from "expect"; |
| 16 | +import { equals, iterableEquality } from "@jest/expect-utils"; |
| 17 | +import * as getType from "jest-get-type"; |
| 18 | + |
| 19 | +function toContainEqualTimes( |
| 20 | + this: jest.MatcherContext, |
| 21 | + received: Array<unknown> | Set<unknown>, |
| 22 | + expected: unknown, |
| 23 | + times: number |
| 24 | +): SyncExpectationResult { |
| 25 | + const matcherName = "toContainEqualTimes"; |
| 26 | + const isNot = this.isNot; |
| 27 | + const options: MatcherHintOptions = { |
| 28 | + comment: "deep equality", |
| 29 | + isNot, |
| 30 | + promise: this.promise, |
| 31 | + }; |
| 32 | + |
| 33 | + if (received === null) { |
| 34 | + throw new Error( |
| 35 | + matcherErrorMessage( |
| 36 | + matcherHint(matcherName, undefined, undefined, options), |
| 37 | + `${RECEIVED_COLOR("received")} value must not be null nor undefined`, |
| 38 | + printWithType("Received", received, printReceived) |
| 39 | + ) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + const matchIndices = Array.from(received).reduce( |
| 44 | + (matchIndices: number[], item: unknown, index: number) => |
| 45 | + equals(item, expected, [...(this.customTesters ?? []), iterableEquality]) |
| 46 | + ? [...matchIndices, index] |
| 47 | + : matchIndices, |
| 48 | + [] |
| 49 | + ); |
| 50 | + |
| 51 | + const pass = matchIndices.length === times; |
| 52 | + |
| 53 | + const message = (): string => { |
| 54 | + const labelExpected = `Expected value ${times} time${ |
| 55 | + times !== 1 ? "s" : "" |
| 56 | + }`; |
| 57 | + const labelReceived = `Received ${(getType.getType ?? getType)( |
| 58 | + received |
| 59 | + )} with ${matchIndices.length} match${ |
| 60 | + matchIndices.length !== 1 ? "es" : "" |
| 61 | + }`; |
| 62 | + const printLabel = getLabelPrinter(labelExpected, labelReceived); |
| 63 | + |
| 64 | + return ( |
| 65 | + matcherHint(matcherName, undefined, undefined, options) + |
| 66 | + "\n\n" + |
| 67 | + `${printLabel(labelExpected)}${ |
| 68 | + isNot === true ? "not " : "" |
| 69 | + }${printExpected(expected)}\n` + |
| 70 | + `${printLabel(labelReceived)}${isNot === true ? " " : ""}${ |
| 71 | + isNot === true && Array.isArray(received) |
| 72 | + ? RECEIVED_COLOR( |
| 73 | + `[${received |
| 74 | + .map((item, i) => { |
| 75 | + const stringified = stringify(item); |
| 76 | + return matchIndices.includes(i) |
| 77 | + ? INVERTED_COLOR(stringified) |
| 78 | + : stringified; |
| 79 | + }) |
| 80 | + .join(", ")}]` |
| 81 | + ) |
| 82 | + : printReceived(received) |
| 83 | + }` |
| 84 | + ); |
| 85 | + }; |
| 86 | + |
| 87 | + return { message, pass }; |
| 88 | +} |
| 89 | + |
| 90 | +expect.extend({ |
| 91 | + toContainEqualTimes, |
| 92 | +}); |
| 93 | + |
| 94 | +declare global { |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
| 96 | + namespace jest { |
| 97 | + interface Matchers<R> { |
| 98 | + toContainEqualTimes(expected: unknown, times: number): R; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments