I'm creating 2 objects based on given values in my jest test and expect that they would be equal except of the property uuid
which is generated indevidualy for each object.
uuid
can be deeply nested multiple times. for example:
const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }];
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }];
How can I pare the objects, ignoring uuid
property?
I'm creating 2 objects based on given values in my jest test and expect that they would be equal except of the property uuid
which is generated indevidualy for each object.
uuid
can be deeply nested multiple times. for example:
const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }];
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }];
How can I pare the objects, ignoring uuid
property?
-
You can omit keys with
_.omit
and deep pare objects with_.isEqual
– jabaa Commented Aug 23, 2021 at 10:11
3 Answers
Reset to default 3You can remove uuid first than pare them.
const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }]};
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }]};
const removeUuid = o => {
if (o) {
switch (typeof o) {
case "object":
delete o.uuid;
Object.keys(o).forEach(k => removeUuid(o[k]));
break;
case "array":
o.forEach(a => removeUuid(a));
}
}
}
removeUuid(object1);
removeUuid(object2);
expect(object1).toBe(object2);
You can use expect.any to ignore uuid
attribute. Just make sure that the attributes are existing, and the uuid is a string:
describe("Compare 2 objects", () => {
it("should ...", () => {
const actual = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' }] }] };
const expected = {
uuid: expect.any(String), // <--
name: 'xxx',
branches: [{ uuid: expect.any(String), children: [{ uuid: expect.any(String) }] }]
};
expect(actual).toEqual(expected);
});
});
I succeeded to achieve this with the following solution:
const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }] };
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }] };
const pareExcludeKeys = (object1, object2, excludeKeys = []) => {
if (Object.keys(object1).length !== Object.keys(object2).length) return false;
return Object.entries(object1).reduce((isEqual, [key, value]) => {
const isValueEqual = typeof value === 'object' && value !== null
? pareExcludeKeys(value, object2[key], excludeKeys)
: excludeKeys.includes(key) || object2[key] === value;
return isEqual && isValueEqual;
}, true);
};
console.log(pareExcludeKeys(object1, object2, ['uuid']));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366526a4624605.html
评论列表(0条)