pokerogue/test/test-utils/matchers/to-equal-unsorted.ts
Bertie690 1691951c87
[Test] Added Map key matcher; enforced strong typing on matchers (#6561)
* Added `toHaveKey` matcher + fixed imports

* Broke up the test matchers into multiple smaller interfaces

* Added restricted typing on matchers

Now we can't call `expect(game).toHaveFullHp()`!!!!!

* Updated comment

* Renamed `toEqualArrayUnsorted` into `toEqualUnsorted`

* Moved comment at top of file

* Fix `@module` doc comment

* Remove extra space

* Fix typo

* Fixed key ssue in matchers

* Update to-have-key.ts

* Update test/@types/vitest.d.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Fixed missing braces inside comment

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-09-20 17:24:27 -05:00

45 lines
1.6 KiB
TypeScript

import { getOnelineDiffStr } from "#test/test-utils/string-utils";
import { receivedStr } from "#test/test-utils/test-utils";
import type { MatcherState, SyncExpectationResult } from "@vitest/expect";
/**
* Matcher that checks if an array contains exactly the given items, disregarding order.
* @param received - The received value. Should be an array of elements
* @param expected - The array to check equality with
* @returns Whether the matcher passed
*/
export function toEqualUnsorted(this: MatcherState, received: unknown, expected: unknown[]): SyncExpectationResult {
if (!Array.isArray(received)) {
return {
pass: this.isNot,
message: () => `Expected to receive an array, but got ${receivedStr(received)}!`,
};
}
if (received.length !== expected.length) {
return {
pass: false,
message: () => `Expected to receive an array of length ${received.length}, but got ${expected.length} instead!`,
expected,
actual: received,
};
}
const actualSorted = received.toSorted();
const expectedSorted = expected.toSorted();
const pass = this.equals(actualSorted, expectedSorted, [...this.customTesters, this.utils.iterableEquality]);
const actualStr = getOnelineDiffStr.call(this, actualSorted);
const expectedStr = getOnelineDiffStr.call(this, expectedSorted);
return {
pass,
message: () =>
pass
? `Expected ${actualStr} to NOT exactly equal ${expectedStr} without order, but it did!`
: `Expected ${actualStr} to exactly equal ${expectedStr} without order, but it didn't!`,
expected: expectedSorted,
actual: actualSorted,
};
}