aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/test-utils.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/utils/test-utils.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/utils/test-utils.ts')
-rw-r--r--packages/utils/test-utils.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/packages/utils/test-utils.ts b/packages/utils/test-utils.ts
new file mode 100644
index 0000000..1dfd14c
--- /dev/null
+++ b/packages/utils/test-utils.ts
@@ -0,0 +1,33 @@
+import { diffStringsUnified } from "jest-diff";
+
+expect.extend({
+ toCloselyEqualPoints(received, expected, precision) {
+ if (!Array.isArray(received) || !Array.isArray(expected)) {
+ throw new Error("expected and received are not point arrays");
+ }
+
+ const COMPARE = 1 / Math.pow(10, precision || 2);
+ const pass = expected.every(
+ (point, idx) =>
+ Math.abs(received[idx]?.[0] - point[0]) < COMPARE &&
+ Math.abs(received[idx]?.[1] - point[1]) < COMPARE,
+ );
+
+ if (!pass) {
+ return {
+ message: () => ` The provided array of points are not close enough.
+
+${diffStringsUnified(
+ JSON.stringify(expected, undefined, 2),
+ JSON.stringify(received, undefined, 2),
+)}`,
+ pass: false,
+ };
+ }
+
+ return {
+ message: () => `expected ${received} to not be close to ${expected}`,
+ pass: true,
+ };
+ },
+});