-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertObjectsEqual.js
36 lines (31 loc) · 1.07 KB
/
assertObjectsEqual.js
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
const eqArrays = (actual, expected) => {
return actual.length === expected.length && actual.every((val, index) => val === expected[index])
? true : false;
};
const eqObjects = function(object1, object2) {
const firstKeyArr = Object.keys(object1).sort();
const secondKeyArr = Object.keys(object2).sort();
if (firstKeyArr.length !== secondKeyArr.length) {
return false;
}
for (let key of firstKeyArr) {
if (Array.isArray(object1[key])) {
if (!eqArrays(object1[key], object2[key])) {
return false;
}
} else if (object1[key] !== object2[key]) {
return false;
}
}
return true;
};
const assertObjectsEqual = function(actual, expected) {
const inspect = require('util').inspect;
if (eqObjects(actual, expected)) {
console.log(`🟢🟢🟢 Assertion Passed: [${inspect(actual)}] === [${inspect(expected)}]`);
} else {
console.log(console.log(`🛑🛑🛑 Assertion Failed: [${inspect(actual)}] !== [${inspect(expected)}]`));
}
};
assertObjectsEqual({ a: '1', b: 2 }, { b: 2, a: '1' });
module.exports = assertObjectsEqual;