mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 22:17:14 +03:00
Print when partial deep equals fails
This commit is contained in:
@@ -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(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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)}`);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user