All files / Scripts/jestMatchers receivedEqual.ts

74% Statements 37/50
78.57% Branches 22/28
25% Functions 1/4
78.94% Lines 30/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 723x         3x 3x 68x 68x 68x   68x 68x 1x           67x             67x 365x 365x 365x 365x 364x 294x             67x 544x 522x 480x 480x 480x   438x 3x 3x 3x               67x   67x           3x            
import { expect } from "@jest/globals";
import type { MatcherFunction } from "expect";
 
import { ReceivedRow } from "../row/ReceivedRow";
 
export const receivedEqual: MatcherFunction<[expected: { [index: string]: string | number | null }]> =
    function (actualUnknown: unknown, expected: { [index: string]: string | number | null }) {
        const actual = actualUnknown as ReceivedRow;
        let passed = true;
        const messages: string[] = [];
 
        try {
            if (typeof actual !== "object" || actual == null) {
                return {
                    message: () => "Actual is not an object",
                    pass: false,
                };
            }
 
            Iif (typeof expected !== "object" || expected == null) {
                return {
                    message: () => "Expected is not an object",
                    pass: false,
                };
            }
 
            for (const [field, value] of Object.entries(expected)) {
                Iif (field === "date") continue;
                Iif (field === "postFix") continue;
                Iif (field === "valueInternal") continue;
                if (value === null && actual[field]?.toString() === "null") continue;
                if (typeof value === "number" && value.toString() === actual[field]?.toString()) continue;
                if (typeof value === "string" && value === actual[field]?.toString()) continue;
 
                messages.push("actual: " + field + " = " + actual[field]);
                messages.push("expected: " + field + " = " + value);
                passed = false;
            }
 
            for (const field in actual) {
                if (field === "date") continue;
                if (field === "onGetUrl") continue;
                Iif (field === "setField") continue;
                Iif (field === "postFix") continue;
                if (field === "valueInternal") continue;
                // If a field in value is non-null/empty there must also be a field in expected
                if (actual[field] && actual[field].toString() && expected[field] === undefined) {
                    messages.push("actual: " + field + " = " + actual[field]);
                    messages.push("expected: " + field + " = " + expected[field]);
                    passed = false;
                }
            }
        }
        catch (e: unknown) {
            console.log(e);
        }
 
        if (messages.length === 0) { messages.push("Received rows are equal"); }
 
        return {
            message: () => messages.join("\n"),
            pass: passed
        };
    };
 
expect.extend({ receivedEqual, });
 
declare module "expect" {
    interface Matchers<R> {
        receivedEqual(expected: { [index: string]: string | number | null }): R;
    }
}