Print when partial deep equals fails

This commit is contained in:
Ajay Ramachandran
2021-08-10 23:13:56 -04:00
parent 1cbd162a22
commit d653f00a2d
2 changed files with 27 additions and 8 deletions

View File

@@ -12,7 +12,8 @@ describe("Test utils ", () => {
}, },
{ {
} },
false
), "Did not match empty expect"); ), "Did not match empty expect");
assert(partialDeepEquals( assert(partialDeepEquals(
[{a: [1,2,3]}, {a: [1,2]}], [{a: [1,2,3]}, {a: [1,2]}],
@@ -39,7 +40,8 @@ describe("Test utils ", () => {
{ {
vip: false, vip: false,
old: 19 old: 19
} },
false
), "Matched different properties"); ), "Matched different properties");
// do not match missing property // do not match missing property
assert(!partialDeepEquals( assert(!partialDeepEquals(
@@ -53,7 +55,8 @@ describe("Test utils ", () => {
child: { child: {
name: "" name: ""
} }
} },
false
)); ));
assert(!partialDeepEquals( assert(!partialDeepEquals(
{ {
@@ -69,7 +72,8 @@ describe("Test utils ", () => {
child: { child: {
name: "" name: ""
} }
} },
false
), "Matched incorrect child property"); ), "Matched incorrect child property");
assert(!partialDeepEquals( assert(!partialDeepEquals(
{ {
@@ -91,7 +95,8 @@ describe("Test utils ", () => {
name: "b", name: "b",
} }
} }
} },
false
), "Matched incorrected 2-nested property"); ), "Matched incorrected 2-nested property");
assert(partialDeepEquals( assert(partialDeepEquals(
{ {

View File

@@ -1,4 +1,5 @@
import {config} from "../src/config"; import {config} from "../src/config";
import { Logger } from "../src/utils/logger";
export function getbaseURL(): string { export function getbaseURL(): string {
return `http://localhost:${config.port}`; return `http://localhost:${config.port}`;
@@ -13,14 +14,27 @@ export type Done = (err?: any) => void;
* *
* Check object contains expected properties * Check object contains expected properties
*/ */
export const partialDeepEquals = (actual: Record<string, any>, expected: Record<string, any>): boolean => { export const partialDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
// loop over key, value of expected // loop over key, value of expected
for (const [ key, value ] of Object.entries(expected)) { for (const [ key, value ] of Object.entries(expected)) {
// if value is object or array, recurse // if value is object or array, recurse
if (Array.isArray(value) || typeof value === "object") { if (Array.isArray(value) || typeof value === "object") {
if (!partialDeepEquals(actual?.[key], value)) return false; if (!partialDeepEquals(actual?.[key], value, false)) {
if (print) printActualExpected(actual, expected);
return false;
}
}
else if (actual?.[key] !== value) {
if (print) printActualExpected(actual, expected);
return false;
} }
else if (actual?.[key] !== value) return false;
} }
return true; return true;
}; };
function printActualExpected(actual: Record<string, any>, expected: Record<string, any>): void {
Logger.error(`Actual: ${JSON.stringify(actual)}`);
Logger.error(`Expected: ${JSON.stringify(expected)}`);
}