All files / Scripts/jestMatchers stacksEqual.ts

58.06% Statements 18/31
60% Branches 6/10
57.14% Functions 4/7
64.28% Lines 18/28

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 611x             18x 18x 158x               158x       1x 1x 9x 9x 9x   9x 9x   9x     9x         9x 9x                 9x           1x            
import { expect } from "@jest/globals";
import type { MatcherFunction } from "expect";
 
// Strip stack of rows with jest.
// Used to normalize cross environment differences strictly for testing purposes
// Real stacks sent up will contain cross browser quirks
function cleanStack(stack: string[]) {
    Iif (!stack) return null;
    return stack.map(function (item: string): string {
        return item
            .replace(/[A-Z]:\\.*?\\MHA\\/, "") // Remove path prefix that start <drive letter>:\src\MHA
            .replace(/MHA\\src/, "src") // Remove path prefix that start MHA\\src
            .replace(/Function\.get \[as parse\]/, "Function.parse") // normalize function name
            .replace(/.*jest.*/, "") // Don't care about jest internals
            .replace(/:\d+:\d*\)/, ")") // remove column and line # since they may vary
        ;
    }).filter(function (item: string): boolean {
        return !!item;
    });
}
 
export const stacksEqual: MatcherFunction<[expected: string[]]> =
    function (actualUnknown: unknown, expected: string[]) {
        const actual = actualUnknown as string[];
        let passed = true;
        const messages: string[] = [];
 
        const actualStack = cleanStack(actual);
        const expectedStack = cleanStack(expected);
 
        Iif (actualStack === undefined || actualStack === null) {
            passed = false;
            messages.push("actual is undefined");
        } else Iif (expectedStack === undefined || expectedStack === null) {
            passed = false;
            messages.push("expected is undefined");
        }
        else {
            passed = this.equals(actualStack, expectedStack);
            Iif (!passed) {
                messages.push("Stacks do not match");
                messages.push("Actual stack:");
                actualStack.forEach((actualItem) => { messages.push("\t" + actualItem); });
                messages.push("Expected stack:");
                expectedStack.forEach((expectedItem) => { messages.push("\t" + expectedItem); });
            }
        }
 
        return {
            pass: passed,
            message: () => messages.join("\n"),
        };
    };
 
expect.extend({ stacksEqual, });
 
declare module "expect" {
    interface Matchers<R> {
        stacksEqual(expected: string[]): R;
    }
}