From b428369d301ff0efd596c37a54988fb0226b01a6 Mon Sep 17 00:00:00 2001 From: Vlad Velici Date: Tue, 3 Dec 2024 20:33:37 +0000 Subject: [PATCH] Improve failure diffs for .toBeErrorInfo() helper --- test/helper/expectations.ts | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/test/helper/expectations.ts b/test/helper/expectations.ts index 416aa944..b83d985e 100644 --- a/test/helper/expectations.ts +++ b/test/helper/expectations.ts @@ -1,15 +1,12 @@ import * as Ably from 'ably'; import { expect } from 'vitest'; -const extractCommonKeys = (received: unknown, expected: unknown): Set => { - const receivedKeys = new Set(Object.keys(received as Ably.ErrorInfo)); - const expectedKeys = new Set(Object.keys(expected as Ably.ErrorInfo)); - - return new Set([...expectedKeys].filter((key) => receivedKeys.has(key))); +const extractExpectedKeys = (expected: unknown): Set => { + return new Set(Object.keys(expected as Ably.ErrorInfo)); }; const actualErrorInfo = (received: unknown, expected: unknown): Record => { - const commonKeys = extractCommonKeys(received, expected); + const commonKeys = extractExpectedKeys(expected); const returnVal = Object.fromEntries( [...commonKeys].map((key) => [key, (received as Ably.ErrorInfo)[key as keyof Ably.ErrorInfo]]), @@ -25,14 +22,6 @@ const actualErrorInfo = (received: unknown, expected: unknown): Record => { - const commonKeys = extractCommonKeys(received, expected); - - return Object.fromEntries( - [...commonKeys].map((key) => [key, (expected as Ably.ErrorInfo)[key as keyof Ably.ErrorInfo]]), - ); -}; - export interface ErrorInfoCompareType { code?: number; statusCode?: number; @@ -51,7 +40,7 @@ const toBeErrorInfo = (received: unknown, expected: ErrorInfoCompareType): Check if (!(received instanceof Ably.ErrorInfo)) { return { pass: false, - message: () => `Expected ErrorInfo`, + message: () => `Expected ErrorInfo, found ${typeof received}`, expected: expected, actual: received, }; @@ -67,7 +56,7 @@ const toBeErrorInfo = (received: unknown, expected: ErrorInfoCompareType): Check message: () => { return `Expected matching ErrorInfo`; }, - expected: expectedErrorInfo(received, expected), + expected: expected, actual: actualErrorInfo(received, expected), }; };