Skip to content

Commit

Permalink
Improve failure diffs for .toBeErrorInfo() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vladvelici committed Dec 4, 2024
1 parent ad7559b commit b428369
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions test/helper/expectations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as Ably from 'ably';
import { expect } from 'vitest';

const extractCommonKeys = (received: unknown, expected: unknown): Set<string> => {
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<string> => {
return new Set(Object.keys(expected as Ably.ErrorInfo));
};

const actualErrorInfo = (received: unknown, expected: unknown): Record<string, unknown> => {
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]]),
Expand All @@ -25,14 +22,6 @@ const actualErrorInfo = (received: unknown, expected: unknown): Record<string, u
return returnVal;
};

const expectedErrorInfo = (received: unknown, expected: unknown): Record<string, unknown> => {
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;
Expand All @@ -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,
};
Expand All @@ -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),
};
};
Expand Down

0 comments on commit b428369

Please sign in to comment.